Extract loading of accounts to middleware
This commit is contained in:
parent
e30fab6a06
commit
6628a4849f
@ -8,26 +8,19 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountsData struct {
|
type AlwaysNeededData struct {
|
||||||
|
Budget postgres.Budget
|
||||||
Accounts []postgres.GetAccountsWithBalanceRow
|
Accounts []postgres.GetAccountsWithBalanceRow
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AccountsData struct {
|
||||||
|
AlwaysNeededData
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) accounts(c *gin.Context) {
|
func (h *Handler) accounts(c *gin.Context) {
|
||||||
budgetID := c.Param("budgetid")
|
base := c.MustGet("data").(AlwaysNeededData)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
d := AccountsData{
|
d := AccountsData{
|
||||||
Accounts: accounts,
|
base,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "accounts.html", d)
|
c.HTML(http.StatusOK, "accounts.html", d)
|
||||||
|
17
http/http.go
17
http/http.go
@ -46,15 +46,16 @@ func (h *Handler) Serve() {
|
|||||||
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.html", nil) })
|
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.html", nil) })
|
||||||
router.GET("/login", h.login)
|
router.GET("/login", h.login)
|
||||||
router.GET("/register", h.register)
|
router.GET("/register", h.register)
|
||||||
authenticatedFrontend := router.Group("")
|
withBudget := router.Group("")
|
||||||
{
|
{
|
||||||
authenticatedFrontend.Use(h.verifyLoginWithRedirect)
|
withBudget.Use(h.verifyLoginWithRedirect)
|
||||||
authenticatedFrontend.GET("/dashboard", h.dashboard)
|
withBudget.Use(h.getImportantData)
|
||||||
authenticatedFrontend.GET("/budget/:budgetid", h.budget)
|
withBudget.GET("/dashboard", h.dashboard)
|
||||||
authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts)
|
withBudget.GET("/budget/:budgetid", h.budget)
|
||||||
authenticatedFrontend.GET("/account/:accountid", h.account)
|
withBudget.GET("/budget/:budgetid/accounts", h.accounts)
|
||||||
authenticatedFrontend.GET("/admin", h.admin)
|
withBudget.GET("/account/:accountid", h.account)
|
||||||
authenticatedFrontend.GET("/admin/clear-database", h.clearDatabase)
|
withBudget.GET("/admin", h.admin)
|
||||||
|
withBudget.GET("/admin/clear-database", h.clearDatabase)
|
||||||
}
|
}
|
||||||
api := router.Group("/api/v1")
|
api := router.Group("/api/v1")
|
||||||
{
|
{
|
||||||
|
@ -8,6 +8,7 @@ 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) {
|
||||||
@ -25,6 +26,28 @@ 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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user