Cleanup unused backend code and routes
This commit is contained in:
parent
68c2d3ff28
commit
065159a817
@ -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)
|
||||
|
@ -1,10 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
)
|
||||
|
||||
type AlwaysNeededData struct {
|
||||
Budget postgres.Budget
|
||||
Accounts []postgres.GetAccountsWithBalanceRow
|
||||
}
|
@ -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"`
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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")
|
||||
|
||||
|
@ -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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user