add item adding/viewing
This commit is contained in:
@@ -21,6 +21,9 @@ type CRUDController interface {
|
|||||||
|
|
||||||
type ShopItemController interface {
|
type ShopItemController interface {
|
||||||
CRUDController
|
CRUDController
|
||||||
|
ShopItemView(*gin.Context)
|
||||||
|
AddItemView(*gin.Context)
|
||||||
|
AddItemHandler(*gin.Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
type shopItemController struct {}
|
type shopItemController struct {}
|
||||||
@@ -113,6 +116,57 @@ func (rc *shopItemController) Delete(c *gin.Context) {
|
|||||||
ReplyOK(c, "shopItem was deleted")
|
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) {
|
func ReplyError(ctx *gin.Context, err error) {
|
||||||
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
|
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
|
||||||
}
|
}
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -74,13 +74,16 @@ func main() {
|
|||||||
viewRoutes := server.Group("/", authValidator.OptionalAuth)
|
viewRoutes := server.Group("/", authValidator.OptionalAuth)
|
||||||
{
|
{
|
||||||
viewRoutes.GET("/", userController.MainView)
|
viewRoutes.GET("/", userController.MainView)
|
||||||
|
viewRoutes.GET("/shopitems/:id", shopItemController.ShopItemView)
|
||||||
//write middleware that redirects to homescreen on register/login/reset for logged in users
|
//write middleware that redirects to homescreen on register/login/reset for logged in users
|
||||||
viewRoutes.GET("/login", userController.LoginView)
|
viewRoutes.GET("/login", userController.LoginView)
|
||||||
viewRoutes.GET("/logout", userController.Logout)
|
viewRoutes.GET("/logout", userController.Logout)
|
||||||
viewRoutes.GET("/register", userController.RegisterView)
|
viewRoutes.GET("/register", userController.RegisterView)
|
||||||
viewRoutes.GET("/passwordreset", userController.ResetView)
|
viewRoutes.GET("/passwordreset", userController.ResetView)
|
||||||
|
viewRoutes.GET("/additem", authValidator.RequireAuth, shopItemController.AddItemView)
|
||||||
viewRoutes.POST("/login", userController.LoginHandler)
|
viewRoutes.POST("/login", userController.LoginHandler)
|
||||||
viewRoutes.POST("/register", userController.RegisterHandler)
|
viewRoutes.POST("/register", userController.RegisterHandler)
|
||||||
|
viewRoutes.POST("/additem", authValidator.RequireAuth, shopItemController.AddItemHandler)
|
||||||
viewRoutes.POST("/passwordreset", userController.ResetHandler)
|
viewRoutes.POST("/passwordreset", userController.ResetHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -15,11 +17,25 @@ type ShopItem struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewShopItem(ctx *gin.Context) (ShopItem, error) {
|
func NewShopItem(ctx *gin.Context) (ShopItem, error) {
|
||||||
var shopItem ShopItem
|
name := ctx.PostForm("name")
|
||||||
err := ctx.ShouldBindJSON(&shopItem)
|
description := ctx.PostForm("description")
|
||||||
|
priceStr := ctx.PostForm("price")
|
||||||
|
|
||||||
if err != nil {
|
// Convert the price string to float64
|
||||||
return ShopItem{}, err
|
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
|
return shopItem, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user