feat: move remaining db package to core
Some checks failed
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Failing after 57s

This commit is contained in:
2025-12-26 06:34:36 +01:00
parent 75433834ed
commit 5e563f2c59
9 changed files with 122 additions and 133 deletions

View File

@@ -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
}

View File

@@ -1,6 +1,11 @@
package core
import "errors"
import (
"context"
"database/sql"
"errors"
"log/slog"
)
var (
ErrNotFound = errors.New("the value does not exist")
@@ -11,3 +16,28 @@ var (
ErrBadRequest = errors.New("bad request")
)
func TransformAndLogDbError(ctx context.Context, module string, r sql.Result, err error) error {
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
slog.ErrorContext(ctx, "database sql", "module", module, "err", err)
return ErrInternal
}
if r != nil {
rows, err := r.RowsAffected()
if err != nil {
slog.ErrorContext(ctx, "database rows affected", "module", module, "err", err)
return ErrInternal
}
if rows == 0 {
slog.InfoContext(ctx, "row not found", "module", module)
return ErrNotFound
}
}
return nil
}

View File

@@ -1,10 +1,9 @@
package db
package core
import (
"context"
"errors"
"log/slog"
"spend-sparrow/internal/core"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
@@ -25,7 +24,7 @@ func RunMigrations(ctx context.Context, db *sqlx.DB, pathPrefix string) error {
driver, err := sqlite3.WithInstance(db.DB, &sqlite3.Config{})
if err != nil {
slog.ErrorContext(ctx, "Could not create Migration instance", "err", err)
return core.ErrInternal
return ErrInternal
}
m, err := migrate.NewWithDatabaseInstance(
@@ -34,14 +33,14 @@ func RunMigrations(ctx context.Context, db *sqlx.DB, pathPrefix string) error {
driver)
if err != nil {
slog.ErrorContext(ctx, "Could not create migrations instance", "err", err)
return core.ErrInternal
return ErrInternal
}
m.Log = migrationLogger{}
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
slog.ErrorContext(ctx, "Could not run migrations", "err", err)
return core.ErrInternal
return ErrInternal
}
return nil

View File

@@ -1,34 +0,0 @@
package db
import (
"context"
"database/sql"
"errors"
"log/slog"
"spend-sparrow/internal/core"
)
func TransformAndLogDbError(ctx context.Context, module string, r sql.Result, err error) error {
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return core.ErrNotFound
}
slog.ErrorContext(ctx, "database sql", "module", module, "err", err)
return core.ErrInternal
}
if r != nil {
rows, err := r.RowsAffected()
if err != nil {
slog.ErrorContext(ctx, "database rows affected", "module", module, "err", err)
return core.ErrInternal
}
if rows == 0 {
slog.InfoContext(ctx, "row not found", "module", module)
return core.ErrNotFound
}
}
return nil
}

View File

@@ -10,7 +10,6 @@ import (
"spend-sparrow/internal/account"
"spend-sparrow/internal/authentication"
"spend-sparrow/internal/core"
"spend-sparrow/internal/db"
"spend-sparrow/internal/handler"
"spend-sparrow/internal/handler/middleware"
"spend-sparrow/internal/log"
@@ -58,7 +57,7 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
}
// init db
err = db.RunMigrations(ctx, database, migrationsPrefix)
err = core.RunMigrations(ctx, database, migrationsPrefix)
if err != nil {
return fmt.Errorf("could not run migrations: %w", err)
}

View File

