diff --git a/http/http.go b/http/http.go index 2ce61b3..dbe8fa3 100644 --- a/http/http.go +++ b/http/http.go @@ -14,6 +14,8 @@ import ( "git.javil.eu/jacob1123/budgeteer/web" "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/jackc/pgtype" ) // Handler handles incoming requests @@ -44,26 +46,98 @@ func (h *Handler) Serve() { router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index", nil) }) router.GET("/login", h.login) router.GET("/register", h.register) - router.GET("/dashboard", h.dashboard) - router.GET("/budget/:budgetid", h.budget) + authenticatedFrontend := router.Group("") + { + authenticatedFrontend.Use(h.verifyLoginWithRedirect) + authenticatedFrontend.GET("/dashboard", h.dashboard) + authenticatedFrontend.GET("/budget/:budgetid", h.budget) + } api := router.Group("/api/v1") { user := api.Group("/user") { - user.GET("/logout", logout) - user.GET("/login", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, "/login") }) - user.POST("/login", h.loginPost) - user.POST("/register", h.registerPost) + unauthenticated := user.Group("") + { + unauthenticated.GET("/login", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, "/login") }) + unauthenticated.POST("/login", h.loginPost) + unauthenticated.POST("/register", h.registerPost) + } + authenticated := user.Group("") + { + authenticated.Use(h.verifyLoginWithRedirect) + authenticated.GET("/logout", logout) + } } budget := api.Group("/budget") { + budget.Use(h.verifyLoginWithRedirect) budget.POST("/new", h.newBudget) } + transaction := api.Group("/transaction") + { + transaction.Use(h.verifyLoginWithRedirect) + transaction.POST("/new", h.newTransaction) + } } router.Run(":1323") } +func (h *Handler) verifyLoginWithRedirect(c *gin.Context) { + _, err := h.verifyLogin(c) + if err != nil { + c.Redirect(http.StatusTemporaryRedirect, "/login") + return + } + + c.Next() +} + +func (h *Handler) newTransaction(c *gin.Context) { + + transactionMemo, succ := c.GetPostForm("memo") + if !succ { + c.AbortWithStatus(http.StatusNotAcceptable) + return + } + + transactionAccount, succ := c.GetPostForm("account_id") + if !succ { + c.AbortWithStatus(http.StatusNotAcceptable) + return + } + + transactionAccountID, err := uuid.Parse(transactionAccount) + if !succ { + c.AbortWithStatus(http.StatusNotAcceptable) + return + } + + transactionDate, succ := c.GetPostForm("date") + if !succ { + c.AbortWithStatus(http.StatusNotAcceptable) + return + } + + transactionDateValue, err := time.Parse("2006-01-02", transactionDate) + if err != nil { + c.AbortWithStatus(http.StatusNotAcceptable) + return + } + + new := postgres.CreateTransactionParams{ + Memo: transactionMemo, + Date: transactionDateValue, + Amount: pgtype.Numeric{}, + AccountID: transactionAccountID, + } + _, err = h.Service.DB.CreateTransaction(c.Request.Context(), new) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } +} + func (h *Handler) newBudget(c *gin.Context) { token, err := h.verifyLogin(c) if err != nil { diff --git a/web/budget.html b/web/budget.html index 462fe0f..6048db8 100644 --- a/web/budget.html +++ b/web/budget.html @@ -25,7 +25,7 @@ {{end}} - {{template "budget-new"}} + {{template "transaction-new"}} {{end}} \ No newline at end of file diff --git a/web/transaction-new.html b/web/transaction-new.html new file mode 100644 index 0000000..51c61f2 --- /dev/null +++ b/web/transaction-new.html @@ -0,0 +1,44 @@ +{{define "transaction-new"}} + +{{end}} \ No newline at end of file