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

View file

@ -2,7 +2,6 @@ package repository
import (
"context"
"fmt"
"time"
"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
WHERE id = $1`
res, 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())
}
_, err := p.conn.Exec(ctx, query, dev.ID, dev.ExpiresAt, dev.GracePeriodExpiresAt)
return err
}
func (p *postgresDeviceRepository) Delete(ctx context.Context, token string) error {
query := `DELETE FROM devices WHERE apns_token = $1`
res, err := p.conn.Exec(ctx, query, token)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
_, err := p.conn.Exec(ctx, query, token)
return err
}
@ -185,11 +176,7 @@ func (p *postgresDeviceRepository) SetNotifiable(ctx context.Context, dev *domai
global_mute = $3
WHERE device_id = $4 AND account_id = $5`
res, 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())
}
_, err := p.conn.Exec(ctx, query, inbox, watcher, global, dev.ID, acct.ID)
return err
}

View file

@ -2,7 +2,6 @@ package repository
import (
"context"
"fmt"
"strings"
"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 {
query := `DELETE FROM users WHERE id = $1`
res, err := p.conn.Exec(ctx, query, id)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
_, err := p.conn.Exec(ctx, query, id)
return err
}

View file

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