Show Account Balance

This commit is contained in:
2021-12-02 21:29:11 +00:00
parent 276fdb4ade
commit 6f8a94ff5d
4 changed files with 19 additions and 9 deletions

View File

@ -29,20 +29,28 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A
}
const getAccounts = `-- name: GetAccounts :many
SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts
SELECT accounts.id, accounts.name, SUM(transactions.amount) as balance FROM accounts
WHERE accounts.budget_id = $1
AND transactions.date < NOW()
GROUP BY accounts.id, accounts.name
`
func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Account, error) {
type GetAccountsRow struct {
ID uuid.UUID
Name string
Balance int64
}
func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]GetAccountsRow, error) {
rows, err := q.db.Query(ctx, getAccounts, budgetID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Account
var items []GetAccountsRow
for rows.Next() {
var i Account
if err := rows.Scan(&i.ID, &i.BudgetID, &i.Name); err != nil {
var i GetAccountsRow
if err := rows.Scan(&i.ID, &i.Name, &i.Balance); err != nil {
return nil, err
}
items = append(items, i)