feat: move remaining db package to core
Some checks failed
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Failing after 57s
Some checks failed
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Failing after 57s
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"log/slog"
|
||||
"spend-sparrow/internal/auth_types"
|
||||
"spend-sparrow/internal/core"
|
||||
"spend-sparrow/internal/db"
|
||||
"spend-sparrow/internal/service"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -70,7 +69,7 @@ func (s ServiceImpl) Add(ctx context.Context, user *auth_types.User, name string
|
||||
r, err := s.db.NamedExecContext(ctx, `
|
||||
INSERT INTO account (id, user_id, name, current_balance, oink_balance, created_at, created_by)
|
||||
VALUES (:id, :user_id, :name, :current_balance, :oink_balance, :created_at, :created_by)`, account)
|
||||
err = db.TransformAndLogDbError(ctx, "account Insert", r, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Insert", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -93,7 +92,7 @@ func (s ServiceImpl) UpdateName(ctx context.Context, user *auth_types.User, id s
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError(ctx, "account Update", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -103,7 +102,7 @@ func (s ServiceImpl) UpdateName(ctx context.Context, user *auth_types.User, id s
|
||||
|
||||
var account Account
|
||||
err = tx.GetContext(ctx, &account, `SELECT * FROM account WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError(ctx, "account Update", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Update", nil, err)
|
||||
if err != nil {
|
||||
if errors.Is(err, core.ErrNotFound) {
|
||||
return nil, fmt.Errorf("account %v not found: %w", id, core.ErrBadRequest)
|
||||
@@ -124,13 +123,13 @@ func (s ServiceImpl) UpdateName(ctx context.Context, user *auth_types.User, id s
|
||||
updated_by = :updated_by
|
||||
WHERE id = :id
|
||||
AND user_id = :user_id`, account)
|
||||
err = db.TransformAndLogDbError(ctx, "account Update", r, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Update", r, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
err = db.TransformAndLogDbError(ctx, "account Update", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Update", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -151,7 +150,7 @@ func (s ServiceImpl) Get(ctx context.Context, user *auth_types.User, id string)
|
||||
var account Account
|
||||
err = s.db.GetContext(ctx, &account, `
|
||||
SELECT * FROM account WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError(ctx, "account Get", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Get", nil, err)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "account get", "err", err)
|
||||
return nil, err
|
||||
@@ -168,7 +167,7 @@ func (s ServiceImpl) GetAll(ctx context.Context, user *auth_types.User) ([]*Acco
|
||||
accounts := make([]*Account, 0)
|
||||
err := s.db.SelectContext(ctx, &accounts, `
|
||||
SELECT * FROM account WHERE user_id = ? ORDER BY name`, user.Id)
|
||||
err = db.TransformAndLogDbError(ctx, "account GetAll", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account GetAll", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -187,7 +186,7 @@ func (s ServiceImpl) Delete(ctx context.Context, user *auth_types.User, id strin
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
err = db.TransformAndLogDbError(ctx, "account Delete", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -197,7 +196,7 @@ func (s ServiceImpl) Delete(ctx context.Context, user *auth_types.User, id strin
|
||||
|
||||
transactionsCount := 0
|
||||
err = tx.GetContext(ctx, &transactionsCount, `SELECT COUNT(*) FROM "transaction" WHERE user_id = ? AND account_id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError(ctx, "account Delete", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -206,13 +205,13 @@ func (s ServiceImpl) Delete(ctx context.Context, user *auth_types.User, id strin
|
||||
}
|
||||
|
||||
res, err := tx.ExecContext(ctx, "DELETE FROM account WHERE id = ? and user_id = ?", uuid, user.Id)
|
||||
err = db.TransformAndLogDbError(ctx, "account Delete", res, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Delete", res, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
err = db.TransformAndLogDbError(ctx, "account Delete", nil, err)
|
||||
err = core.TransformAndLogDbError(ctx, "account Delete", nil, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user