Extract getImportantData to own file

This commit is contained in:
Jan Bader 2021-12-04 22:58:52 +00:00
parent 6628a4849f
commit 61fa6ed776
4 changed files with 48 additions and 37 deletions

View File

@ -8,11 +8,6 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
type AlwaysNeededData struct {
Budget postgres.Budget
Accounts []postgres.GetAccountsWithBalanceRow
}
type AccountsData struct { type AccountsData struct {
AlwaysNeededData AlwaysNeededData
} }

View File

@ -0,0 +1,44 @@
package http
import (
"context"
"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
}
func (h *Handler) getImportantData(c *gin.Context) {
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
}
accounts, err := h.Service.DB.GetAccountsWithBalance(c.Request.Context(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
base := AlwaysNeededData{
Accounts: accounts,
Budget: budget,
}
c.Set("data", base)
c.Next()
}

View File

@ -10,11 +10,12 @@ import (
) )
type BudgetData struct { type BudgetData struct {
Budget *postgres.Budget AlwaysNeededData
Transactions []postgres.GetTransactionsForBudgetRow Transactions []postgres.GetTransactionsForBudgetRow
} }
func (h *Handler) budget(c *gin.Context) { func (h *Handler) budget(c *gin.Context) {
base := c.MustGet("data").(AlwaysNeededData)
budgetID := c.Param("budgetid") budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID) budgetUUID, err := uuid.Parse(budgetID)
if err != nil { if err != nil {
@ -22,12 +23,6 @@ func (h *Handler) budget(c *gin.Context) {
return 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) transactions, err := h.Service.DB.GetTransactionsForBudget(context.Background(), budgetUUID)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
@ -35,8 +30,8 @@ func (h *Handler) budget(c *gin.Context) {
} }
d := BudgetData{ d := BudgetData{
Budget: &budget, base,
Transactions: transactions, transactions,
} }
c.HTML(http.StatusOK, "budget.html", d) c.HTML(http.StatusOK, "budget.html", d)

View File

@ -8,7 +8,6 @@ import (
"git.javil.eu/jacob1123/budgeteer" "git.javil.eu/jacob1123/budgeteer"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) { func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) {
@ -26,28 +25,6 @@ func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) {
return token, nil return token, nil
} }
func (h *Handler) getImportantData(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 := AlwaysNeededData{
Accounts: accounts,
}
c.Set("data", base)
c.Next()
}
func (h *Handler) verifyLoginWithRedirect(c *gin.Context) { func (h *Handler) verifyLoginWithRedirect(c *gin.Context) {
token, err := h.verifyLogin(c) token, err := h.verifyLogin(c)
if err != nil { if err != nil {