Try to implement new transaction
This commit is contained in:
86
http/http.go
86
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 {
|
||||
|
Reference in New Issue
Block a user