Also display account & payee

This commit is contained in:
Jan Bader 2021-12-02 20:56:49 +00:00
parent a4659c7133
commit 276fdb4ade
5 changed files with 45 additions and 10 deletions

View File

@ -11,7 +11,7 @@ import (
type BudgetData struct { type BudgetData struct {
Budget *postgres.Budget Budget *postgres.Budget
Transactions []postgres.Transaction Transactions []postgres.GetTransactionsForBudgetRow
} }
func (h *Handler) budget(c *gin.Context) { func (h *Handler) budget(c *gin.Context) {

View File

@ -48,3 +48,17 @@ func (tx Transaction) GetPositive() bool {
amount := tx.GetAmount() amount := tx.GetAmount()
return amount >= 0 return amount >= 0
} }
func (tx GetTransactionsForBudgetRow) GetAmount() float64 {
var amount float64
err := tx.Amount.AssignTo(&amount)
if err != nil {
panic(err)
}
return amount
}
func (tx GetTransactionsForBudgetRow) GetPositive() bool {
amount := tx.GetAmount()
return amount >= 0
}

View File

@ -5,8 +5,11 @@ VALUES ($1, $2, $3, $4, $5)
RETURNING *; RETURNING *;
-- name: GetTransactionsForBudget :many -- name: GetTransactionsForBudget :many
SELECT transactions.* FROM transactions SELECT transactions.id, transactions.date, transactions.memo, transactions.amount,
LEFT JOIN accounts ON accounts.id = transactions.account_id accounts.name as account, payees.name as payee
FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id
INNER JOIN payees ON payees.id = transactions.payee_id
WHERE accounts.budget_id = $1 WHERE accounts.budget_id = $1
ORDER BY transactions.date DESC ORDER BY transactions.date DESC
LIMIT 200; LIMIT 200;

View File

@ -81,29 +81,41 @@ func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.
} }
const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions SELECT transactions.id, transactions.date, transactions.memo, transactions.amount,
LEFT JOIN accounts ON accounts.id = transactions.account_id accounts.name as account, payees.name as payee
FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id
INNER JOIN payees ON payees.id = transactions.payee_id
WHERE accounts.budget_id = $1 WHERE accounts.budget_id = $1
ORDER BY transactions.date DESC ORDER BY transactions.date DESC
LIMIT 200 LIMIT 200
` `
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]Transaction, error) { type GetTransactionsForBudgetRow struct {
ID uuid.UUID
Date time.Time
Memo string
Amount pgtype.Numeric
Account string
Payee string
}
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]GetTransactionsForBudgetRow, error) {
rows, err := q.db.Query(ctx, getTransactionsForBudget, budgetID) rows, err := q.db.Query(ctx, getTransactionsForBudget, budgetID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var items []Transaction var items []GetTransactionsForBudgetRow
for rows.Next() { for rows.Next() {
var i Transaction var i GetTransactionsForBudgetRow
if err := rows.Scan( if err := rows.Scan(
&i.ID, &i.ID,
&i.Date, &i.Date,
&i.Memo, &i.Memo,
&i.Amount, &i.Amount,
&i.AccountID, &i.Account,
&i.PayeeID, &i.Payee,
); err != nil { ); err != nil {
return nil, err return nil, err
} }

View File

@ -16,6 +16,12 @@
{{range .Transactions}} {{range .Transactions}}
<tr> <tr>
<td>{{.Date}}</td> <td>{{.Date}}</td>
<td>
{{.Account}}
</td>
<td>
{{.Payee}}
</td>
<td> <td>
<a href="transaction/{{.ID}}">{{.Memo}}</a> <a href="transaction/{{.ID}}">{{.Memo}}</a>
</td> </td>