diff --git a/server/account.go b/server/account.go index 65b0908..d92746c 100644 --- a/server/account.go +++ b/server/account.go @@ -69,5 +69,5 @@ func (h *Handler) editAccount(c *gin.Context) { return } - h.returnBudgetingData(c, account.BudgetID) + h.getBudget(c, account.BudgetID) } diff --git a/server/admin.go b/server/admin.go index 7d19da3..b3a5b81 100644 --- a/server/admin.go +++ b/server/admin.go @@ -64,49 +64,3 @@ func (h *Handler) clearBudget(c *gin.Context) { h.clearBudgetData(c, budgetUUID) } - -func (h *Handler) cleanNegativeBudget(c *gin.Context) { - /*budgetID := c.Param("budgetid") - budgetUUID, err := uuid.Parse(budgetID) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - }*/ - - /*min_date, err := h.Service.GetFirstActivity(c.Request.Context(), budgetUUID) - date := getFirstOfMonthTime(min_date) - for { - nextDate := date.AddDate(0, 1, 0) - params := postgres.GetCategoriesWithBalanceParams{ - BudgetID: budgetUUID, - ToDate: nextDate, - FromDate: date, - } - categories, err := h.Service.GetCategoriesWithBalance(c.Request.Context(), params) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - for _, category := range categories { - available := category.Available.GetFloat64() - if available >= 0 { - continue - } - var negativeAvailable postgres.Numeric - negativeAvailable.Set(-available) - createAssignment := postgres.CreateAssignmentParams{ - Date: nextDate.AddDate(0, 0, -1), - Amount: negativeAvailable, - CategoryID: category.ID, - } - h.Service.CreateAssignment(c.Request.Context(), createAssignment) - } - - if nextDate.Before(time.Now()) { - date = nextDate - } else { - break - } - }*/ -} diff --git a/server/budgeting.go b/server/budgeting.go index b8341b7..814d94b 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -126,7 +126,7 @@ type BudgetingResponse struct { Budget postgres.Budget } -func (h *Handler) budgeting(c *gin.Context) { +func (h *Handler) budget(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { @@ -134,10 +134,10 @@ func (h *Handler) budgeting(c *gin.Context) { return } - h.returnBudgetingData(c, budgetUUID) + h.getBudget(c, budgetUUID) } -func (h *Handler) returnBudgetingData(c *gin.Context, budgetUUID uuid.UUID) { +func (h *Handler) getBudget(c *gin.Context, budgetUUID uuid.UUID) { budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusNotFound, err) diff --git a/server/dashboard.go b/server/dashboard.go deleted file mode 100644 index 64667d8..0000000 --- a/server/dashboard.go +++ /dev/null @@ -1,25 +0,0 @@ -package server - -import ( - "net/http" - - "git.javil.eu/jacob1123/budgeteer/postgres" - "github.com/gin-gonic/gin" -) - -func (h *Handler) dashboard(c *gin.Context) { - userID := MustGetToken(c).GetID() - budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), userID) - if err != nil { - return - } - - d := DashboardData{ - Budgets: budgets, - } - c.JSON(http.StatusOK, d) -} - -type DashboardData struct { - Budgets []postgres.Budget -} diff --git a/server/http.go b/server/http.go index e958687..b96a645 100644 --- a/server/http.go +++ b/server/http.go @@ -48,35 +48,34 @@ func (h *Handler) LoadRoutes(router *gin.Engine) { api := router.Group("/api/v1") anonymous := api.Group("/user") - anonymous.GET("/login", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, "/login") }) anonymous.POST("/login", h.loginPost) anonymous.POST("/register", h.registerPost) authenticated := api.Group("") - authenticated.Use(h.verifyLoginWithForbidden) - authenticated.GET("/dashboard", h.dashboard) - authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount) - authenticated.POST("/account/:accountid/reconcile", h.reconcileTransactions) - authenticated.POST("/account/:accountid", h.editAccount) - authenticated.GET("/admin/clear-database", h.clearDatabase) + { + authenticated.Use(h.verifyLoginWithForbidden) + authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount) + authenticated.POST("/account/:accountid/reconcile", h.reconcileTransactions) + authenticated.POST("/account/:accountid", h.editAccount) + authenticated.GET("/admin/clear-database", h.clearDatabase) - budget := authenticated.Group("/budget") - budget.POST("/new", h.newBudget) - budget.GET("/:budgetid", h.budgeting) - budget.GET("/:budgetid/:year/:month", h.budgetingForMonth) - budget.POST("/:budgetid/category/:categoryid/:year/:month", h.setCategoryAssignment) - budget.GET("/:budgetid/autocomplete/payees", h.autocompletePayee) - budget.GET("/:budgetid/autocomplete/categories", h.autocompleteCategories) - budget.DELETE("/:budgetid", h.deleteBudget) - budget.POST("/:budgetid/import/ynab", h.importYNAB) - budget.POST("/:budgetid/export/ynab/transactions", h.exportYNABTransactions) - budget.POST("/:budgetid/export/ynab/assignments", h.exportYNABAssignments) - budget.POST("/:budgetid/settings/clear", h.clearBudget) - budget.POST("/:budgetid/settings/clean-negative", h.cleanNegativeBudget) + budget := authenticated.Group("/budget") + budget.POST("/new", h.newBudget) + budget.GET("/:budgetid", h.budget) + budget.GET("/:budgetid/:year/:month", h.budgetingForMonth) + budget.POST("/:budgetid/category/:categoryid/:year/:month", h.setCategoryAssignment) + budget.GET("/:budgetid/autocomplete/payees", h.autocompletePayee) + budget.GET("/:budgetid/autocomplete/categories", h.autocompleteCategories) + budget.DELETE("/:budgetid", h.deleteBudget) + budget.POST("/:budgetid/import/ynab", h.importYNAB) + budget.POST("/:budgetid/export/ynab/transactions", h.exportYNABTransactions) + budget.POST("/:budgetid/export/ynab/assignments", h.exportYNABAssignments) + budget.POST("/:budgetid/settings/clear", h.clearBudget) - transaction := authenticated.Group("/transaction") - transaction.POST("/new", h.newTransaction) - transaction.POST("/:transactionid", h.newTransaction) + transaction := authenticated.Group("/transaction") + transaction.POST("/new", h.newTransaction) + transaction.POST("/:transactionid", h.updateTransaction) + } } func (h *Handler) ServeStatic(c *gin.Context) { diff --git a/server/transaction.go b/server/transaction.go index a9cce82..0a959ba 100644 --- a/server/transaction.go +++ b/server/transaction.go @@ -27,7 +27,7 @@ type NewTransactionPayload struct { State string `json:"state"` } -func (h *Handler) newTransaction(c *gin.Context) { +func (h *Handler) updateTransaction(c *gin.Context) { var payload NewTransactionPayload err := c.BindJSON(&payload) if err != nil { @@ -42,8 +42,26 @@ func (h *Handler) newTransaction(c *gin.Context) { } transactionID := c.Param("transactionid") - if transactionID != "" { - h.UpdateTransaction(payload, amount, transactionID, c) + transactionUUID, err := uuid.Parse(transactionID) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"transactionid missing from URL"}) + return + } + + h.UpdateTransaction(payload, amount, transactionUUID, c) +} + +func (h *Handler) newTransaction(c *gin.Context) { + var payload NewTransactionPayload + err := c.BindJSON(&payload) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + amount, err := numeric.Parse(payload.Amount) + if err != nil { + c.AbortWithError(http.StatusBadRequest, fmt.Errorf("amount: %w", err)) return } @@ -87,8 +105,7 @@ func (h *Handler) newTransaction(c *gin.Context) { c.JSON(http.StatusOK, transaction) } -func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeric.Numeric, transactionID string, c *gin.Context) { - transactionUUID := uuid.MustParse(transactionID) +func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeric.Numeric, transactionUUID uuid.UUID, c *gin.Context) { if amount.IsZero() { err := h.Service.DeleteTransaction(c.Request.Context(), transactionUUID) if err != nil { diff --git a/web/src/pages/Settings.vue b/web/src/pages/Settings.vue index dd2b55d..55a9a94 100644 --- a/web/src/pages/Settings.vue +++ b/web/src/pages/Settings.vue @@ -44,9 +44,6 @@ function deleteBudget() { function clearBudget() { POST("/budget/" + CurrentBudgetID.value + "/settings/clear", null) }; -function cleanNegative() { - // Fix all historic negative category-balances -}; function ynabImport() { if (transactionsFile.value == undefined || assignmentsFile.value == undefined) return @@ -109,21 +106,6 @@ function ynabExport() { Delete budget - -

- Fix all historic negative category-balances -

-

- This restores YNABs functionality, that would substract any - overspent categories' balances from next months inflows. -

- -

Import YNAB Budget