From c56335adad4d9f09f4bc1254de10e1502e585bcb Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 2 Dec 2021 21:49:14 +0000 Subject: [PATCH] Finalize account view --- http/accounts.go | 2 +- http/http.go | 2 +- postgres/queries/transactions.sql | 6 +++++- postgres/transactions.sql.go | 25 +++++++++++++++++------ web/account.html | 33 +++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 web/account.html diff --git a/http/accounts.go b/http/accounts.go index 58e0493..a0aa701 100644 --- a/http/accounts.go +++ b/http/accounts.go @@ -63,5 +63,5 @@ func (h *Handler) account(c *gin.Context) { Transactions: transactions, } - c.HTML(http.StatusOK, "budget.html", d) + c.HTML(http.StatusOK, "account.html", d) } diff --git a/http/http.go b/http/http.go index ad774b1..506305f 100644 --- a/http/http.go +++ b/http/http.go @@ -52,7 +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("/account/:accountid", h.account) authenticatedFrontend.GET("/admin", h.admin) authenticatedFrontend.GET("/admin/clear-database", h.clearDatabase) } diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index abefa78..adbe754 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -15,7 +15,11 @@ ORDER BY transactions.date DESC LIMIT 200; -- name: GetTransactionsForAccount :many -SELECT transactions.* FROM transactions +SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, + accounts.name as account, payees.name as payee +FROM transactions +INNER JOIN accounts ON accounts.id = transactions.account_id +INNER JOIN payees ON payees.id = transactions.payee_id WHERE transactions.account_id = $1 ORDER BY transactions.date DESC LIMIT 200; \ No newline at end of file diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index 353ba0a..8360017 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -46,28 +46,41 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa } const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many -SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions +SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, + accounts.name as account, payees.name as payee +FROM transactions +INNER JOIN accounts ON accounts.id = transactions.account_id +INNER JOIN payees ON payees.id = transactions.payee_id WHERE transactions.account_id = $1 ORDER BY transactions.date DESC LIMIT 200 ` -func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]Transaction, error) { +type GetTransactionsForAccountRow struct { + ID uuid.UUID + Date time.Time + Memo string + Amount postgres.Numeric + Account string + Payee string +} + +func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]GetTransactionsForAccountRow, error) { rows, err := q.db.QueryContext(ctx, getTransactionsForAccount, accountID) if err != nil { return nil, err } defer rows.Close() - var items []Transaction + var items []GetTransactionsForAccountRow for rows.Next() { - var i Transaction + var i GetTransactionsForAccountRow if err := rows.Scan( &i.ID, &i.Date, &i.Memo, &i.Amount, - &i.AccountID, - &i.PayeeID, + &i.Account, + &i.Payee, ); err != nil { return nil, err } diff --git a/web/account.html b/web/account.html new file mode 100644 index 0000000..5e7528e --- /dev/null +++ b/web/account.html @@ -0,0 +1,33 @@ +{{template "base" .}} + +{{define "title"}}{{.Account.Name}}{{end}} + +{{define "new"}} + {{template "transaction-new"}} +{{end}} + +{{define "main"}} +
+ New Transaction + +
+ + {{range .Transactions}} + + + + + + + + {{end}} +
{{.Date}} + {{.Account}} + + {{.Payee}} + + {{.Memo}} + + {{printf "%.2f" .Amount.GetFloat64}} +
+{{end}} \ No newline at end of file