From f760f9b85537083a1b174d62824ed3a74b9599e0 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 2 Dec 2021 10:52:58 +0000 Subject: [PATCH] Add accounts --- http/accounts.go | 43 ++++++++++++++++++++++++++++++ http/http.go | 1 + postgres/accounts.sql.go | 49 +++++++++++++++++++++++++++++++++++ postgres/queries/accounts.sql | 9 +++++++ web/accounts.html | 23 ++++++++++++++++ web/budget.html | 1 + 6 files changed, 126 insertions(+) create mode 100644 http/accounts.go create mode 100644 postgres/accounts.sql.go create mode 100644 postgres/queries/accounts.sql create mode 100644 web/accounts.html diff --git a/http/accounts.go b/http/accounts.go new file mode 100644 index 0000000..c823a93 --- /dev/null +++ b/http/accounts.go @@ -0,0 +1,43 @@ +package http + +import ( + "net/http" + + "git.javil.eu/jacob1123/budgeteer" + "git.javil.eu/jacob1123/budgeteer/postgres" + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +type AccountData struct { + Token budgeteer.Token + Accounts []postgres.Account +} + +func (h *Handler) accounts(c *gin.Context) { + token, err := h.verifyLogin(c) + if err != nil { + c.Redirect(http.StatusTemporaryRedirect, "/login") + return + } + + budgetID := c.Param("budgetid") + budgetUUID, err := uuid.Parse(budgetID) + if err != nil { + c.Redirect(http.StatusTemporaryRedirect, "/login") + return + } + + accounts, err := h.Service.DB.GetAccounts(c.Request.Context(), budgetUUID) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + d := AccountData{ + Token: token, + Accounts: accounts, + } + + c.HTML(http.StatusOK, "accounts", d) +} diff --git a/http/http.go b/http/http.go index dbe8fa3..4411b53 100644 --- a/http/http.go +++ b/http/http.go @@ -51,6 +51,7 @@ func (h *Handler) Serve() { authenticatedFrontend.Use(h.verifyLoginWithRedirect) authenticatedFrontend.GET("/dashboard", h.dashboard) authenticatedFrontend.GET("/budget/:budgetid", h.budget) + authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts) } api := router.Group("/api/v1") { diff --git a/postgres/accounts.sql.go b/postgres/accounts.sql.go new file mode 100644 index 0000000..305ab12 --- /dev/null +++ b/postgres/accounts.sql.go @@ -0,0 +1,49 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: accounts.sql + +package postgres + +import ( + "context" + + "github.com/google/uuid" +) + +const createAccount = `-- name: CreateAccount :one +INSERT INTO accounts +(name) +VALUES ($1) +RETURNING id, budget_id, name +` + +func (q *Queries) CreateAccount(ctx context.Context, name string) (Account, error) { + row := q.db.QueryRow(ctx, createAccount, name) + 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 +` + +func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Account, error) { + rows, err := q.db.Query(ctx, getAccounts, budgetID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Account + for rows.Next() { + var i Account + if err := rows.Scan(&i.ID, &i.BudgetID, &i.Name); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/postgres/queries/accounts.sql b/postgres/queries/accounts.sql new file mode 100644 index 0000000..22784a5 --- /dev/null +++ b/postgres/queries/accounts.sql @@ -0,0 +1,9 @@ +-- name: CreateAccount :one +INSERT INTO accounts +(name) +VALUES ($1) +RETURNING *; + +-- name: GetAccounts :many +SELECT accounts.* FROM accounts +WHERE accounts.budget_id = $1; \ No newline at end of file diff --git a/web/accounts.html b/web/accounts.html new file mode 100644 index 0000000..f55dc1e --- /dev/null +++ b/web/accounts.html @@ -0,0 +1,23 @@ +{{define "accounts"}} + + + + Budgets + + {{template "head"}} + + + +
+ {{range .Accounts}} +
+ {{.Name}} + +
+ {{end}} +
+ + +{{end}} \ No newline at end of file diff --git a/web/budget.html b/web/budget.html index 6048db8..1aaa45b 100644 --- a/web/budget.html +++ b/web/budget.html @@ -10,6 +10,7 @@ +
Go to Accounts
New Budget