Add budgeting
This commit is contained in:
70
http/budgeting.go
Normal file
70
http/budgeting.go
Normal file
@ -0,0 +1,70 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type BudgetingData struct {
|
||||
AlwaysNeededData
|
||||
Categories []postgres.GetCategoriesWithBalanceRow
|
||||
}
|
||||
|
||||
func (h *Handler) budgeting(c *gin.Context) {
|
||||
budgetID := c.Param("budgetid")
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
if err != nil {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
var year, month int
|
||||
yearString := c.Param("year")
|
||||
monthString := c.Param("month")
|
||||
if yearString != "" && monthString != "" {
|
||||
year, err = strconv.Atoi(yearString)
|
||||
if err != nil {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String())
|
||||
return
|
||||
}
|
||||
|
||||
month, err = strconv.Atoi(monthString)
|
||||
if err != nil {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String())
|
||||
return
|
||||
}
|
||||
|
||||
} else {
|
||||
var monthM time.Month
|
||||
year, monthM, _ = now.Date()
|
||||
month = int(monthM)
|
||||
}
|
||||
|
||||
firstOfMonth := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, now.Location())
|
||||
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
|
||||
|
||||
params := postgres.GetCategoriesWithBalanceParams{
|
||||
BudgetID: budgetUUID,
|
||||
FromDate: firstOfMonth,
|
||||
ToDate: lastOfMonth,
|
||||
}
|
||||
categories, err := h.Service.DB.GetCategoriesWithBalance(context.Background(), params)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
d := BudgetingData{
|
||||
c.MustGet("data").(AlwaysNeededData),
|
||||
categories,
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "budgeting.html", d)
|
||||
}
|
@ -57,6 +57,7 @@ func (h *Handler) Serve() {
|
||||
withBudget.Use(h.verifyLoginWithRedirect)
|
||||
withBudget.Use(h.getImportantData)
|
||||
withBudget.GET("/budget/:budgetid", h.budgeting)
|
||||
withBudget.GET("/budget/:budgetid/:year/:month", h.budgeting)
|
||||
withBudget.GET("/budget/:budgetid/all-accounts", h.budget)
|
||||
withBudget.GET("/budget/:budgetid/accounts", h.accounts)
|
||||
withBudget.GET("/budget/:budgetid/account/:accountid", h.account)
|
||||
|
Reference in New Issue
Block a user