From d09f5be69b36aeac95e9d5dc0cd4297c7c1bad23 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 24 Feb 2022 21:37:26 +0000 Subject: [PATCH 1/5] Extract modal component --- web/src/components/Button.vue | 8 +++-- web/src/components/Modal.vue | 54 +++++++++++++++++++++++++++++++++ web/src/dialogs/NewBudget.vue | 56 ++++------------------------------- 3 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 web/src/components/Modal.vue diff --git a/web/src/components/Button.vue b/web/src/components/Button.vue index ff7554f..c7c16a0 100644 --- a/web/src/components/Button.vue +++ b/web/src/components/Button.vue @@ -2,7 +2,9 @@ \ No newline at end of file diff --git a/web/src/components/Modal.vue b/web/src/components/Modal.vue new file mode 100644 index 0000000..9dc851b --- /dev/null +++ b/web/src/components/Modal.vue @@ -0,0 +1,54 @@ + + + \ No newline at end of file diff --git a/web/src/dialogs/NewBudget.vue b/web/src/dialogs/NewBudget.vue index 75e095e..9d23508 100644 --- a/web/src/dialogs/NewBudget.vue +++ b/web/src/dialogs/NewBudget.vue @@ -1,64 +1,18 @@ \ No newline at end of file From 03d1d1e5200d9b48a9277b5cd15bc2c6ce96aa5a Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 24 Feb 2022 21:38:57 +0000 Subject: [PATCH 2/5] Use grid instead of width for buttons --- web/src/components/Modal.vue | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/web/src/components/Modal.vue b/web/src/components/Modal.vue index 9dc851b..b783232 100644 --- a/web/src/components/Modal.vue +++ b/web/src/components/Modal.vue @@ -36,17 +36,15 @@ function submitDialog() {

{{ buttonText }}

-
- - - - +
+ +
From f51807e4599a6ba5e8dd9bdc84a9107d760b1d07 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 24 Feb 2022 22:12:10 +0000 Subject: [PATCH 3/5] Implement EditAccount in Backend --- postgres/accounts.sql.go | 26 ++++++++++++++++++++++++++ postgres/queries/accounts.sql | 9 ++++++++- server/account.go | 34 ++++++++++++++++++++++++++++++++++ server/budgeting.go | 14 ++++++++++---- server/http.go | 1 + 5 files changed, 79 insertions(+), 5 deletions(-) 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) From 466df523ab81098afc853517de16de778208be30 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 24 Feb 2022 22:12:26 +0000 Subject: [PATCH 4/5] Implement EditAccount in Frontend --- web/src/components/Modal.vue | 16 +++++++++----- web/src/pages/Account.vue | 41 +++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/web/src/components/Modal.vue b/web/src/components/Modal.vue index b783232..7452479 100644 --- a/web/src/components/Modal.vue +++ b/web/src/components/Modal.vue @@ -7,7 +7,8 @@ const props = defineProps<{ }>(); const emit = defineEmits<{ - (e: 'submit'): void + (e: 'submit'): void, + (e: 'open'): void, }>(); const visible = ref(false); @@ -15,6 +16,7 @@ function closeDialog() { visible.value = false; }; function openDialog() { + emit("open"); visible.value = true; }; function submitDialog() { @@ -24,10 +26,14 @@ function submitDialog() {