Implement EditAccount in Backend
This commit is contained in:
parent
03d1d1e520
commit
f51807e459
@ -175,3 +175,29 @@ func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams)
|
|||||||
}
|
}
|
||||||
return items, nil
|
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
|
||||||
|
}
|
||||||
|
@ -25,4 +25,11 @@ ORDER BY accounts.name;
|
|||||||
SELECT accounts.id, accounts.budget_id, accounts.name, true as is_account FROM accounts
|
SELECT accounts.id, accounts.budget_id, accounts.name, true as is_account FROM accounts
|
||||||
WHERE accounts.budget_id = @budget_id
|
WHERE accounts.budget_id = @budget_id
|
||||||
AND accounts.name LIKE @search
|
AND accounts.name LIKE @search
|
||||||
ORDER BY accounts.name;
|
ORDER BY accounts.name;
|
||||||
|
|
||||||
|
-- name: UpdateAccount :one
|
||||||
|
UPDATE accounts
|
||||||
|
SET name = $1,
|
||||||
|
on_budget = $2
|
||||||
|
WHERE accounts.id = $3
|
||||||
|
RETURNING *;
|
@ -35,3 +35,37 @@ type TransactionsResponse struct {
|
|||||||
Account postgres.Account
|
Account postgres.Account
|
||||||
Transactions []postgres.GetTransactionsForAccountRow
|
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)
|
||||||
|
}
|
||||||
|
@ -132,6 +132,11 @@ func (*Handler) getAvailableBalance(categories []postgres.GetCategoriesRow, budg
|
|||||||
return availableBalance
|
return availableBalance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BudgetingResponse struct {
|
||||||
|
Accounts []postgres.GetAccountsWithBalanceRow
|
||||||
|
Budget postgres.Budget
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) budgeting(c *gin.Context) {
|
func (h *Handler) budgeting(c *gin.Context) {
|
||||||
budgetID := c.Param("budgetid")
|
budgetID := c.Param("budgetid")
|
||||||
budgetUUID, err := uuid.Parse(budgetID)
|
budgetUUID, err := uuid.Parse(budgetID)
|
||||||
@ -140,6 +145,10 @@ func (h *Handler) budgeting(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.returnBudgetingData(c, budgetUUID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) returnBudgetingData(c *gin.Context, budgetUUID uuid.UUID) {
|
||||||
budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID)
|
budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusNotFound, err)
|
c.AbortWithError(http.StatusNotFound, err)
|
||||||
@ -152,10 +161,7 @@ func (h *Handler) budgeting(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := struct {
|
data := BudgetingResponse{accounts, budget}
|
||||||
Accounts []postgres.GetAccountsWithBalanceRow
|
|
||||||
Budget postgres.Budget
|
|
||||||
}{accounts, budget}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, data)
|
c.JSON(http.StatusOK, data)
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
|||||||
authenticated.Use(h.verifyLoginWithForbidden)
|
authenticated.Use(h.verifyLoginWithForbidden)
|
||||||
authenticated.GET("/dashboard", h.dashboard)
|
authenticated.GET("/dashboard", h.dashboard)
|
||||||
authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount)
|
authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount)
|
||||||
|
authenticated.POST("/account/:accountid", h.editAccount)
|
||||||
authenticated.GET("/admin/clear-database", h.clearDatabase)
|
authenticated.GET("/admin/clear-database", h.clearDatabase)
|
||||||
authenticated.GET("/budget/:budgetid", h.budgeting)
|
authenticated.GET("/budget/:budgetid", h.budgeting)
|
||||||
authenticated.GET("/budget/:budgetid/:year/:month", h.budgetingForMonth)
|
authenticated.GET("/budget/:budgetid/:year/:month", h.budgetingForMonth)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user