Implement categories for new transactions
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
|
@@ -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,
|
||||
}
|
||||
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user