@@ -4,7 +4,6 @@ import (
"context"
"spend-sparrow/internal/auth_types"
"spend-sparrow/internal/core"
"spend-sparrow/internal/db"
"spend-sparrow/internal/types"
"time"
@@ -36,7 +35,7 @@ func (s Dashboard) MainChart(
FROM "transaction"
WHERE user_id = ?
ORDER BY timestamp`, user.Id)
err = db.TransformAndLogDbError(ctx, "dashboard Chart", nil, err)
err = core.TransformAndLogDbError(ctx, "dashboard Chart", nil, err)
if err != nil {
return nil, err
}
@@ -92,7 +91,7 @@ func (s Dashboard) TreasureChests(
treasureChests := make([]*types.TreasureChest, 0)
err := s.db.SelectContext(ctx, &treasureChests, `SELECT * FROM treasure_chest WHERE user_id = ?`, user.Id)
err = db.TransformAndLogDbError(ctx, "dashboard TreasureChests", nil, err)
err = core.TransformAndLogDbError(ctx, "dashboard TreasureChests", nil, err)
if err != nil {
return nil, err
}
@@ -136,7 +135,7 @@ func (s Dashboard) TreasureChest(
WHERE user_id = ?
AND treasure_chest_id = ?
ORDER BY timestamp`, user.Id, treausureChestId)
err = db.TransformAndLogDbError(ctx, "dashboard Chart", nil, err)
err = core.TransformAndLogDbError(ctx, "dashboard Chart", nil, err)
if err != nil {
return nil, err
}

View File

@@ -7,7 +7,6 @@ import (
"log/slog"
"spend-sparrow/internal/auth_types"
"spend-sparrow/internal/core"
"spend-sparrow/internal/db"
"spend-sparrow/internal/types"
"strconv"
"time"
@@ -52,7 +51,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *auth_types.
if tx == nil {
ownsTransaction = true
tx, err = s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transaction Add", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Add", nil, err)
if err != nil {
return nil, err
}
@@ -71,7 +70,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *auth_types.
party, description, error, created_at, created_by)
VALUES (:id, :user_id, :account_id, :treasure_chest_id, :value, :timestamp,
:party, :description, :error, :created_at, :created_by)`, transaction)
err = db.TransformAndLogDbError(ctx, "transaction Insert", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Insert", r, err)
if err != nil {
return nil, err
}
@@ -81,7 +80,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *auth_types.
UPDATE account
SET current_balance = current_balance + ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Add", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Add", r, err)
if err != nil {
return nil, err
}
@@ -92,7 +91,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *auth_types.
UPDATE treasure_chest
SET current_balance = current_balance + ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Add", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Add", r, err)
if err != nil {
return nil, err
}
@@ -100,7 +99,7 @@ func (s TransactionImpl) Add(ctx context.Context, tx *sqlx.Tx, user *auth_types.
if ownsTransaction {
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transaction Add", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Add", nil, err)
if err != nil {
return nil, err
}
@@ -115,7 +114,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *auth_types.User, inpu
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transaction Update", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", nil, err)
if err != nil {
return nil, err
}
@@ -125,7 +124,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *auth_types.User, inpu
transaction := &types.Transaction{}
err = tx.GetContext(ctx, transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, input.Id)
err = db.TransformAndLogDbError(ctx, "transaction Update", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("transaction %v not found: %w", input.Id, core.ErrBadRequest)
@@ -138,7 +137,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *auth_types.User, inpu
UPDATE account
SET current_balance = current_balance - ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", r, err)
if err != nil {
return nil, err
}
@@ -148,7 +147,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *auth_types.User, inpu
UPDATE treasure_chest
SET current_balance = current_balance - ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", r, err)
if err != nil {
return nil, err
}
@@ -164,7 +163,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *auth_types.User, inpu
UPDATE account
SET current_balance = current_balance + ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", r, err)
if err != nil {
return nil, err
}
@@ -174,7 +173,7 @@ func (s TransactionImpl) Update(ctx context.Context, user *auth_types.User, inpu
UPDATE treasure_chest
SET current_balance = current_balance + ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", r, err)
if err != nil {
return nil, err
}
@@ -194,13 +193,13 @@ func (s TransactionImpl) Update(ctx context.Context, user *auth_types.User, inpu
updated_by = :updated_by
WHERE id = :id
AND user_id = :user_id`, transaction)
err = db.TransformAndLogDbError(ctx, "transaction Update", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", r, err)
if err != nil {
return nil, err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transaction Update", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Update", nil, err)
if err != nil {
return nil, err
}
@@ -220,7 +219,7 @@ func (s TransactionImpl) Get(ctx context.Context, user *auth_types.User, id stri
var transaction types.Transaction
err = s.db.GetContext(ctx, &transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
err = db.TransformAndLogDbError(ctx, "transaction Get", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Get", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("transaction %v not found: %w", id, core.ErrBadRequest)
@@ -271,7 +270,7 @@ func (s TransactionImpl) GetAll(ctx context.Context, user *auth_types.User, filt
filter.Error,
page_size,
offset)
err = db.TransformAndLogDbError(ctx, "transaction GetAll", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction GetAll", nil, err)
if err != nil {
return nil, err
}
@@ -290,7 +289,7 @@ func (s TransactionImpl) Delete(ctx context.Context, user *auth_types.User, id s
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
if err != nil {
return nil
}
@@ -300,7 +299,7 @@ func (s TransactionImpl) Delete(ctx context.Context, user *auth_types.User, id s
var transaction types.Transaction
err = tx.GetContext(ctx, &transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
err = db.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
if err != nil {
return err
}
@@ -311,7 +310,7 @@ func (s TransactionImpl) Delete(ctx context.Context, user *auth_types.User, id s
SET current_balance = current_balance - ?
WHERE id = ?
AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Delete", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Delete", r, err)
if err != nil && !errors.Is(err, core.ErrNotFound) {
return err
}
@@ -323,20 +322,20 @@ func (s TransactionImpl) Delete(ctx context.Context, user *auth_types.User, id s
SET current_balance = current_balance - ?
WHERE id = ?
AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Delete", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Delete", r, err)
if err != nil && !errors.Is(err, core.ErrNotFound) {
return err
}
}
r, err := tx.ExecContext(ctx, "DELETE FROM \"transaction\" WHERE id = ? AND user_id = ?", uuid, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction Delete", r, err)
err = core.TransformAndLogDbError(ctx, "transaction Delete", r, err)
if err != nil {
return err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction Delete", nil, err)
if err != nil {
return err
}
@@ -350,7 +349,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
if err != nil {
return err
}
@@ -362,7 +361,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
UPDATE account
SET current_balance = 0
WHERE user_id = ?`, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
if err != nil && !errors.Is(err, core.ErrNotFound) {
return err
}
@@ -371,7 +370,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
UPDATE treasure_chest
SET current_balance = 0
WHERE user_id = ?`, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
if err != nil && !errors.Is(err, core.ErrNotFound) {
return err
}
@@ -380,7 +379,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
SELECT *
FROM "transaction"
WHERE user_id = ?`, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
if err != nil && !errors.Is(err, core.ErrNotFound) {
return err
}
@@ -394,7 +393,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
var transaction types.Transaction
for rows.Next() {
err = rows.StructScan(&transaction)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
if err != nil {
return err
}
@@ -405,7 +404,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
SET error = ?
WHERE user_id = ?
AND id = ?`, transaction.Error, user.Id, transaction.Id)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
if err != nil {
return err
}
@@ -419,7 +418,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
UPDATE account
SET current_balance = current_balance + ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.AccountId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
if err != nil {
return err
}
@@ -429,7 +428,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
UPDATE treasure_chest
SET current_balance = current_balance + ?
WHERE id = ? AND user_id = ?`, transaction.Value, transaction.TreasureChestId, user.Id)
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", r, err)
if err != nil {
return err
}
@@ -437,7 +436,7 @@ func (s TransactionImpl) RecalculateBalances(ctx context.Context, user *auth_typ
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction RecalculateBalances", nil, err)
if err != nil {
return err
}
@@ -475,7 +474,7 @@ func (s TransactionImpl) validateAndEnrichTransaction(ctx context.Context, tx *s
if input.AccountId != nil {
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, input.AccountId, userId)
err = db.TransformAndLogDbError(ctx, "transaction validate", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction validate", nil, err)
if err != nil {
return nil, err
}
@@ -488,7 +487,7 @@ func (s TransactionImpl) validateAndEnrichTransaction(ctx context.Context, tx *s
if input.TreasureChestId != nil {
var treasureChest types.TreasureChest
err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, input.TreasureChestId, userId)
err = db.TransformAndLogDbError(ctx, "transaction validate", nil, err)
err = core.TransformAndLogDbError(ctx, "transaction validate", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("treasure chest not found: %w", core.ErrBadRequest)

View File

@@ -8,7 +8,6 @@ import (
"math"
"spend-sparrow/internal/auth_types"
"spend-sparrow/internal/core"
"spend-sparrow/internal/db"
"spend-sparrow/internal/types"
"strconv"
"time"
@@ -53,7 +52,7 @@ func (s TransactionRecurringImpl) Add(ctx context.Context,
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Add", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Add", nil, err)
if err != nil {
return nil, err
}
@@ -72,13 +71,13 @@ func (s TransactionRecurringImpl) Add(ctx context.Context,
VALUES (:id, :user_id, :interval_months,
:next_execution, :party, :description, :account_id, :treasure_chest_id, :value, :created_at, :created_by)`,
transactionRecurring)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Insert", r, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Insert", r, err)
if err != nil {
return nil, err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transactionRecurring Add", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Add", nil, err)
if err != nil {
return nil, err
}
@@ -100,7 +99,7 @@ func (s TransactionRecurringImpl) Update(ctx context.Context,
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Update", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Update", nil, err)
if err != nil {
return nil, err
}
@@ -110,7 +109,7 @@ func (s TransactionRecurringImpl) Update(ctx context.Context,
transactionRecurring := &types.TransactionRecurring{}
err = tx.GetContext(ctx, transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Update", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Update", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("transactionRecurring %v not found: %w", input.Id, core.ErrBadRequest)
@@ -137,13 +136,13 @@ func (s TransactionRecurringImpl) Update(ctx context.Context,
updated_by = :updated_by
WHERE id = :id
AND user_id = :user_id`, transactionRecurring)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Update", r, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Update", r, err)
if err != nil {
return nil, err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transactionRecurring Update", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Update", nil, err)
if err != nil {
return nil, err
}
@@ -163,7 +162,7 @@ func (s TransactionRecurringImpl) GetAll(ctx context.Context, user *auth_types.U
WHERE user_id = ?
ORDER BY created_at DESC`,
user.Id)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAll", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAll", nil, err)
if err != nil {
return nil, err
}
@@ -183,7 +182,7 @@ func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *aut
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAllByAccount", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAllByAccount", nil, err)
if err != nil {
return nil, err
}
@@ -193,7 +192,7 @@ func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *aut
var rowCount int
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, user.Id)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAllByAccount", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAllByAccount", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("account %v not found: %w", accountId, core.ErrBadRequest)
@@ -209,13 +208,13 @@ func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *aut
AND account_id = ?
ORDER BY created_at DESC`,
user.Id, accountUuid)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAll", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAll", nil, err)
if err != nil {
return nil, err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAllByAccount", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAllByAccount", nil, err)
if err != nil {
return nil, err
}
@@ -238,7 +237,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context,
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAllByTreasureChest", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAllByTreasureChest", nil, err)
if err != nil {
return nil, err
}
@@ -248,7 +247,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context,
var rowCount int
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestId, user.Id)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAllByTreasureChest", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAllByTreasureChest", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("treasurechest %v not found: %w", treasureChestId, core.ErrBadRequest)
@@ -264,13 +263,13 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context,
AND treasure_chest_id = ?
ORDER BY created_at DESC`,
user.Id, treasureChestUuid)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAll", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAll", nil, err)
if err != nil {
return nil, err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transactionRecurring GetAllByTreasureChest", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GetAllByTreasureChest", nil, err)
if err != nil {
return nil, err
}
@@ -289,7 +288,7 @@ func (s TransactionRecurringImpl) Delete(ctx context.Context, user *auth_types.U
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Delete", nil, err)
if err != nil {
return nil
}
@@ -299,19 +298,19 @@ func (s TransactionRecurringImpl) Delete(ctx context.Context, user *auth_types.U
var transactionRecurring types.TransactionRecurring
err = tx.GetContext(ctx, &transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Delete", nil, err)
if err != nil {
return err
}
r, err := tx.ExecContext(ctx, "DELETE FROM transaction_recurring WHERE id = ? AND user_id = ?", uuid, user.Id)
err = db.TransformAndLogDbError(ctx, "transactionRecurring Delete", r, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Delete", r, err)
if err != nil {
return err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transactionRecurring Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring Delete", nil, err)
if err != nil {
return err
}
@@ -323,7 +322,7 @@ func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context) erro
now := s.clock.Now()
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", nil, err)
if err != nil {
return err
}
@@ -335,7 +334,7 @@ func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context) erro
err = tx.SelectContext(ctx, &recurringTransactions, `
SELECT * FROM transaction_recurring WHERE next_execution <= ?`,
now)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", nil, err)
if err != nil {
return err
}
@@ -361,14 +360,14 @@ func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context) erro
nextExecution := transactionRecurring.NextExecution.AddDate(0, int(transactionRecurring.IntervalMonths), 0)
r, err := tx.ExecContext(ctx, `UPDATE transaction_recurring SET next_execution = ? WHERE id = ? AND user_id = ?`,
nextExecution, transactionRecurring.Id, user.Id)
err = db.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", r, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", r, err)
if err != nil {
return err
}
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring GenerateTransactions", nil, err)
if err != nil {
return err
}
@@ -422,7 +421,7 @@ func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring(
}
accountUuid = &temp
err = tx.GetContext(ctx, &rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, userId)
err = db.TransformAndLogDbError(ctx, "transactionRecurring validate", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring validate", nil, err)
if err != nil {
return nil, err
}
@@ -443,7 +442,7 @@ func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring(
treasureChestUuid = &temp
var treasureChest types.TreasureChest
err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestUuid, userId)
err = db.TransformAndLogDbError(ctx, "transactionRecurring validate", nil, err)
err = core.TransformAndLogDbError(ctx, "transactionRecurring validate", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("treasure chest not found: %w", core.ErrBadRequest)

View File

@@ -8,7 +8,6 @@ import (
"slices"
"spend-sparrow/internal/auth_types"
"spend-sparrow/internal/core"
"spend-sparrow/internal/db"
"spend-sparrow/internal/types"
"github.com/google/uuid"
@@ -82,7 +81,7 @@ func (s TreasureChestImpl) Add(ctx context.Context, user *auth_types.User, paren
r, err := s.db.NamedExecContext(ctx, `
INSERT INTO treasure_chest (id, parent_id, user_id, name, current_balance, created_at, created_by)
VALUES (:id, :parent_id, :user_id, :name, :current_balance, :created_at, :created_by)`, treasureChest)
err = db.TransformAndLogDbError(ctx, "treasureChest Insert", r, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Insert", r, err)
if err != nil {
return nil, err
}
@@ -105,7 +104,7 @@ func (s TreasureChestImpl) Update(ctx context.Context, user *auth_types.User, id
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
if err != nil {
return nil, err
}
@@ -115,7 +114,7 @@ func (s TreasureChestImpl) Update(ctx context.Context, user *auth_types.User, id
treasureChest := &types.TreasureChest{}
err = tx.GetContext(ctx, treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, id)
err = db.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("treasureChest %v not found: %w", idStr, err)
@@ -131,7 +130,7 @@ func (s TreasureChestImpl) Update(ctx context.Context, user *auth_types.User, id
}
var childCount int
err = tx.GetContext(ctx, &childCount, `SELECT COUNT(*) FROM treasure_chest WHERE user_id = ? AND parent_id = ?`, user.Id, id)
err = db.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
if err != nil {
return nil, err
}
@@ -158,13 +157,13 @@ func (s TreasureChestImpl) Update(ctx context.Context, user *auth_types.User, id
updated_by = :updated_by
WHERE id = :id
AND user_id = :user_id`, treasureChest)
err = db.TransformAndLogDbError(ctx, "treasureChest Update", r, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Update", r, err)
if err != nil {
return nil, err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Update", nil, err)
if err != nil {
return nil, err
}
@@ -184,7 +183,7 @@ func (s TreasureChestImpl) Get(ctx context.Context, user *auth_types.User, id st
var treasureChest types.TreasureChest
err = s.db.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, uuid)
err = db.TransformAndLogDbError(ctx, "treasureChest Get", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Get", nil, err)
if err != nil {
if errors.Is(err, core.ErrNotFound) {
return nil, fmt.Errorf("treasureChest %v not found: %w", id, err)
@@ -202,7 +201,7 @@ func (s TreasureChestImpl) GetAll(ctx context.Context, user *auth_types.User) ([
treasureChests := make([]*types.TreasureChest, 0)
err := s.db.SelectContext(ctx, &treasureChests, `SELECT * FROM treasure_chest WHERE user_id = ?`, user.Id)
err = db.TransformAndLogDbError(ctx, "treasureChest GetAll", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest GetAll", nil, err)
if err != nil {
return nil, err
}
@@ -221,7 +220,7 @@ func (s TreasureChestImpl) Delete(ctx context.Context, user *auth_types.User, id
}
tx, err := s.db.BeginTxx(ctx, nil)
err = db.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
if err != nil {
return nil
}
@@ -231,7 +230,7 @@ func (s TreasureChestImpl) Delete(ctx context.Context, user *auth_types.User, id
childCount := 0
err = tx.GetContext(ctx, &childCount, `SELECT COUNT(*) FROM treasure_chest WHERE user_id = ? AND parent_id = ?`, user.Id, id)
err = db.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
if err != nil {
return err
}
@@ -244,7 +243,7 @@ func (s TreasureChestImpl) Delete(ctx context.Context, user *auth_types.User, id
err = tx.GetContext(ctx, &transactionsCount,
`SELECT COUNT(*) FROM "transaction" WHERE user_id = ? AND treasure_chest_id = ?`,
user.Id, id)
err = db.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
if err != nil {
return err
}
@@ -256,7 +255,7 @@ func (s TreasureChestImpl) Delete(ctx context.Context, user *auth_types.User, id
err = tx.GetContext(ctx, &recurringCount, `
SELECT COUNT(*) FROM transaction_recurring WHERE user_id = ? AND treasure_chest_id = ?`,
user.Id, id)
err = db.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
if err != nil {
return err
}
@@ -265,13 +264,13 @@ func (s TreasureChestImpl) Delete(ctx context.Context, user *auth_types.User, id
}
r, err := tx.ExecContext(ctx, `DELETE FROM treasure_chest WHERE id = ? AND user_id = ?`, id, user.Id)
err = db.TransformAndLogDbError(ctx, "treasureChest Delete", r, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Delete", r, err)
if err != nil {
return err
}
err = tx.Commit()
err = db.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
err = core.TransformAndLogDbError(ctx, "treasureChest Delete", nil, err)
if err != nil {
return err
}