Display accounts sorted by name

This commit is contained in:
Jan Bader 2021-12-06 20:47:47 +00:00
parent 2caaa1a048
commit 29cee46a14
2 changed files with 6 additions and 2 deletions

View File

@ -43,6 +43,7 @@ func (q *Queries) GetAccount(ctx context.Context, id uuid.UUID) (Account, error)
const getAccounts = `-- name: GetAccounts :many const getAccounts = `-- name: GetAccounts :many
SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts
WHERE accounts.budget_id = $1 WHERE accounts.budget_id = $1
ORDER BY accounts.name
` `
func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Account, error) { func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Account, error) {
@ -75,6 +76,7 @@ LEFT JOIN transactions ON transactions.account_id = accounts.id
WHERE accounts.budget_id = $1 WHERE accounts.budget_id = $1
AND transactions.date < NOW() AND transactions.date < NOW()
GROUP BY accounts.id, accounts.name GROUP BY accounts.id, accounts.name
ORDER BY accounts.name
` `
type GetAccountsWithBalanceRow struct { type GetAccountsWithBalanceRow struct {

View File

@ -10,7 +10,8 @@ WHERE accounts.id = $1;
-- name: GetAccounts :many -- name: GetAccounts :many
SELECT accounts.* FROM accounts SELECT accounts.* FROM accounts
WHERE accounts.budget_id = $1; WHERE accounts.budget_id = $1
ORDER BY accounts.name;
-- name: GetAccountsWithBalance :many -- name: GetAccountsWithBalance :many
SELECT accounts.id, accounts.name, SUM(transactions.amount)::decimal(12,2) as balance SELECT accounts.id, accounts.name, SUM(transactions.amount)::decimal(12,2) as balance
@ -18,4 +19,5 @@ FROM accounts
LEFT JOIN transactions ON transactions.account_id = accounts.id LEFT JOIN transactions ON transactions.account_id = accounts.id
WHERE accounts.budget_id = $1 WHERE accounts.budget_id = $1
AND transactions.date < NOW() AND transactions.date < NOW()
GROUP BY accounts.id, accounts.name; GROUP BY accounts.id, accounts.name
ORDER BY accounts.name;