feat: deposit funds

This commit is contained in:
juancwu 2026-05-03 18:06:04 +00:00
commit 4298890d68
8 changed files with 353 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import (
type TransactionRepository interface {
CreateBillAtomic(t *model.Transaction, newBalance decimal.Decimal, categoryID *string) error
CreateDepositAtomic(t *model.Transaction, newBalance decimal.Decimal) error
ListByAccount(accountID string, limit, offset int) ([]*model.Transaction, error)
CountByAccount(accountID string) (int, error)
}
@ -54,6 +55,30 @@ func (r *transactionRepository) CreateBillAtomic(t *model.Transaction, newBalanc
})
}
func (r *transactionRepository) CreateDepositAtomic(t *model.Transaction, newBalance decimal.Decimal) error {
return WithTx(r.db, func(tx *sqlx.Tx) error {
insertTxn := `
INSERT INTO transactions
(id, value, type, account_id, title, description, occurred_at, created_at, updated_at)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9);
`
if _, err := tx.Exec(
insertTxn,
t.ID, t.Value, t.Type, t.AccountID, t.Title, t.Description,
t.OccurredAt, t.CreatedAt, t.UpdatedAt,
); err != nil {
return err
}
updateBalance := `UPDATE accounts SET balance = $1, updated_at = $2 WHERE id = $3;`
if _, err := tx.Exec(updateBalance, newBalance, time.Now(), t.AccountID); err != nil {
return err
}
return nil
})
}
func (r *transactionRepository) ListByAccount(accountID string, limit, offset int) ([]*model.Transaction, error) {
query := `
SELECT id, value, type, account_id, title, description, occurred_at, created_at, updated_at