From 8f374f1d62ea97def31d7bfa24cbe451bfe97a85 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 7 Dec 2021 13:02:39 +0000 Subject: [PATCH] Try to display simple budget screen --- http/http.go | 3 +- postgres/categories.sql.go | 54 ++++++++++++++++++++++++ postgres/queries/categories.sql | 11 ++++- postgres/schema/202112021109_initial.sql | 24 ++++------- web/budget-sidebar.tpl | 7 ++- 5 files changed, 80 insertions(+), 19 deletions(-) diff --git a/http/http.go b/http/http.go index 0e41296..b71f69b 100644 --- a/http/http.go +++ b/http/http.go @@ -56,7 +56,8 @@ func (h *Handler) Serve() { withBudget := router.Group("") withBudget.Use(h.verifyLoginWithRedirect) 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/account/:accountid", h.account) diff --git a/postgres/categories.sql.go b/postgres/categories.sql.go index 27e9908..c6642c7 100644 --- a/postgres/categories.sql.go +++ b/postgres/categories.sql.go @@ -5,6 +5,7 @@ package postgres import ( "context" + "time" "github.com/google/uuid" ) @@ -88,6 +89,59 @@ func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetC 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 SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups WHERE category_groups.budget_id = $1 diff --git a/postgres/queries/categories.sql b/postgres/queries/categories.sql index 4ee4e58..f9e5511 100644 --- a/postgres/queries/categories.sql +++ b/postgres/queries/categories.sql @@ -17,4 +17,13 @@ RETURNING *; -- name: GetCategories :many SELECT categories.*, category_groups.name as group FROM categories INNER JOIN category_groups ON categories.category_group_id = category_groups.id -WHERE category_groups.budget_id = $1; \ No newline at end of file +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; \ No newline at end of file diff --git a/postgres/schema/202112021109_initial.sql b/postgres/schema/202112021109_initial.sql index baea6ff..9448d2a 100644 --- a/postgres/schema/202112021109_initial.sql +++ b/postgres/schema/202112021109_initial.sql @@ -14,48 +14,42 @@ CREATE TABLE users ( ); CREATE TABLE user_budgets ( - user_id uuid NOT NULL, - budget_id uuid NOT NULL + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + 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 ( 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 ); -ALTER TABLE "accounts" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); CREATE TABLE payees ( 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 ); -ALTER TABLE "payees" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); CREATE TABLE category_groups ( 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 ); -ALTER TABLE "category_groups" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); CREATE TABLE categories ( 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 ); -ALTER TABLE "categories" ADD FOREIGN KEY ("category_group_id") REFERENCES "category_groups" ("id"); CREATE TABLE transactions ( id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, date date NOT NULL, memo text NOT NULL, amount decimal(12,2) NOT NULL, - account_id uuid NOT NULL, - category_id uuid, - payee_id uuid + account_id uuid NOT NULL REFERENCES accounts (id), + category_id uuid REFERENCES categories (id), + payee_id uuid REFERENCES payees (id) ); ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id"); diff --git a/web/budget-sidebar.tpl b/web/budget-sidebar.tpl index 09475d0..e8e1c86 100644 --- a/web/budget-sidebar.tpl +++ b/web/budget-sidebar.tpl @@ -1,8 +1,8 @@ {{define "budget-sidebar"}}