From ef3f3ff18b25cf15a9ad23ee53ead6c651217544 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 23 May 2022 11:49:48 -0400 Subject: [PATCH] fix error handling on postgres repositories --- internal/repository/postgres_account.go | 18 +++--------------- internal/repository/postgres_device.go | 19 +++---------------- internal/repository/postgres_user.go | 7 +------ internal/repository/postgres_watcher.go | 24 ++++-------------------- 4 files changed, 11 insertions(+), 57 deletions(-) diff --git a/internal/repository/postgres_account.go b/internal/repository/postgres_account.go index cba45f4..6e938ba 100644 --- a/internal/repository/postgres_account.go +++ b/internal/repository/postgres_account.go @@ -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 } diff --git a/internal/repository/postgres_device.go b/internal/repository/postgres_device.go index 5152669..8e01bc9 100644 --- a/internal/repository/postgres_device.go +++ b/internal/repository/postgres_device.go @@ -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 } diff --git a/internal/repository/postgres_user.go b/internal/repository/postgres_user.go index 946f9e7..22b05e9 100644 --- a/internal/repository/postgres_user.go +++ b/internal/repository/postgres_user.go @@ -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 } diff --git a/internal/repository/postgres_watcher.go b/internal/repository/postgres_watcher.go index 2f686dc..a17c618 100644 --- a/internal/repository/postgres_watcher.go +++ b/internal/repository/postgres_watcher.go @@ -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 }