variants
This commit is contained in:
@@ -53,6 +53,8 @@ func (rc *cartItemController) NewCartItemFromForm(ctx *gin.Context) (models.Cart
|
||||
sessionId := GetSessionId(ctx)
|
||||
shopItemIdStr := ctx.PostForm("ShopItemId")
|
||||
shopItemId, err := strconv.Atoi(shopItemIdStr)
|
||||
itemVariantIdStr := ctx.PostForm("ItemVariantId")
|
||||
itemVariantId, err := strconv.Atoi(itemVariantIdStr)
|
||||
|
||||
if err != nil {
|
||||
return models.CartItem{}, err
|
||||
@@ -66,10 +68,14 @@ func (rc *cartItemController) NewCartItemFromForm(ctx *gin.Context) (models.Cart
|
||||
return models.CartItem{}, err
|
||||
}
|
||||
|
||||
itemVariant, err := repositories.ShopItems.GetVariantById(itemVariantIdStr)
|
||||
|
||||
cartItem := models.CartItem{
|
||||
SessionId: sessionId,
|
||||
ShopItemId: uint(shopItemId),
|
||||
ShopItem: shopItem,
|
||||
ItemVariantId: uint(itemVariantId),
|
||||
ItemVariant: itemVariant,
|
||||
Quantity: quantity,
|
||||
}
|
||||
|
||||
@@ -155,7 +161,7 @@ func (rc *cartItemController) AddItemHandler(c *gin.Context) {
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.HTML(http.StatusBadRequest, "cart.html", gin.H{ "error": err })
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{ "error": err })
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
|
||||
name := ctx.PostForm("name")
|
||||
abstract := ctx.PostForm("abstract")
|
||||
description := ctx.PostForm("description")
|
||||
categoryStr := ctx.PostForm("category")
|
||||
variantNames := ctx.PostFormArray("variant-name[]")
|
||||
variantValues := ctx.PostFormArray("variant-value[]")
|
||||
tagIds := ctx.PostFormArray("tags[]")
|
||||
@@ -86,8 +87,15 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
|
||||
return models.ShopItem{}, fmt.Errorf("Name or description empty")
|
||||
}
|
||||
|
||||
category, err := models.ParseCategory(categoryStr)
|
||||
if err != nil {
|
||||
return models.ShopItem{}, err
|
||||
}
|
||||
|
||||
var variants []models.ItemVariant
|
||||
|
||||
fmt.Println("VariantNames: ", variantNames)
|
||||
fmt.Println("VariantValues: ", variantValues)
|
||||
if len(variantNames) != len(variantValues) {
|
||||
return models.ShopItem{}, fmt.Errorf("Different number of variant names and values")
|
||||
}
|
||||
@@ -114,6 +122,7 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
|
||||
Name: name,
|
||||
Abstract: abstract,
|
||||
Description: description,
|
||||
Category: category,
|
||||
IsPublic: true,
|
||||
Image: dst,
|
||||
Variants: variants,
|
||||
@@ -235,24 +244,7 @@ func (rc *shopItemController) AddItemView(c *gin.Context) {
|
||||
|
||||
|
||||
func (rc *shopItemController) AddItemHandler(c *gin.Context) {
|
||||
shopItem, err := rc.NewShopItemFromForm(c)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
|
||||
return
|
||||
}
|
||||
|
||||
tags, err := repositories.Tags.GetAll()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
|
||||
return
|
||||
}
|
||||
|
||||
_, err = repositories.ShopItems.Create(shopItem)
|
||||
if err != nil {
|
||||
errorHandler := func(err error, tags []models.Tag) {
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"error": err,
|
||||
"success": "",
|
||||
@@ -260,6 +252,23 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) {
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "additem.html", data)
|
||||
}
|
||||
|
||||
tags, err := repositories.Tags.GetAll()
|
||||
if err != nil {
|
||||
errorHandler(err, tags)
|
||||
return
|
||||
}
|
||||
|
||||
shopItem, err := rc.NewShopItemFromForm(c)
|
||||
if err != nil {
|
||||
errorHandler(err, tags)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = repositories.ShopItems.Create(shopItem)
|
||||
if err != nil {
|
||||
errorHandler(err, tags)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user