Show Account Balance
This commit is contained in:
parent
276fdb4ade
commit
6f8a94ff5d
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type AccountData struct {
|
||||
Accounts []postgres.Account
|
||||
Accounts []postgres.GetAccountsRow
|
||||
}
|
||||
|
||||
func (h *Handler) accounts(c *gin.Context) {
|
||||
|
@ -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)
|
||||
|
@ -5,5 +5,7 @@ VALUES ($1, $2)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetAccounts :many
|
||||
SELECT accounts.* FROM accounts
|
||||
WHERE accounts.budget_id = $1;
|
||||
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;
|
@ -11,7 +11,7 @@
|
||||
{{range .Accounts}}
|
||||
<div class="budget-item">
|
||||
<a href="account/{{.ID}}">{{.Name}}</a>
|
||||
<span class="time"></span>
|
||||
<span class="time">{{.Balance}}</span>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
Loading…
x
Reference in New Issue
Block a user