Extract loading of accounts to middleware

This commit is contained in:
2021-12-04 22:47:01 +00:00
parent e30fab6a06
commit 6628a4849f
3 changed files with 40 additions and 23 deletions

View File

@ -8,26 +8,19 @@ import (
"github.com/google/uuid"
)
type AccountsData struct {
type AlwaysNeededData struct {
Budget postgres.Budget
Accounts []postgres.GetAccountsWithBalanceRow
}
type AccountsData struct {
AlwaysNeededData
}
func (h *Handler) accounts(c *gin.Context) {
budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID)
if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
accounts, err := h.Service.DB.GetAccountsWithBalance(c.Request.Context(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
base := c.MustGet("data").(AlwaysNeededData)
d := AccountsData{
Accounts: accounts,
base,
}
c.HTML(http.StatusOK, "accounts.html", d)