Extract getImportantData to own file
This commit is contained in:
parent
6628a4849f
commit
61fa6ed776
@ -8,11 +8,6 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AlwaysNeededData struct {
|
||||
Budget postgres.Budget
|
||||
Accounts []postgres.GetAccountsWithBalanceRow
|
||||
}
|
||||
|
||||
type AccountsData struct {
|
||||
AlwaysNeededData
|
||||
}
|
||||
|
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()
|
||||
}
|
@ -10,11 +10,12 @@ import (
|
||||
)
|
||||
|
||||
type BudgetData struct {
|
||||
Budget *postgres.Budget
|
||||
AlwaysNeededData
|
||||
Transactions []postgres.GetTransactionsForBudgetRow
|
||||
}
|
||||
|
||||
func (h *Handler) budget(c *gin.Context) {
|
||||
base := c.MustGet("data").(AlwaysNeededData)
|
||||
budgetID := c.Param("budgetid")
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
if err != nil {
|
||||
@ -22,12 +23,6 @@ func (h *Handler) budget(c *gin.Context) {
|
||||
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)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
@ -35,8 +30,8 @@ func (h *Handler) budget(c *gin.Context) {
|
||||
}
|
||||
|
||||
d := BudgetData{
|
||||
Budget: &budget,
|
||||
Transactions: transactions,
|
||||
base,
|
||||
transactions,
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "budget.html", d)
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
"git.javil.eu/jacob1123/budgeteer"
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
token, err := h.verifyLogin(c)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user