Extract getImportantData to own file
This commit is contained in:
44
http/always-needed-data.go
Normal file
44
http/always-needed-data.go
Normal 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()
|
||||
}
|
Reference in New Issue
Block a user