Try to display simple budget screen

This commit is contained in:
Jan Bader 2021-12-07 13:02:39 +00:00
parent 494c1431fe
commit 8f374f1d62
5 changed files with 80 additions and 19 deletions

View File

@ -56,7 +56,8 @@ func (h *Handler) Serve() {
withBudget := router.Group("") withBudget := router.Group("")
withBudget.Use(h.verifyLoginWithRedirect) withBudget.Use(h.verifyLoginWithRedirect)
withBudget.Use(h.getImportantData) withBudget.Use(h.getImportantData)
withBudget.GET("/budget/:budgetid", h.budget) withBudget.GET("/budget/:budgetid", h.budgeting)
withBudget.GET("/budget/:budgetid/all-accounts", h.budget)
withBudget.GET("/budget/:budgetid/accounts", h.accounts) withBudget.GET("/budget/:budgetid/accounts", h.accounts)
withBudget.GET("/budget/:budgetid/account/:accountid", h.account) withBudget.GET("/budget/:budgetid/account/:accountid", h.account)

View File

@ -5,6 +5,7 @@ package postgres
import ( import (
"context" "context"
"time"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -88,6 +89,59 @@ func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetC
return items, nil return items, nil
} }
const getCategoriesWithBalance = `-- name: GetCategoriesWithBalance :many
SELECT categories.id, categories.name, category_groups.name as group, SUM(t_hist.amount)::decimal(12,2) as balance, SUM(t_this.amount)::decimal(12,2) as activity
FROM categories
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
INNER JOIN transactions t_hist ON categories.id = t_hist.category_id AND t_hist.date < $1
INNER JOIN transactions t_this ON categories.id = t_this.category_id AND t_this.date >= $1 AND t_this.date < $2
WHERE category_groups.budget_id = $3
GROUP BY categories.id, categories.name, category_groups.name
`
type GetCategoriesWithBalanceParams struct {
FromDate time.Time
ToDate time.Time
BudgetID uuid.UUID
}
type GetCategoriesWithBalanceRow struct {
ID uuid.UUID
Name string
Group string
Balance Numeric
Activity Numeric
}
func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) {
rows, err := q.db.QueryContext(ctx, getCategoriesWithBalance, arg.FromDate, arg.ToDate, arg.BudgetID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetCategoriesWithBalanceRow
for rows.Next() {
var i GetCategoriesWithBalanceRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Group,
&i.Balance,
&i.Activity,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCategoryGroups = `-- name: GetCategoryGroups :many const getCategoryGroups = `-- name: GetCategoryGroups :many
SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups
WHERE category_groups.budget_id = $1 WHERE category_groups.budget_id = $1

View File

@ -17,4 +17,13 @@ RETURNING *;
-- name: GetCategories :many -- name: GetCategories :many
SELECT categories.*, category_groups.name as group FROM categories SELECT categories.*, category_groups.name as group FROM categories
INNER JOIN category_groups ON categories.category_group_id = category_groups.id INNER JOIN category_groups ON categories.category_group_id = category_groups.id
WHERE category_groups.budget_id = $1; WHERE category_groups.budget_id = $1;
-- name: GetCategoriesWithBalance :many
SELECT categories.id, categories.name, category_groups.name as group, SUM(t_hist.amount)::decimal(12,2) as balance, SUM(t_this.amount)::decimal(12,2) as activity
FROM categories
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
INNER JOIN transactions t_hist ON categories.id = t_hist.category_id AND t_hist.date < @from_date
INNER JOIN transactions t_this ON categories.id = t_this.category_id AND t_this.date >= @from_date AND t_this.date < @to_date
WHERE category_groups.budget_id = @budget_id
GROUP BY categories.id, categories.name, category_groups.name;

View File

@ -14,48 +14,42 @@ CREATE TABLE users (
); );
CREATE TABLE user_budgets ( CREATE TABLE user_budgets (
user_id uuid NOT NULL, user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
budget_id uuid NOT NULL budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE
); );
ALTER TABLE "user_budgets" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "user_budgets" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");
CREATE TABLE accounts ( CREATE TABLE accounts (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
budget_id uuid NOT NULL, budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE,
name varchar(50) NOT NULL name varchar(50) NOT NULL
); );
ALTER TABLE "accounts" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");
CREATE TABLE payees ( CREATE TABLE payees (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
budget_id uuid NOT NULL, budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE,
name varchar(50) NOT NULL name varchar(50) NOT NULL
); );
ALTER TABLE "payees" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");
CREATE TABLE category_groups ( CREATE TABLE category_groups (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
budget_id uuid NOT NULL, budget_id uuid NOT NULL REFERENCES budgets (id) ON DELETE CASCADE,
name varchar(50) NOT NULL name varchar(50) NOT NULL
); );
ALTER TABLE "category_groups" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");
CREATE TABLE categories ( CREATE TABLE categories (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
category_group_id uuid NOT NULL, category_group_id uuid NOT NULL REFERENCES category_groups (id) ON DELETE CASCADE,
name varchar(50) NOT NULL name varchar(50) NOT NULL
); );
ALTER TABLE "categories" ADD FOREIGN KEY ("category_group_id") REFERENCES "category_groups" ("id");
CREATE TABLE transactions ( CREATE TABLE transactions (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
date date NOT NULL, date date NOT NULL,
memo text NOT NULL, memo text NOT NULL,
amount decimal(12,2) NOT NULL, amount decimal(12,2) NOT NULL,
account_id uuid NOT NULL, account_id uuid NOT NULL REFERENCES accounts (id),
category_id uuid, category_id uuid REFERENCES categories (id),
payee_id uuid payee_id uuid REFERENCES payees (id)
); );
ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id");
ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id"); ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id");

View File

@ -1,8 +1,8 @@
{{define "budget-sidebar"}} {{define "budget-sidebar"}}
<ul> <ul>
<li>Budget (Coming Soon)</li> <li><a href="/budget/{{.Budget.ID}}">Budget</a></li>
<li>Reports (Coming Soon)</li> <li>Reports (Coming Soon)</li>
<li><a href="/budget/{{.Budget.ID}}/accounts">All Accounts</a></li> <li><a href="/budget/{{.Budget.ID}}/all-accounts">All Accounts</a></li>
<li> <li>
On-Budget Accounts On-Budget Accounts
<ul class="two-valued"> <ul class="two-valued">
@ -12,6 +12,9 @@
<span class="right">{{printf "%.2f" .Balance.GetFloat64}}</span> <span class="right">{{printf "%.2f" .Balance.GetFloat64}}</span>
</li> </li>
{{end}} {{end}}
<li>
<a href="/budget/{{$.Budget.ID}}/accounts">Edit accounts</a>
</li>
</ul> </ul>
</li> </li>
<li> <li>