package http import ( "net/http" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type AlwaysNeededData struct { Budget postgres.Budget Accounts []postgres.GetAccountsWithBalanceRow OnBudgetAccounts []postgres.GetAccountsWithBalanceRow OffBudgetAccounts []postgres.GetAccountsWithBalanceRow } func (h *Handler) getImportantData(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { c.Redirect(http.StatusTemporaryRedirect, "/login") c.Abort() return } budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusNotFound, err) return } accounts, err := h.Service.GetAccountsWithBalance(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } var onBudgetAccounts, offBudgetAccounts []postgres.GetAccountsWithBalanceRow for _, account := range accounts { if account.OnBudget { onBudgetAccounts = append(onBudgetAccounts, account) } else { offBudgetAccounts = append(offBudgetAccounts, account) } } base := AlwaysNeededData{ Accounts: accounts, OnBudgetAccounts: onBudgetAccounts, OffBudgetAccounts: offBudgetAccounts, Budget: budget, } c.Set("data", base) c.Next() }