Skip to content

Commit

Permalink
src: now update return count of updated rows
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitrii Aleksandrov <[email protected]>
  • Loading branch information
Insei committed Feb 3, 2025
1 parent c708520 commit 68082d9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (r *repository[TModel]) Insert(ctx context.Context, model *TModel, qFns ...
return nil
}

func (r *repository[TModel]) Update(ctx context.Context, model *TModel, qFns ...func(m *TModel, h query.UpdateHelper[TModel])) (err error) {
func (r *repository[TModel]) Update(ctx context.Context, model *TModel, qFns ...func(m *TModel, h query.UpdateHelper[TModel])) (count int64, err error) {
r.beforeUpdate(ctx, model)
stmt := sqlstmt.NewUpdate(ctx, r.columns, r.table)
r.persistentQuery.Apply(stmt)
Expand All @@ -176,14 +176,14 @@ func (r *repository[TModel]) Update(ctx context.Context, model *TModel, qFns ...

updatedCount, err := r.executor.Update(ctx, stmt, model)
if err != nil {
return r.errorTransformer(err)
return updatedCount, r.errorTransformer(err)
}

if updatedCount < 1 {
return r.errorTransformer(fmt.Errorf("nothing to update: %w", ErrNotFound))
return updatedCount, r.errorTransformer(fmt.Errorf("nothing to update: %w", ErrNotFound))
}
r.afterUpdate(ctx, model)
return nil
return updatedCount, nil
}

func (r *repository[TModel]) Delete(ctx context.Context, qFns ...func(m *TModel, h query.DeleteHelper[TModel])) (count int64, err error) {
Expand Down
2 changes: 1 addition & 1 deletion repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func TestRepository_Update(t *testing.T) {
repoCasted.executor = tt.executor

ctx := context.Background()
err = repo.Update(ctx, tt.model, tt.qFns...)
_, err = repo.Update(ctx, tt.model, tt.qFns...)
if !errors.Is(err, tt.expectedErr) {
t.Errorf("expected error %v, got %v", tt.expectedErr, err)
}
Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Repository[TModel any] interface {
// Insert adds a new record to the database using the provided model and query options.
Insert(ctx context.Context, model *TModel, qFns ...func(m *TModel, h query.InsertHelper[TModel])) (err error)
// Update modifies an existing record in the database based on the provided model and query options.
Update(ctx context.Context, model *TModel, qFns ...func(m *TModel, h query.UpdateHelper[TModel])) (err error)
Update(ctx context.Context, model *TModel, qFns ...func(m *TModel, h query.UpdateHelper[TModel])) (count int64, err error)
// Delete removes records from the database based on the query conditions and returns the count of deleted records.
Delete(ctx context.Context, qFns ...func(m *TModel, h query.DeleteHelper[TModel])) (count int64, err error)
}
Expand Down

0 comments on commit 68082d9

Please sign in to comment.