This commit is contained in:
2025-03-03 13:49:36 +01:00
parent 016b54f8dd
commit c7b10d24be
7 changed files with 147 additions and 71 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin"
"example.com/gin/test/models"
//"example.com/gin/test/services"
"example.com/gin/test/repositories"
)
@@ -24,6 +25,8 @@ type ShopItemController interface {
ShopItemView(*gin.Context)
AddItemView(*gin.Context)
AddItemHandler(*gin.Context)
CreateTag(*gin.Context)
GetAllTags(*gin.Context)
}
type shopItemController struct {}
@@ -54,26 +57,60 @@ func (rc *shopItemController) GetById(c *gin.Context) {
ReplyOK(c, shopItem)
}
func (rc *shopItemController) Create(c *gin.Context) {
shopItem, err := models.NewShopItem(c)
func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.ShopItem, error) {
name := ctx.PostForm("name")
abstract := ctx.PostForm("abstract")
description := ctx.PostForm("description")
priceStr := ctx.PostForm("price")
tagIds := ctx.PostFormArray("tags[]")
if err != nil {
ReplyError(c, err)
if name == "" || description == "" {
return models.ShopItem{}, fmt.Errorf("Name or description empty")
}
_, err = repositories.ShopItems.Create(shopItem)
// Convert the price string to float64
price, err := strconv.ParseFloat(priceStr, 64)
if err != nil {
return models.ShopItem{}, fmt.Errorf("Could not parse price")
}
shopItem := models.ShopItem{
Name: name,
Abstract: abstract,
Description: description,
Price: price,
IsPublic: true,
}
for _, tagId := range tagIds {
tag, err := repositories.Tags.GetById(tagId)
if err != nil {
return models.ShopItem{}, fmt.Errorf("Could not get tag by id")
}
shopItem.Tags = append(shopItem.Tags, tag)
}
return shopItem, nil
//return services.ShopItems.NewShopItem(name, abstract, description, price, tagIds)
}
func (rc *shopItemController) Create(c *gin.Context) {
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
//userID := user.(models.User).ID
//rc.DB.Model(&models.shopItem{}).Where("id = ?"), room.ID).Association("Admins").Append(&models.User{ID: userID})
_, err = repositories.ShopItems.Create(shopItem)
//if result.Error != nil {
// ReplyError(c, fmt.Errorf("shopItem creation failed: %s", result.Error))
// return
//}
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
ReplyOK(c, "shopItem was created")
}
@@ -87,7 +124,7 @@ func (rc *shopItemController) Update(c *gin.Context) {
return
}
shopItem, err := models.NewShopItem(c)
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
ReplyError(c, err)
@@ -119,8 +156,20 @@ func (rc *shopItemController) Delete(c *gin.Context) {
func (rc *shopItemController) ShopItemView(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
if err != nil {
c.HTML(http.StatusBadRequest, "shopitem.html", gin.H{ "data": gin.H{ "error": err } })
}
//TODO: get tags by item
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "shopitem.html", gin.H{ "data": gin.H{ "error": err } })
}
data := CreateSessionData(c, gin.H{
"shopItem": shopItem,
"tags": tags,
})
if err != nil {
@@ -132,9 +181,16 @@ func (rc *shopItemController) ShopItemView(c *gin.Context) {
func (rc *shopItemController) AddItemView(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
}
data := CreateSessionData(c, gin.H{
"error": "",
"success": "",
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
@@ -142,10 +198,18 @@ func (rc *shopItemController) AddItemView(c *gin.Context) {
func (rc *shopItemController) AddItemHandler(c *gin.Context) {
shopItem, err := models.NewShopItem(c)
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
ReplyError(c, err)
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
return
}
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
return
}
_, err = repositories.ShopItems.Create(shopItem)
@@ -153,6 +217,7 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) {
data := CreateSessionData(c, gin.H{
"error": err,
"success": "",
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
@@ -162,11 +227,48 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) {
data := CreateSessionData(c, gin.H{
"error": "",
"success": fmt.Sprintf("Item '%s' Registered", shopItem.Name),
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
}
func (rc *shopItemController) CreateTag(c *gin.Context) {
tag, err := models.NewTagByJson(c)
if err != nil {
ReplyError(c, err)
}
_, err = repositories.Tags.Create(tag)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
//userID := user.(models.User).ID
//rc.DB.Model(&models.shopItem{}).Where("id = ?"), room.ID).Association("Admins").Append(&models.User{ID: userID})
//if result.Error != nil {
// ReplyError(c, fmt.Errorf("shopItem creation failed: %s", result.Error))
// return
//}
ReplyOK(c, fmt.Sprintf("tag '%s' was created", tag.Name))
}
func (rc *shopItemController) GetAllTags(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
ReplyError(c, fmt.Errorf("Could not query Tags"))
return
}
c.JSON(http.StatusOK, tags)
}
func ReplyError(ctx *gin.Context, err error) {
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
}