Add accounts

This commit is contained in:
Jan Bader 2021-12-02 10:52:58 +00:00
parent e465b961a5
commit f760f9b855
6 changed files with 126 additions and 0 deletions

43
http/accounts.go Normal file
View File

@ -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)
}

View File

@ -51,6 +51,7 @@ func (h *Handler) Serve() {
authenticatedFrontend.Use(h.verifyLoginWithRedirect) authenticatedFrontend.Use(h.verifyLoginWithRedirect)
authenticatedFrontend.GET("/dashboard", h.dashboard) authenticatedFrontend.GET("/dashboard", h.dashboard)
authenticatedFrontend.GET("/budget/:budgetid", h.budget) authenticatedFrontend.GET("/budget/:budgetid", h.budget)
authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts)
} }
api := router.Group("/api/v1") api := router.Group("/api/v1")
{ {

49
postgres/accounts.sql.go Normal file
View File

@ -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
}

View File

@ -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;

23
web/accounts.html Normal file
View File

@ -0,0 +1,23 @@
{{define "accounts"}}
<!DOCTYPE html>
<html>
<head>
<title>Budgets</title>
{{template "head"}}
</head>
<body>
<div class="container" id="head">
Budgeteer - {{.Token.GetName}}
</div>
<div class="container col-lg-12" id="content">
{{range .Accounts}}
<div class="budget-item">
<a href="account/{{.ID}}">{{.Name}}</a>
<span class="time"></span>
</div>
{{end}}
</div>
</body>
</html>
{{end}}

View File

@ -10,6 +10,7 @@
<div class="container" id="head"> <div class="container" id="head">
Budgeteer - {{.Token.GetName}} - {{.Budget.Name}} Budgeteer - {{.Token.GetName}} - {{.Budget.Name}}
</div> </div>
<div class="container"><a href="/budget/{{.Budget.ID}}/accounts">Go to Accounts</a></div>
<div class="budget-item"> <div class="budget-item">
<a href="#newbudgetmodal" data-toggle="modal" data-target="#newbudgetmodal">New Budget</a> <a href="#newbudgetmodal" data-toggle="modal" data-target="#newbudgetmodal">New Budget</a>
<span class="time"></span> <span class="time"></span>