diff --git a/http/account.go b/http/account.go index 77919de..ecfc409 100644 --- a/http/account.go +++ b/http/account.go @@ -11,10 +11,13 @@ import ( type AccountData struct { AlwaysNeededData Account *postgres.Account + Categories []postgres.GetCategoriesRow Transactions []postgres.GetTransactionsForAccountRow } func (h *Handler) account(c *gin.Context) { + data := c.MustGet("data").(AlwaysNeededData) + accountID := c.Param("accountid") accountUUID, err := uuid.Parse(accountID) if err != nil { @@ -28,6 +31,12 @@ func (h *Handler) account(c *gin.Context) { return } + categories, err := h.Service.GetCategories(c.Request.Context(), data.Budget.ID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + transactions, err := h.Service.GetTransactionsForAccount(c.Request.Context(), accountUUID) if err != nil { c.AbortWithError(http.StatusNotFound, err) @@ -35,8 +44,9 @@ func (h *Handler) account(c *gin.Context) { } d := AccountData{ - c.MustGet("data").(AlwaysNeededData), + data, &account, + categories, transactions, } diff --git a/http/budget.go b/http/budget.go index 3b8b190..39c38b6 100644 --- a/http/budget.go +++ b/http/budget.go @@ -12,6 +12,7 @@ import ( type AllAccountsData struct { AlwaysNeededData Account *postgres.Account + Categories []postgres.GetCategoriesRow Transactions []postgres.GetTransactionsForBudgetRow } @@ -23,6 +24,12 @@ func (h *Handler) allAccounts(c *gin.Context) { return } + categories, err := h.Service.GetCategories(c.Request.Context(), budgetUUID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + transactions, err := h.Service.GetTransactionsForBudget(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) @@ -34,6 +41,7 @@ func (h *Handler) allAccounts(c *gin.Context) { &postgres.Account{ Name: "All accounts", }, + categories, transactions, } diff --git a/http/transaction.go b/http/transaction.go index 8244842..d57266f 100644 --- a/http/transaction.go +++ b/http/transaction.go @@ -10,17 +10,48 @@ import ( "github.com/google/uuid" ) +func getUUID(c *gin.Context, name string) (uuid.UUID, error) { + value, succ := c.GetPostForm(name) + if !succ { + return uuid.UUID{}, fmt.Errorf("not set") + } + + id, err := uuid.Parse(value) + if err != nil { + return uuid.UUID{}, fmt.Errorf("not a valid uuid: %w", err) + } + + return id, nil +} + +func getNullUUID(c *gin.Context, name string) (uuid.NullUUID, error) { + value, succ := c.GetPostForm(name) + if !succ { + return uuid.NullUUID{}, nil + } + + id, err := uuid.Parse(value) + if err != nil { + return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err) + } + + return uuid.NullUUID{ + UUID: id, + Valid: true, + }, nil +} + func (h *Handler) newTransaction(c *gin.Context) { transactionMemo, _ := c.GetPostForm("memo") - transactionAccount, succ := c.GetPostForm("account_id") - if !succ { - c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id missing")) + transactionAccountID, err := getUUID(c, "account_id") + if err != nil { + c.AbortWithError(http.StatusNotAcceptable, err) return } - transactionAccountID, err := uuid.Parse(transactionAccount) - if !succ { - c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id is not a valid uuid")) + transactionCategoryID, err := getNullUUID(c, "category_id") + if err != nil { + c.AbortWithError(http.StatusNotAcceptable, err) return } @@ -50,7 +81,7 @@ func (h *Handler) newTransaction(c *gin.Context) { Amount: amount, AccountID: transactionAccountID, PayeeID: uuid.NullUUID{}, - CategoryID: uuid.NullUUID{}, + CategoryID: transactionCategoryID, } _, err = h.Service.CreateTransaction(c.Request.Context(), new) if err != nil { diff --git a/web/budget-new.tpl b/web/budget-new.tpl index 50d3c6a..245c11f 100644 --- a/web/budget-new.tpl +++ b/web/budget-new.tpl @@ -14,7 +14,7 @@ diff --git a/web/transaction-new.tpl b/web/transaction-new.tpl index 30fd033..41a9a38 100644 --- a/web/transaction-new.tpl +++ b/web/transaction-new.tpl @@ -14,13 +14,22 @@