fix error handling on postgres repositories

This commit is contained in:
Andre Medeiros 2022-05-23 11:49:48 -04:00
parent c35ec006f1
commit ef3f3ff18b
4 changed files with 11 additions and 57 deletions

View file

@ -2,7 +2,6 @@ package repository
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/christianselig/apollo-backend/internal/domain" "github.com/christianselig/apollo-backend/internal/domain"
@ -141,7 +140,7 @@ func (p *postgresAccountRepository) Update(ctx context.Context, acc *domain.Acco
check_count = $10 check_count = $10
WHERE id = $1` WHERE id = $1`
res, err := p.conn.Exec( _, err := p.conn.Exec(
ctx, ctx,
query, query,
acc.ID, acc.ID,
@ -156,19 +155,12 @@ func (p *postgresAccountRepository) Update(ctx context.Context, acc *domain.Acco
acc.CheckCount, acc.CheckCount,
) )
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }
func (p *postgresAccountRepository) Delete(ctx context.Context, id int64) error { func (p *postgresAccountRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM accounts WHERE id = $1` query := `DELETE FROM accounts WHERE id = $1`
res, err := p.conn.Exec(ctx, query, id) _, err := p.conn.Exec(ctx, query, id)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }
@ -184,11 +176,7 @@ func (p *postgresAccountRepository) Associate(ctx context.Context, acc *domain.A
func (p *postgresAccountRepository) Disassociate(ctx context.Context, acc *domain.Account, dev *domain.Device) error { func (p *postgresAccountRepository) Disassociate(ctx context.Context, acc *domain.Account, dev *domain.Device) error {
query := `DELETE FROM devices_accounts WHERE account_id = $1 AND device_id = $2` query := `DELETE FROM devices_accounts WHERE account_id = $1 AND device_id = $2`
res, err := p.conn.Exec(ctx, query, acc.ID, dev.ID) _, err := p.conn.Exec(ctx, query, acc.ID, dev.ID)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }

View file

@ -2,7 +2,6 @@ package repository
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/christianselig/apollo-backend/internal/domain" "github.com/christianselig/apollo-backend/internal/domain"
@ -157,22 +156,14 @@ func (p *postgresDeviceRepository) Update(ctx context.Context, dev *domain.Devic
SET expires_at = $2, grace_period_expires_at = $3 SET expires_at = $2, grace_period_expires_at = $3
WHERE id = $1` WHERE id = $1`
res, err := p.conn.Exec(ctx, query, dev.ID, dev.ExpiresAt, dev.GracePeriodExpiresAt) _, err := p.conn.Exec(ctx, query, dev.ID, dev.ExpiresAt, dev.GracePeriodExpiresAt)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }
func (p *postgresDeviceRepository) Delete(ctx context.Context, token string) error { func (p *postgresDeviceRepository) Delete(ctx context.Context, token string) error {
query := `DELETE FROM devices WHERE apns_token = $1` query := `DELETE FROM devices WHERE apns_token = $1`
res, err := p.conn.Exec(ctx, query, token) _, err := p.conn.Exec(ctx, query, token)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }
@ -185,11 +176,7 @@ func (p *postgresDeviceRepository) SetNotifiable(ctx context.Context, dev *domai
global_mute = $3 global_mute = $3
WHERE device_id = $4 AND account_id = $5` WHERE device_id = $4 AND account_id = $5`
res, err := p.conn.Exec(ctx, query, inbox, watcher, global, dev.ID, acct.ID) _, err := p.conn.Exec(ctx, query, inbox, watcher, global, dev.ID, acct.ID)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }

View file

@ -2,7 +2,6 @@ package repository
import ( import (
"context" "context"
"fmt"
"strings" "strings"
"github.com/christianselig/apollo-backend/internal/domain" "github.com/christianselig/apollo-backend/internal/domain"
@ -97,10 +96,6 @@ func (p *postgresUserRepository) CreateOrUpdate(ctx context.Context, u *domain.U
func (p *postgresUserRepository) Delete(ctx context.Context, id int64) error { func (p *postgresUserRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM users WHERE id = $1` query := `DELETE FROM users WHERE id = $1`
res, err := p.conn.Exec(ctx, query, id) _, err := p.conn.Exec(ctx, query, id)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }

View file

@ -2,7 +2,6 @@ package repository
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/christianselig/apollo-backend/internal/domain" "github.com/christianselig/apollo-backend/internal/domain"
@ -255,7 +254,7 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.
label = $9 label = $9
WHERE id = $1` WHERE id = $1`
res, err := p.conn.Exec( _, err := p.conn.Exec(
ctx, ctx,
query, query,
watcher.ID, watcher.ID,
@ -269,38 +268,23 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.
watcher.Label, watcher.Label,
) )
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }
func (p *postgresWatcherRepository) IncrementHits(ctx context.Context, id int64) error { func (p *postgresWatcherRepository) IncrementHits(ctx context.Context, id int64) error {
query := `UPDATE watchers SET hits = hits + 1, last_notified_at = $2 WHERE id = $1` query := `UPDATE watchers SET hits = hits + 1, last_notified_at = $2 WHERE id = $1`
res, err := p.conn.Exec(ctx, query, id, time.Now()) _, err := p.conn.Exec(ctx, query, id, time.Now())
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }
func (p *postgresWatcherRepository) Delete(ctx context.Context, id int64) error { func (p *postgresWatcherRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM watchers WHERE id = $1` query := `DELETE FROM watchers WHERE id = $1`
res, err := p.conn.Exec(ctx, query, id) _, err := p.conn.Exec(ctx, query, id)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }
func (p *postgresWatcherRepository) DeleteByTypeAndWatcheeID(ctx context.Context, typ domain.WatcherType, id int64) error { func (p *postgresWatcherRepository) DeleteByTypeAndWatcheeID(ctx context.Context, typ domain.WatcherType, id int64) error {
query := `DELETE FROM watchers WHERE type = $1 AND watchee_id = $2` query := `DELETE FROM watchers WHERE type = $1 AND watchee_id = $2`
res, err := p.conn.Exec(ctx, query, typ, id) _, err := p.conn.Exec(ctx, query, typ, id)
if res.RowsAffected() == 0 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err return err
} }