From 065159a817d6c58a71d789afdb4d72eccfc4c189 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Sun, 6 Feb 2022 20:15:00 +0000 Subject: [PATCH] Cleanup unused backend code and routes --- http/account.go | 7 -- http/always-needed-data.go | 10 --- http/budget.go | 41 ----------- http/budgeting.go | 137 +++++++++++++++++++++---------------- http/http.go | 2 - http/transaction-edit.go | 62 ----------------- 6 files changed, 77 insertions(+), 182 deletions(-) delete mode 100644 http/always-needed-data.go delete mode 100644 http/transaction-edit.go diff --git a/http/account.go b/http/account.go index 1417661..93867a7 100644 --- a/http/account.go +++ b/http/account.go @@ -8,13 +8,6 @@ import ( "github.com/google/uuid" ) -type AccountData struct { - AlwaysNeededData - Account *postgres.Account - Categories []postgres.GetCategoriesRow - Transactions []postgres.GetTransactionsForAccountRow -} - func (h *Handler) transactionsForAccount(c *gin.Context) { accountID := c.Param("accountid") accountUUID, err := uuid.Parse(accountID) diff --git a/http/always-needed-data.go b/http/always-needed-data.go deleted file mode 100644 index ae2c23a..0000000 --- a/http/always-needed-data.go +++ /dev/null @@ -1,10 +0,0 @@ -package http - -import ( - "git.javil.eu/jacob1123/budgeteer/postgres" -) - -type AlwaysNeededData struct { - Budget postgres.Budget - Accounts []postgres.GetAccountsWithBalanceRow -} diff --git a/http/budget.go b/http/budget.go index 6129ec4..5b93c40 100644 --- a/http/budget.go +++ b/http/budget.go @@ -5,50 +5,9 @@ import ( "net/http" "git.javil.eu/jacob1123/budgeteer" - "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" - "github.com/google/uuid" ) -type AllAccountsData struct { - AlwaysNeededData - Account *postgres.Account - Categories []postgres.GetCategoriesRow - Transactions []postgres.GetTransactionsForBudgetRow -} - -func (h *Handler) allAccounts(c *gin.Context) { - budgetID := c.Param("budgetid") - budgetUUID, err := uuid.Parse(budgetID) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - } - - categories, err := h.Service.GetCategories(c.Request.Context(), budgetUUID) - if err != nil { - c.AbortWithError(http.StatusNotFound, err) - return - } - - transactions, err := h.Service.GetTransactionsForBudget(c.Request.Context(), budgetUUID) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - d := AllAccountsData{ - c.MustGet("data").(AlwaysNeededData), - &postgres.Account{ - Name: "All accounts", - }, - categories, - transactions, - } - - c.HTML(http.StatusOK, "account.html", d) -} - type newBudgetInformation struct { Name string `json:"name"` } diff --git a/http/budgeting.go b/http/budgeting.go index f827dcf..f666fec 100644 --- a/http/budgeting.go +++ b/http/budgeting.go @@ -11,15 +11,6 @@ import ( "github.com/google/uuid" ) -type BudgetingData struct { - AlwaysNeededData - Categories []CategoryWithBalance - AvailableBalance float64 - Date time.Time - Next time.Time - Previous time.Time -} - func getFirstOfMonth(year, month int, location *time.Location) time.Time { return time.Date(year, time.Month(month), 1, 0, 0, 0, 0, location) } @@ -104,6 +95,73 @@ func (h *Handler) autocompletePayee(c *gin.Context) { c.JSON(http.StatusOK, payees) } +func (h *Handler) budgetingForMonth(c *gin.Context) { + budgetID := c.Param("budgetid") + budgetUUID, err := uuid.Parse(budgetID) + if err != nil { + c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL")) + return + } + + budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + firstOfMonth, err := getDate(c) + if err != nil { + c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String()) + return + } + + categories, err := h.Service.GetCategories(c.Request.Context(), budgetUUID) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) + cumultativeBalances, err := h.Service.GetCumultativeBalances(c.Request.Context(), budgetUUID) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("load balances: %w", err)) + return + } + + // skip everything in the future + categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) + if err != nil { + return + } + + var availableBalance float64 = 0 + for _, cat := range categories { + if cat.ID != budget.IncomeCategoryID { + continue + } + availableBalance = moneyUsed + + for _, bal := range cumultativeBalances { + if bal.CategoryID != cat.ID { + continue + } + + if !bal.Date.Before(firstOfNextMonth) { + continue + } + + availableBalance += bal.Transactions.GetFloat64() + } + } + + data := struct { + Categories []CategoryWithBalance + AvailableBalance float64 + }{categoriesWithBalance, availableBalance} + c.JSON(http.StatusOK, data) + +} + func (h *Handler) budgeting(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) @@ -124,64 +182,23 @@ func (h *Handler) budgeting(c *gin.Context) { return } - data := AlwaysNeededData{ - Accounts: accounts, - Budget: budget, - } - - firstOfMonth, err := getDate(c) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String()) - return - } - - firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) - firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) - d := BudgetingData{ - AlwaysNeededData: data, - Date: firstOfMonth, - Next: firstOfNextMonth, - Previous: firstOfPreviousMonth, - } - categories, err := h.Service.GetCategories(c.Request.Context(), budgetUUID) - - cumultativeBalances, err := h.Service.GetCumultativeBalances(c.Request.Context(), budgetUUID) if err != nil { - c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("load balances: %w", err)) + c.AbortWithError(http.StatusInternalServerError, err) return } - // skip everything in the future - categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, data.Budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) - if err != nil { - return - } - d.Categories = categoriesWithBalance - - var availableBalance float64 = 0 - for _, cat := range categories { - if cat.ID != data.Budget.IncomeCategoryID { - continue - } - availableBalance = moneyUsed - - for _, bal := range cumultativeBalances { - if bal.CategoryID != cat.ID { - continue - } - - if !bal.Date.Before(firstOfNextMonth) { - continue - } - - availableBalance += bal.Transactions.GetFloat64() - } + data := struct { + Budget postgres.Budget + Accounts []postgres.GetAccountsWithBalanceRow + Categories []postgres.GetCategoriesRow + }{ + Accounts: accounts, + Budget: budget, + Categories: categories, } - d.AvailableBalance = availableBalance - - c.JSON(http.StatusOK, d) + c.JSON(http.StatusOK, data) } func (h *Handler) calculateBalances(c *gin.Context, budget postgres.Budget, firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, float64, error) { diff --git a/http/http.go b/http/http.go index 2ba5920..01a0742 100644 --- a/http/http.go +++ b/http/http.go @@ -49,9 +49,7 @@ func (h *Handler) Serve() { withBudget := router.Group("") withBudget.Use(h.verifyLoginWithForbidden) withBudget.GET("/budget/:budgetid/:year/:month", h.budgeting) - withBudget.GET("/budget/:budgetid/all-accounts", h.allAccounts) withBudget.GET("/budget/:budgetid/settings/clean-negative", h.cleanNegativeBudget) - withBudget.GET("/budget/:budgetid/transaction/:transactionid", h.transaction) api := router.Group("/api/v1") diff --git a/http/transaction-edit.go b/http/transaction-edit.go deleted file mode 100644 index d784e2b..0000000 --- a/http/transaction-edit.go +++ /dev/null @@ -1,62 +0,0 @@ -package http - -import ( - "net/http" - - "git.javil.eu/jacob1123/budgeteer/postgres" - "github.com/gin-gonic/gin" - "github.com/google/uuid" -) - -type TransactionData struct { - AlwaysNeededData - Transaction *postgres.Transaction - Account *postgres.Account - Categories []postgres.GetCategoriesRow - Payees []postgres.Payee -} - -func (h *Handler) transaction(c *gin.Context) { - data := c.MustGet("data").(AlwaysNeededData) - - transactionID := c.Param("transactionid") - transactionUUID, err := uuid.Parse(transactionID) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - } - - transaction, err := h.Service.GetTransaction(c.Request.Context(), transactionUUID) - if err != nil { - c.AbortWithError(http.StatusNotFound, err) - return - } - - account, err := h.Service.GetAccount(c.Request.Context(), transaction.AccountID) - if err != nil { - c.AbortWithError(http.StatusNotFound, err) - return - } - - categories, err := h.Service.GetCategories(c.Request.Context(), data.Budget.ID) - if err != nil { - c.AbortWithError(http.StatusNotFound, err) - return - } - - payees, err := h.Service.GetPayees(c.Request.Context(), data.Budget.ID) - if err != nil { - c.AbortWithError(http.StatusNotFound, err) - return - } - - d := TransactionData{ - data, - &transaction, - &account, - categories, - payees, - } - - c.HTML(http.StatusOK, "transaction.html", d) -}