From 31f44227394b4b0cf4e2b2a0b0911a10e3dbb7f6 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sun, 2 Mar 2025 20:29:38 +0100 Subject: [PATCH] add item adding/viewing --- controllers/shopItemController.go | 54 +++++++++++++++++++++++++++++++ main.go | 3 ++ models/shopItem.go | 24 +++++++++++--- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/controllers/shopItemController.go b/controllers/shopItemController.go index 9af8aba..6d874d7 100644 --- a/controllers/shopItemController.go +++ b/controllers/shopItemController.go @@ -21,6 +21,9 @@ type CRUDController interface { type ShopItemController interface { CRUDController + ShopItemView(*gin.Context) + AddItemView(*gin.Context) + AddItemHandler(*gin.Context) } type shopItemController struct {} @@ -113,6 +116,57 @@ func (rc *shopItemController) Delete(c *gin.Context) { ReplyOK(c, "shopItem was deleted") } +func (rc *shopItemController) ShopItemView(c *gin.Context) { + shopItem, err := repositories.ShopItems.GetById(c.Param("id")) + + data := CreateSessionData(c, gin.H{ + "shopItem": shopItem, + }) + + if err != nil { + c.HTML(http.StatusBadRequest, "shopitem.html", data) + } + + c.HTML(http.StatusOK, "shopitem.html", data) +} + + +func (rc *shopItemController) AddItemView(c *gin.Context) { + data := CreateSessionData(c, gin.H{ + "error": "", + "success": "", + }) + + c.HTML(http.StatusOK, "additem.html", data) +} + + +func (rc *shopItemController) AddItemHandler(c *gin.Context) { + shopItem, err := models.NewShopItem(c) + + if err != nil { + ReplyError(c, err) + } + + _, err = repositories.ShopItems.Create(shopItem) + if err != nil { + data := CreateSessionData(c, gin.H{ + "error": err, + "success": "", + }) + + c.HTML(http.StatusOK, "additem.html", data) + return + } + + data := CreateSessionData(c, gin.H{ + "error": "", + "success": fmt.Sprintf("Item '%s' Registered", shopItem.Name), + }) + + c.HTML(http.StatusOK, "additem.html", data) +} + func ReplyError(ctx *gin.Context, err error) { ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() }) } diff --git a/main.go b/main.go index 605d3a0..5218b5e 100644 --- a/main.go +++ b/main.go @@ -74,13 +74,16 @@ func main() { viewRoutes := server.Group("/", authValidator.OptionalAuth) { viewRoutes.GET("/", userController.MainView) + viewRoutes.GET("/shopitems/:id", shopItemController.ShopItemView) //write middleware that redirects to homescreen on register/login/reset for logged in users viewRoutes.GET("/login", userController.LoginView) viewRoutes.GET("/logout", userController.Logout) viewRoutes.GET("/register", userController.RegisterView) viewRoutes.GET("/passwordreset", userController.ResetView) + viewRoutes.GET("/additem", authValidator.RequireAuth, shopItemController.AddItemView) viewRoutes.POST("/login", userController.LoginHandler) viewRoutes.POST("/register", userController.RegisterHandler) + viewRoutes.POST("/additem", authValidator.RequireAuth, shopItemController.AddItemHandler) viewRoutes.POST("/passwordreset", userController.ResetHandler) } diff --git a/models/shopItem.go b/models/shopItem.go index ec65765..de8d38f 100644 --- a/models/shopItem.go +++ b/models/shopItem.go @@ -1,6 +1,8 @@ package models import ( + "fmt" + "strconv" "gorm.io/gorm" "github.com/gin-gonic/gin" ) @@ -15,11 +17,25 @@ type ShopItem struct { } func NewShopItem(ctx *gin.Context) (ShopItem, error) { - var shopItem ShopItem - err := ctx.ShouldBindJSON(&shopItem) + name := ctx.PostForm("name") + description := ctx.PostForm("description") + priceStr := ctx.PostForm("price") - if err != nil { - return ShopItem{}, err + // Convert the price string to float64 + price, err := strconv.ParseFloat(priceStr, 64) + if err != nil { + return ShopItem{}, fmt.Errorf("Could not parse price") + } + + shopItem := ShopItem{ + Name: name, + Description: description, + Price: price, + IsPublic: true, + } + + if name == "" || description == "" { + return ShopItem{}, fmt.Errorf("Name or description empty") } return shopItem, nil