diff --git a/http/admin.go b/http/admin.go index 852c97c..bef6cc9 100644 --- a/http/admin.go +++ b/http/admin.go @@ -19,14 +19,20 @@ func (h *Handler) clearDatabase(c *gin.Context) { } } -func (h *Handler) clearBudget(c *gin.Context) { +func (h *Handler) deleteBudget(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") + c.AbortWithStatus(http.StatusBadRequest) return } + h.clearBudgetData(c, budgetUUID) + + h.Service.DeleteBudget(c.Request.Context(), budgetUUID) +} + +func (h *Handler) clearBudgetData(c *gin.Context, budgetUUID uuid.UUID) { rows, err := h.Service.DeleteAllAssignments(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) @@ -44,6 +50,17 @@ func (h *Handler) clearBudget(c *gin.Context) { fmt.Printf("Deleted %d transactions\n", rows) } +func (h *Handler) clearBudget(c *gin.Context) { + budgetID := c.Param("budgetid") + budgetUUID, err := uuid.Parse(budgetID) + if err != nil { + c.AbortWithStatus(http.StatusBadRequest) + return + } + + h.clearBudgetData(c, budgetUUID) +} + func (h *Handler) cleanNegativeBudget(c *gin.Context) { /*budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) diff --git a/http/http.go b/http/http.go index 105a23b..97257df 100644 --- a/http/http.go +++ b/http/http.go @@ -66,6 +66,7 @@ func (h *Handler) Serve() { authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount) authenticated.GET("/admin/clear-database", h.clearDatabase) authenticated.GET("/budget/:budgetid", h.budgeting) + authenticated.DELETE("/budget/:budgetid", h.deleteBudget) authenticated.POST("/budget/:budgetid/import/ynab", h.importYNAB) authenticated.POST("/budget/:budgetid/settings/clear", h.clearBudget) diff --git a/postgres/budgets.sql.go b/postgres/budgets.sql.go index 9ee44cf..780c998 100644 --- a/postgres/budgets.sql.go +++ b/postgres/budgets.sql.go @@ -34,6 +34,15 @@ func (q *Queries) CreateBudget(ctx context.Context, arg CreateBudgetParams) (Bud return i, err } +const deleteBudget = `-- name: DeleteBudget :exec +DELETE FROM budgets WHERE id = $1 +` + +func (q *Queries) DeleteBudget(ctx context.Context, id uuid.UUID) error { + _, err := q.db.ExecContext(ctx, deleteBudget, id) + return err +} + const getBudget = `-- name: GetBudget :one SELECT id, name, last_modification, income_category_id FROM budgets WHERE id = $1 diff --git a/postgres/queries/budgets.sql b/postgres/queries/budgets.sql index 5e74bc6..c7661fd 100644 --- a/postgres/queries/budgets.sql +++ b/postgres/queries/budgets.sql @@ -31,4 +31,7 @@ FROM ( FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id WHERE accounts.budget_id = @budget_id -) dates; \ No newline at end of file +) dates; + +-- name: DeleteBudget :exec +DELETE FROM budgets WHERE id = $1; \ No newline at end of file diff --git a/web/src/pages/Settings.vue b/web/src/pages/Settings.vue index 1890e10..83a369f 100644 --- a/web/src/pages/Settings.vue +++ b/web/src/pages/Settings.vue @@ -23,6 +23,14 @@ export default { gotTransactions(e) { this.$data.transactionsFile = e.target.files[0]; }, + deleteBudget() { + fetch("/api/v1/budget/" + this.$store.getters.CurrentBudget.ID, { + method: "DELETE", + headers: { + 'Authorization': 'Bearer ' + this.$store.state.Session.Token + }, + }) + }, clearBudget() { fetch("/api/v1/budget/" + this.$store.getters.CurrentBudget.ID + "/settings/clear", { method: "POST", @@ -65,6 +73,23 @@ export default { + + + + + + Delete Budget + + + This deletes the whole bugdet including all transactions, assignments, accounts and categories. Not undoable! + + + + + Delete budget + + +