From 871b11bbcc0ed051007b973dd12da57bff3b6af1 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 2 Dec 2021 21:44:35 +0000 Subject: [PATCH] Implement single account transaction-list --- http/accounts.go | 37 ++++++++++++++++++++++++++++-- http/http.go | 1 + postgres/accounts.sql.go | 12 ++++++++++ postgres/conn.go | 42 ----------------------------------- postgres/queries/accounts.sql | 4 ++++ web/accounts.html | 4 ++-- web/budget.html | 4 ++-- 7 files changed, 56 insertions(+), 48 deletions(-) diff --git a/http/accounts.go b/http/accounts.go index 1a77b12..58e0493 100644 --- a/http/accounts.go +++ b/http/accounts.go @@ -8,7 +8,7 @@ import ( "github.com/google/uuid" ) -type AccountData struct { +type AccountsData struct { Accounts []postgres.GetAccountsWithBalanceRow } @@ -26,9 +26,42 @@ func (h *Handler) accounts(c *gin.Context) { return } - d := AccountData{ + d := AccountsData{ Accounts: accounts, } c.HTML(http.StatusOK, "accounts.html", d) } + +type AccountData struct { + Account *postgres.Account + Transactions []postgres.Transaction +} + +func (h *Handler) account(c *gin.Context) { + accountID := c.Param("accountid") + accountUUID, err := uuid.Parse(accountID) + if err != nil { + c.Redirect(http.StatusTemporaryRedirect, "/login") + return + } + + account, err := h.Service.DB.GetAccount(c.Request.Context(), accountUUID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + + transactions, err := h.Service.DB.GetTransactionsForAccount(c.Request.Context(), accountUUID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + + d := AccountData{ + Account: &account, + Transactions: transactions, + } + + c.HTML(http.StatusOK, "budget.html", d) +} diff --git a/http/http.go b/http/http.go index 57f8cb1..ad774b1 100644 --- a/http/http.go +++ b/http/http.go @@ -52,6 +52,7 @@ func (h *Handler) Serve() { authenticatedFrontend.GET("/dashboard", h.dashboard) authenticatedFrontend.GET("/budget/:budgetid", h.budget) authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts) + authenticatedFrontend.GET("/account/:accountid", h.budget) authenticatedFrontend.GET("/admin", h.admin) authenticatedFrontend.GET("/admin/clear-database", h.clearDatabase) } diff --git a/postgres/accounts.sql.go b/postgres/accounts.sql.go index 7751530..d41f426 100644 --- a/postgres/accounts.sql.go +++ b/postgres/accounts.sql.go @@ -28,6 +28,18 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A return i, err } +const getAccount = `-- name: GetAccount :one +SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts +WHERE accounts.id = $1 +` + +func (q *Queries) GetAccount(ctx context.Context, id uuid.UUID) (Account, error) { + row := q.db.QueryRowContext(ctx, getAccount, id) + var i Account + err := row.Scan(&i.ID, &i.BudgetID, &i.Name) + return i, err +} + const getAccounts = `-- name: GetAccounts :many SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts WHERE accounts.budget_id = $1 diff --git a/postgres/conn.go b/postgres/conn.go index 36acf9a..750bffd 100644 --- a/postgres/conn.go +++ b/postgres/conn.go @@ -27,45 +27,3 @@ func Connect(server string, user string, password string, database string) (*Que return New(conn), conn, nil } - -func (tx Transaction) GetAmount() float64 { - var amount float64 - err := tx.Amount.AssignTo(&amount) - if err != nil { - panic(err) - } - return amount -} - -func (tx Transaction) GetPositive() bool { - amount := tx.GetAmount() - 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 -} - -func (tx GetAccountsWithBalanceRow) GetBalance() float64 { - var balance float64 - err := tx.Balance.AssignTo(&balance) - if err != nil { - panic(err) - } - return balance -} - -func (tx GetAccountsWithBalanceRow) GetPositive() bool { - balance := tx.GetBalance() - return balance >= 0 -} diff --git a/postgres/queries/accounts.sql b/postgres/queries/accounts.sql index 788ee42..40f1433 100644 --- a/postgres/queries/accounts.sql +++ b/postgres/queries/accounts.sql @@ -4,6 +4,10 @@ INSERT INTO accounts VALUES ($1, $2) RETURNING *; +-- name: GetAccount :one +SELECT accounts.* FROM accounts +WHERE accounts.id = $1; + -- name: GetAccounts :many SELECT accounts.* FROM accounts WHERE accounts.budget_id = $1; diff --git a/web/accounts.html b/web/accounts.html index 830ec81..2ab7231 100644 --- a/web/accounts.html +++ b/web/accounts.html @@ -10,8 +10,8 @@ {{define "main"}} {{range .Accounts}}
- {{.Name}} - {{printf "%.2f" .GetBalance}} + {{.Name}} + {{printf "%.2f" .Balance.GetFloat64}}
{{end}} {{end}} \ No newline at end of file diff --git a/web/budget.html b/web/budget.html index 79b16ed..ab2bee8 100644 --- a/web/budget.html +++ b/web/budget.html @@ -25,8 +25,8 @@ {{.Memo}} - - {{printf "%.2f" .GetAmount}} + + {{printf "%.2f" .Amount.GetFloat64}} {{end}}