diff --git a/controllers/cartItemController.go b/controllers/cartItemController.go index e5dc767..ade4a84 100644 --- a/controllers/cartItemController.go +++ b/controllers/cartItemController.go @@ -137,7 +137,7 @@ func (rc *cartItemController) CartItemView(c *gin.Context) { priceTotal := 0.0 for _, cartItem := range cartItems { - priceTotal += (float64(cartItem.Quantity) * cartItem.ShopItem.Price) + priceTotal += (float64(cartItem.Quantity) * cartItem.ShopItem.BasePrice) } fmt.Println("PRICE TOTAL", priceTotal) diff --git a/controllers/shopItemController.go b/controllers/shopItemController.go index 341f984..7407760 100644 --- a/controllers/shopItemController.go +++ b/controllers/shopItemController.go @@ -69,7 +69,8 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop name := ctx.PostForm("name") abstract := ctx.PostForm("abstract") description := ctx.PostForm("description") - priceStr := ctx.PostForm("price") + variantNames := ctx.PostFormArray("variant-name[]") + variantValues := ctx.PostFormArray("variant-value[]") tagIds := ctx.PostFormArray("tags[]") image, err := ctx.FormFile("image") dst := "static/img/zine.jpg" @@ -85,19 +86,37 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop return models.ShopItem{}, fmt.Errorf("Name or description empty") } + var variants []models.ItemVariant + + if len(variantNames) != len(variantValues) { + return models.ShopItem{}, fmt.Errorf("Different number of variant names and values") + } + + for idx := range variantNames { + if variantValues[idx] == "" || variantNames[idx] == "" { + continue + } + + price, err := strconv.ParseFloat(variantValues[idx], 64) + if err != nil { + return models.ShopItem{}, fmt.Errorf("Could not variant parse price") + } + + variants = append(variants, models.ItemVariant{ + Name: variantNames[idx], + Price: price, + }) + } + // 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, Image: dst, + Variants: variants, } for _, tagId := range tagIds { @@ -293,9 +312,10 @@ func (rc *shopItemController) EditItemHandler(c *gin.Context) { newShopItem.Name = shopItem.Name newShopItem.Abstract = shopItem.Abstract newShopItem.Description = shopItem.Description - newShopItem.Price = shopItem.Price + newShopItem.BasePrice = shopItem.BasePrice newShopItem.IsPublic = shopItem.IsPublic newShopItem.Tags = shopItem.Tags + newShopItem.Variants = shopItem.Variants tags, err := repositories.Tags.GetAll() if err != nil { diff --git a/models/shopItem.go b/models/shopItem.go index 6f8e616..9e04445 100644 --- a/models/shopItem.go +++ b/models/shopItem.go @@ -4,14 +4,34 @@ import ( "gorm.io/gorm" ) +/* +Sticker + - name, abstr, descr, price, tag +Poster + - name, abstr, descr, price bw/colored, tag +Zines + - name, abstr, descr, price bw/colored/coloredcoveronly, tag +Books + - name, abstr, descr, price, tag +*/ + +type ItemVariant struct { + gorm.Model + Name string `json:"name" gorm:"not null"` + Price float64 `json:"price" gorm:"not null"` + InStock bool `json:"inStock" gorm:"default:true"` + ShopItemID uint +} + type ShopItem struct { gorm.Model Name string `json:"name" binding:"required" gorm:"unique;not null"` Abstract string `json:"Abstract" binding:"required"` Description string `json:"description" binding:"required"` - Price float64 `json:"price" binding:"required"` + Category string `json:"category"` + Variants []ItemVariant `json:"variant"` + BasePrice float64 `json:"basePrice"` IsPublic bool `json:"isPublic" gorm:"default:true"` Tags []Tag `gorm:"many2many:item_tags;"` Image string } - diff --git a/repositories/repository.go b/repositories/repository.go index 8f9ffed..3194fcc 100644 --- a/repositories/repository.go +++ b/repositories/repository.go @@ -21,7 +21,7 @@ func InitRepositories() { panic("failed to connect to database") } - err = db.AutoMigrate(&models.ShopItem{}, &models.User{}, &models.Tag{}, &models.CartItem{}) + err = db.AutoMigrate(&models.ShopItem{}, &models.ItemVariant{}, &models.User{}, &models.Tag{}, &models.CartItem{}) if err != nil { panic("failed to migrate database") } diff --git a/repositories/shopItemRepository.go b/repositories/shopItemRepository.go index 37f419a..d18f3d3 100644 --- a/repositories/shopItemRepository.go +++ b/repositories/shopItemRepository.go @@ -38,14 +38,14 @@ func (r *GORMShopItemRepository) Create(shopItem models.ShopItem) (models.ShopIt func (r *GORMShopItemRepository) GetAll() ([]models.ShopItem, error) { var shopItems []models.ShopItem - result := r.DB.Preload("Tags").Find(&shopItems) + result := r.DB.Preload("Tags").Preload("Variants").Find(&shopItems) return shopItems, result.Error } func (r *GORMShopItemRepository) GetAllPublic() ([]models.ShopItem, error) { var shopItems []models.ShopItem - result := r.DB.Preload("Tags").Where("is_public = 1").Find(&shopItems) + result := r.DB.Preload("Tags").Preload("Variants").Where("is_public = 1").Find(&shopItems) return shopItems, result.Error @@ -59,7 +59,7 @@ func (r *GORMShopItemRepository) GetById(id string) (models.ShopItem, error) { } var shopItem models.ShopItem - result := r.DB.Preload("Tags").First(&shopItem, uint(shopItemId)) + result := r.DB.Preload("Tags").Preload("Variants").First(&shopItem, uint(shopItemId)) if result.Error != nil { return models.ShopItem{}, result.Error @@ -74,6 +74,11 @@ func (r *GORMShopItemRepository) Update(shopItem models.ShopItem) (models.ShopIt return models.ShopItem{}, err } + err = r.DB.Model(&shopItem).Association("Variants").Replace(shopItem.Variants) + if err != nil { + return models.ShopItem{}, err + } + result := r.DB.Save(&shopItem) if result.Error != nil { return models.ShopItem{}, result.Error diff --git a/services/shopItemService.go b/services/shopItemService.go index d1618c4..0355460 100644 --- a/services/shopItemService.go +++ b/services/shopItemService.go @@ -18,7 +18,7 @@ func (u *ShopItemService) NewShopItem(name string, abstract string, description Name: name, Abstract: abstract, Description: description, - Price: price, + BasePrice: price, IsPublic: true, } diff --git a/views/additem.html b/views/additem.html index d6abfb2..7e17476 100644 --- a/views/additem.html +++ b/views/additem.html @@ -55,14 +55,28 @@ -->
+
- +
- +
+ + +
+ +
+ +
+
+ +
+
+ +
+
+ +

+ + +

+ +

Options

+
+ + + +

+
+ +
+ + + +

+
+ +
+ + + +

+
+ + +
+ {{ template "footer.html" . }} diff --git a/views/shopitem.html b/views/shopitem.html index b85b124..2170a26 100644 --- a/views/shopitem.html +++ b/views/shopitem.html @@ -32,10 +32,12 @@ {{ .data.shopItem.Abstract }}

+ {{ range .data.shopItem.Variants }}
- Price: - {{ .data.shopItem.Price }}€ + {{ .Name }}: + {{ .Price }}€
+ {{ end }}
diff --git a/views/shopitems.html b/views/shopitems.html index 652a349..4a4f083 100644 --- a/views/shopitems.html +++ b/views/shopitems.html @@ -17,7 +17,7 @@
-

{{ .Price }}€

+

{{ .BasePrice }}€