diff --git a/postgres/accounts.sql.go b/postgres/accounts.sql.go index 20d30df..fa5ee92 100644 --- a/postgres/accounts.sql.go +++ b/postgres/accounts.sql.go @@ -175,3 +175,29 @@ func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams) } return items, nil } + +const updateAccount = `-- name: UpdateAccount :one +UPDATE accounts +SET name = $1, + on_budget = $2 +WHERE accounts.id = $3 +RETURNING id, budget_id, name, on_budget +` + +type UpdateAccountParams struct { + Name string + OnBudget bool + ID uuid.UUID +} + +func (q *Queries) UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error) { + row := q.db.QueryRowContext(ctx, updateAccount, arg.Name, arg.OnBudget, arg.ID) + var i Account + err := row.Scan( + &i.ID, + &i.BudgetID, + &i.Name, + &i.OnBudget, + ) + return i, err +} diff --git a/postgres/queries/accounts.sql b/postgres/queries/accounts.sql index 62173d9..97e581b 100644 --- a/postgres/queries/accounts.sql +++ b/postgres/queries/accounts.sql @@ -25,4 +25,11 @@ ORDER BY accounts.name; SELECT accounts.id, accounts.budget_id, accounts.name, true as is_account FROM accounts WHERE accounts.budget_id = @budget_id AND accounts.name LIKE @search -ORDER BY accounts.name; \ No newline at end of file +ORDER BY accounts.name; + +-- name: UpdateAccount :one +UPDATE accounts +SET name = $1, + on_budget = $2 +WHERE accounts.id = $3 +RETURNING *; \ No newline at end of file diff --git a/server/account.go b/server/account.go index 18cfe41..59eab6a 100644 --- a/server/account.go +++ b/server/account.go @@ -35,3 +35,37 @@ type TransactionsResponse struct { Account postgres.Account Transactions []postgres.GetTransactionsForAccountRow } + +type EditAccountRequest struct { + Name string `json:"name"` + OnBudget bool `json:"onBudget"` +} + +func (h *Handler) editAccount(c *gin.Context) { + accountID := c.Param("accountid") + accountUUID, err := uuid.Parse(accountID) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + var request EditAccountRequest + err = c.BindJSON(&request) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + updateParams := postgres.UpdateAccountParams{ + Name: request.Name, + OnBudget: request.OnBudget, + ID: accountUUID, + } + account, err := h.Service.UpdateAccount(c.Request.Context(), updateParams) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + + h.returnBudgetingData(c, account.BudgetID) +} diff --git a/server/budgeting.go b/server/budgeting.go index b571d9c..a421dbc 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -132,6 +132,11 @@ func (*Handler) getAvailableBalance(categories []postgres.GetCategoriesRow, budg return availableBalance } +type BudgetingResponse struct { + Accounts []postgres.GetAccountsWithBalanceRow + Budget postgres.Budget +} + func (h *Handler) budgeting(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) @@ -140,6 +145,10 @@ func (h *Handler) budgeting(c *gin.Context) { return } + h.returnBudgetingData(c, budgetUUID) +} + +func (h *Handler) returnBudgetingData(c *gin.Context, budgetUUID uuid.UUID) { budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusNotFound, err) @@ -152,10 +161,7 @@ func (h *Handler) budgeting(c *gin.Context) { return } - data := struct { - Accounts []postgres.GetAccountsWithBalanceRow - Budget postgres.Budget - }{accounts, budget} + data := BudgetingResponse{accounts, budget} c.JSON(http.StatusOK, data) } diff --git a/server/http.go b/server/http.go index a80f55c..74e1fad 100644 --- a/server/http.go +++ b/server/http.go @@ -60,6 +60,7 @@ func (h *Handler) LoadRoutes(router *gin.Engine) { authenticated.Use(h.verifyLoginWithForbidden) authenticated.GET("/dashboard", h.dashboard) authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount) + authenticated.POST("/account/:accountid", h.editAccount) authenticated.GET("/admin/clear-database", h.clearDatabase) authenticated.GET("/budget/:budgetid", h.budgeting) authenticated.GET("/budget/:budgetid/:year/:month", h.budgetingForMonth)