budgeteer/http/budget.go

53 lines
1.1 KiB
Go

package http
import (
"context"
"net/http"
"git.javil.eu/jacob1123/budgeteer"
"git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type BudgetData struct {
Token budgeteer.Token
Budget *postgres.Budget
Transactions []postgres.Transaction
}
func (h *Handler) budget(c *gin.Context) {
token, err := h.verifyLogin(c)
if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID)
if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
budget, err := h.Service.DB.GetBudget(context.Background(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
transactions, err := h.Service.DB.GetTransactionsForBudget(context.Background(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
d := BudgetData{
Token: token,
Budget: &budget,
Transactions: transactions,
}
c.HTML(http.StatusOK, "budget", d)
}