Split displayed accounts by on- or off-budget
This commit is contained in:
parent
04fd687324
commit
f4ddf12214
@ -9,8 +9,10 @@ import (
|
||||
)
|
||||
|
||||
type AlwaysNeededData struct {
|
||||
Budget postgres.Budget
|
||||
Accounts []postgres.GetAccountsWithBalanceRow
|
||||
Budget postgres.Budget
|
||||
Accounts []postgres.GetAccountsWithBalanceRow
|
||||
OnBudgetAccounts []postgres.GetAccountsWithBalanceRow
|
||||
OffBudgetAccounts []postgres.GetAccountsWithBalanceRow
|
||||
}
|
||||
|
||||
func (h *Handler) getImportantData(c *gin.Context) {
|
||||
@ -34,9 +36,20 @@ func (h *Handler) getImportantData(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var onBudgetAccounts, offBudgetAccounts []postgres.GetAccountsWithBalanceRow
|
||||
for _, account := range accounts {
|
||||
if account.OnBudget {
|
||||
onBudgetAccounts = append(onBudgetAccounts, account)
|
||||
} else {
|
||||
offBudgetAccounts = append(offBudgetAccounts, account)
|
||||
}
|
||||
}
|
||||
|
||||
base := AlwaysNeededData{
|
||||
Accounts: accounts,
|
||||
Budget: budget,
|
||||
Accounts: accounts,
|
||||
OnBudgetAccounts: onBudgetAccounts,
|
||||
OffBudgetAccounts: offBudgetAccounts,
|
||||
Budget: budget,
|
||||
}
|
||||
|
||||
c.Set("data", base)
|
||||
|
@ -85,7 +85,7 @@ func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Accoun
|
||||
}
|
||||
|
||||
const getAccountsWithBalance = `-- name: GetAccountsWithBalance :many
|
||||
SELECT accounts.id, accounts.name, SUM(transactions.amount)::decimal(12,2) as balance
|
||||
SELECT accounts.id, accounts.name, accounts.on_budget, SUM(transactions.amount)::decimal(12,2) as balance
|
||||
FROM accounts
|
||||
LEFT JOIN transactions ON transactions.account_id = accounts.id
|
||||
WHERE accounts.budget_id = $1
|
||||
@ -95,9 +95,10 @@ ORDER BY accounts.name
|
||||
`
|
||||
|
||||
type GetAccountsWithBalanceRow struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
Balance Numeric
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
OnBudget bool
|
||||
Balance Numeric
|
||||
}
|
||||
|
||||
func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID) ([]GetAccountsWithBalanceRow, error) {
|
||||
@ -109,7 +110,12 @@ func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID
|
||||
var items []GetAccountsWithBalanceRow
|
||||
for rows.Next() {
|
||||
var i GetAccountsWithBalanceRow
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.Balance); err != nil {
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.OnBudget,
|
||||
&i.Balance,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
|
@ -14,7 +14,7 @@ WHERE accounts.budget_id = $1
|
||||
ORDER BY accounts.name;
|
||||
|
||||
-- name: GetAccountsWithBalance :many
|
||||
SELECT accounts.id, accounts.name, SUM(transactions.amount)::decimal(12,2) as balance
|
||||
SELECT accounts.id, accounts.name, accounts.on_budget, SUM(transactions.amount)::decimal(12,2) as balance
|
||||
FROM accounts
|
||||
LEFT JOIN transactions ON transactions.account_id = accounts.id
|
||||
WHERE accounts.budget_id = $1
|
||||
|
@ -6,23 +6,31 @@
|
||||
<li>
|
||||
On-Budget Accounts
|
||||
<ul class="two-valued">
|
||||
{{range .Accounts}}
|
||||
{{range .OnBudgetAccounts}}
|
||||
<li>
|
||||
<a href="/budget/{{$.Budget.ID}}/account/{{.ID}}">{{.Name}}</a>
|
||||
{{template "amount" .Balance}}
|
||||
</li>
|
||||
{{end}}
|
||||
<li>
|
||||
<a href="/budget/{{$.Budget.ID}}/accounts">Edit accounts</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
Off-Budget Accounts
|
||||
<ul class="two-valued">
|
||||
{{range .OffBudgetAccounts}}
|
||||
<li>
|
||||
<a href="/budget/{{$.Budget.ID}}/account/{{.ID}}">{{.Name}}</a>
|
||||
{{template "amount" .Balance}}
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
Closed Accounts
|
||||
</li>
|
||||
<li>
|
||||
<a href="/budget/{{$.Budget.ID}}/accounts">Edit accounts</a>
|
||||
</li>
|
||||
<li>
|
||||
+ Add Account
|
||||
</li>
|
||||
|
Loading…
x
Reference in New Issue
Block a user