feat: deposit funds
This commit is contained in:
parent
054f1227f0
commit
4298890d68
8 changed files with 353 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue