Implement single account transaction-list

This commit is contained in:
2021-12-02 21:44:35 +00:00
parent f2e8721aa8
commit 871b11bbcc
7 changed files with 56 additions and 48 deletions

View File

@ -8,7 +8,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
type AccountData struct { type AccountsData struct {
Accounts []postgres.GetAccountsWithBalanceRow Accounts []postgres.GetAccountsWithBalanceRow
} }
@ -26,9 +26,42 @@ func (h *Handler) accounts(c *gin.Context) {
return return
} }
d := AccountData{ d := AccountsData{
Accounts: accounts, Accounts: accounts,
} }
c.HTML(http.StatusOK, "accounts.html", d) 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)
}

View File

@ -52,6 +52,7 @@ func (h *Handler) Serve() {
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) authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts)
authenticatedFrontend.GET("/account/:accountid", h.budget)
authenticatedFrontend.GET("/admin", h.admin) authenticatedFrontend.GET("/admin", h.admin)
authenticatedFrontend.GET("/admin/clear-database", h.clearDatabase) authenticatedFrontend.GET("/admin/clear-database", h.clearDatabase)
} }

View File

@ -28,6 +28,18 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A
return i, err 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 const getAccounts = `-- name: GetAccounts :many
SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts
WHERE accounts.budget_id = $1 WHERE accounts.budget_id = $1

View File

@ -27,45 +27,3 @@ func Connect(server string, user string, password string, database string) (*Que
return New(conn), conn, nil 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
}

View File

@ -4,6 +4,10 @@ INSERT INTO accounts
VALUES ($1, $2) VALUES ($1, $2)
RETURNING *; RETURNING *;
-- name: GetAccount :one
SELECT accounts.* FROM accounts
WHERE accounts.id = $1;
-- name: GetAccounts :many -- name: GetAccounts :many
SELECT accounts.* FROM accounts SELECT accounts.* FROM accounts
WHERE accounts.budget_id = $1; WHERE accounts.budget_id = $1;

View File

@ -10,8 +10,8 @@
{{define "main"}} {{define "main"}}
{{range .Accounts}} {{range .Accounts}}
<div class="budget-item"> <div class="budget-item">
<a href="account/{{.ID}}">{{.Name}}</a> <a href="/account/{{.ID}}">{{.Name}}</a>
<span class="time">{{printf "%.2f" .GetBalance}}</span> <span class="time">{{printf "%.2f" .Balance.GetFloat64}}</span>
</div> </div>
{{end}} {{end}}
{{end}} {{end}}

View File

@ -25,8 +25,8 @@
<td> <td>
<a href="transaction/{{.ID}}">{{.Memo}}</a> <a href="transaction/{{.ID}}">{{.Memo}}</a>
</td> </td>
<td style="text-align: right;{{if .GetPositive}}{{else}}color: red;{{end}}"> <td style="text-align: right;{{if .Amount.GetPositive}}{{else}}color: red;{{end}}">
{{printf "%.2f" .GetAmount}} {{printf "%.2f" .Amount.GetFloat64}}
</td> </td>
</tr> </tr>
{{end}} {{end}}