Implement Delete Budget

This commit is contained in:
Jan Bader 2022-01-31 15:50:13 +00:00
parent 1be3b6930d
commit 95b1ac5943
5 changed files with 58 additions and 3 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -32,3 +32,6 @@ FROM (
INNER JOIN accounts ON accounts.id = transactions.account_id
WHERE accounts.budget_id = @budget_id
) dates;
-- name: DeleteBudget :exec
DELETE FROM budgets WHERE id = $1;

View File

@ -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 {
</v-card-actions>
</v-card>
</v-col>
<v-col cols="12" md="6" xl="3">
<v-card>
<v-card-header>
<v-card-header-text>
<v-card-title>
Delete Budget
</v-card-title>
<v-card-subtitle>
This deletes the whole bugdet including all transactions, assignments, accounts and categories. Not undoable!
</v-card-subtitle>
</v-card-header-text>
</v-card-header>
<v-card-actions class="justify-center">
<v-btn @click="deleteBudget" color="error">Delete budget</v-btn>
</v-card-actions>
</v-card>
</v-col>
<v-col cols="12" md="6" xl="3">
<v-card>
<v-card-header>