58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
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.DB.GetBudget(c.Request.Context(), budgetUUID)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusNotFound, err)
|
|
return
|
|
}
|
|
|
|
accounts, err := h.Service.DB.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()
|
|
}
|