Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shal committed Feb 4, 2024
1 parent 0306bfa commit 347bef8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions pkg/store/sqlstore/operation_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlstore
import (
"context"
"database/sql"
"fmt"

"github.com/opencars/operations/pkg/domain/model"
)
Expand Down Expand Up @@ -32,19 +33,19 @@ func (r *OperationRepository) Create(ctx context.Context, operations ...*model.O
)
if err != nil {
_ = tx.Rollback()
return err
return fmt.Errorf("failed to prepare named statement: %w", err)
}

for _, op := range operations {
if _, err := stmt.ExecContext(ctx, op); err != nil {
_ = tx.Rollback()
return err
return fmt.Errorf("failed to execute named statement: %w", err)
}
}

if err := tx.Commit(); err != nil {
_ = tx.Rollback()
return err
return fmt.Errorf("failed to commit transaction: %w", err)
}

return nil
Expand All @@ -63,7 +64,7 @@ func (r *OperationRepository) FindByNumber(ctx context.Context, number string, l
number, limit,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to select operations by number: %w", err)
}

for i := range operations {
Expand All @@ -86,7 +87,7 @@ func (r *OperationRepository) FindByVIN(ctx context.Context, vin string, limit u
vin, limit,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to select operations by vin: %w", err)
}

for i := range operations {
Expand All @@ -105,7 +106,7 @@ func (r *OperationRepository) DeleteByResourceID(ctx context.Context, id int64)

amount, err := res.RowsAffected()
if err != nil {
return 0, err
return 0, fmt.Errorf("failed to delete operations by resource_id: %w", err)
}

return amount, nil
Expand Down
5 changes: 3 additions & 2 deletions pkg/store/sqlstore/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"

"github.com/opencars/operations/pkg/domain/model"
)
Expand All @@ -27,14 +28,14 @@ func (r *ResourceRepository) Create(ctx context.Context, resource *model.Resourc
resource,
)
if err != nil {
return err
return fmt.Errorf("failed to execute named statement: %w", err)
}

defer rows.Close()

for rows.Next() {
if err := rows.Scan(&resource.ID); err != nil {
return err
return fmt.Errorf("failed to scan: %w", err)
}
}

Expand Down

0 comments on commit 347bef8

Please sign in to comment.