From 1ccfc620d0181c8166a0e474126e8674690490bd Mon Sep 17 00:00:00 2001 From: kalipso Date: Wed, 5 Mar 2025 10:39:58 +0100 Subject: [PATCH] variants --- controllers/cartItemController.go | 8 +++++- controllers/shopItemController.go | 45 ++++++++++++++++++------------ models/cart.go | 2 ++ models/shopItem.go | 16 ++++++++++- repositories/cartItemRepository.go | 6 ++-- repositories/shopItemRepository.go | 20 ++++++++++++- static/output.css | 22 ++++++++++++++- views/additem.html | 5 ++-- views/cart.html | 4 +-- views/shopitem.html | 15 ++++++---- 10 files changed, 108 insertions(+), 35 deletions(-) diff --git a/controllers/cartItemController.go b/controllers/cartItemController.go index ade4a84..c28690a 100644 --- a/controllers/cartItemController.go +++ b/controllers/cartItemController.go @@ -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 } diff --git a/controllers/shopItemController.go b/controllers/shopItemController.go index 7407760..7504558 100644 --- a/controllers/shopItemController.go +++ b/controllers/shopItemController.go @@ -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 } diff --git a/models/cart.go b/models/cart.go index 0aa8ded..b9bb46b 100644 --- a/models/cart.go +++ b/models/cart.go @@ -9,5 +9,7 @@ type CartItem struct { SessionId string `json:"sessionid" binding:"required" gorm:"not null"` ShopItemId uint ShopItem ShopItem `json:"shopitem" gorm:"foreignKey:ShopItemId"` //gorm one2one + ItemVariantId uint + ItemVariant ItemVariant `json:"itemvariant" gorm:"foreignKey:ItemVariantId"` //gorm one2one Quantity int `json:"quantity" binding:"required"` } diff --git a/models/shopItem.go b/models/shopItem.go index 9e04445..8537b33 100644 --- a/models/shopItem.go +++ b/models/shopItem.go @@ -1,6 +1,7 @@ package models import ( + "fmt" "gorm.io/gorm" ) @@ -14,6 +15,19 @@ Zines Books - name, abstr, descr, price, tag */ +type Category string + +const ( + Zine Category = "Zine" +) + +func ParseCategory(s string) (c Category, err error) { + if s == "Zine" { + return Zine, nil + } + + return c, fmt.Errorf("Cannot parse category %s", s) +} type ItemVariant struct { gorm.Model @@ -28,7 +42,7 @@ type ShopItem struct { Name string `json:"name" binding:"required" gorm:"unique;not null"` Abstract string `json:"Abstract" binding:"required"` Description string `json:"description" binding:"required"` - Category string `json:"category"` + Category Category `json:"category"` Variants []ItemVariant `json:"variant"` BasePrice float64 `json:"basePrice"` IsPublic bool `json:"isPublic" gorm:"default:true"` diff --git a/repositories/cartItemRepository.go b/repositories/cartItemRepository.go index f35162b..8439785 100644 --- a/repositories/cartItemRepository.go +++ b/repositories/cartItemRepository.go @@ -38,7 +38,7 @@ func (r *GORMCartItemRepository) Create(cartItem models.CartItem) (models.CartIt func (r *GORMCartItemRepository) GetAll() ([]models.CartItem, error) { var cartItems []models.CartItem - result := r.DB.Preload("ShopItem").Find(&cartItems) + result := r.DB.Preload("ShopItem").Preload("ItemVariant").Find(&cartItems) return cartItems, result.Error } @@ -51,7 +51,7 @@ func (t *GORMCartItemRepository) GetById(id string) (models.CartItem, error) { } var cartItem models.CartItem - result := t.DB.First(&cartItem, uint(cartItemId)) + result := t.DB.Preload("ShopItem").Preload("ItemVariant").First(&cartItem, uint(cartItemId)) if result.Error != nil { return models.CartItem{}, result.Error @@ -85,6 +85,6 @@ func (r *GORMCartItemRepository) DeleteById(id string) error { return err } - result := r.DB.Omit("ShopItem").Delete(&models.CartItem{}, cartItemId) + result := r.DB.Omit("ShopItem").Omit("ItemVariant").Delete(&models.CartItem{}, cartItemId) return result.Error } diff --git a/repositories/shopItemRepository.go b/repositories/shopItemRepository.go index d18f3d3..4a4b431 100644 --- a/repositories/shopItemRepository.go +++ b/repositories/shopItemRepository.go @@ -12,6 +12,7 @@ type ShopItemRepository interface { GetAll() ([]models.ShopItem, error) GetAllPublic() ([]models.ShopItem, error) GetById(string) (models.ShopItem, error) + GetVariantById(string) (models.ItemVariant, error) //GetByTagId(string) ([]models.ShopItem, error) Update(models.ShopItem) (models.ShopItem, error) DeleteById(string) error @@ -48,7 +49,6 @@ func (r *GORMShopItemRepository) GetAllPublic() ([]models.ShopItem, error) { result := r.DB.Preload("Tags").Preload("Variants").Where("is_public = 1").Find(&shopItems) return shopItems, result.Error - } func (r *GORMShopItemRepository) GetById(id string) (models.ShopItem, error) { @@ -68,6 +68,24 @@ func (r *GORMShopItemRepository) GetById(id string) (models.ShopItem, error) { return shopItem, nil } +func (r *GORMShopItemRepository) GetVariantById(id string) (models.ItemVariant, error) { + itemVariantId, err := strconv.Atoi(id) + + if err != nil { + return models.ItemVariant{}, err + } + + var itemVariant models.ItemVariant + result := r.DB.First(&itemVariant, uint(itemVariantId)) + + if result.Error != nil { + return models.ItemVariant{}, result.Error + } + + return itemVariant, nil +} + + func (r *GORMShopItemRepository) Update(shopItem models.ShopItem) (models.ShopItem, error) { err := r.DB.Model(&shopItem).Association("Tags").Replace(shopItem.Tags) if err != nil { diff --git a/static/output.css b/static/output.css index 86dfbdc..6acff7f 100644 --- a/static/output.css +++ b/static/output.css @@ -107,7 +107,7 @@ } /* -! tailwindcss v3.4.16 | MIT License | https://tailwindcss.com +! tailwindcss v3.4.17 | MIT License | https://tailwindcss.com */ /* @@ -927,6 +927,11 @@ video { background-color: rgb(209 213 219 / var(--tw-bg-opacity, 1)); } +.bg-gray-50 { + --tw-bg-opacity: 1; + background-color: rgb(249 250 251 / var(--tw-bg-opacity, 1)); +} + .bg-gray-800 { --tw-bg-opacity: 1; background-color: rgb(31 41 55 / var(--tw-bg-opacity, 1)); @@ -1639,6 +1644,16 @@ video { color: rgb(255 255 255 / var(--tw-text-opacity, 1)); } + .dark\:placeholder-gray-400::-moz-placeholder { + --tw-placeholder-opacity: 1; + color: rgb(156 163 175 / var(--tw-placeholder-opacity, 1)); + } + + .dark\:placeholder-gray-400::placeholder { + --tw-placeholder-opacity: 1; + color: rgb(156 163 175 / var(--tw-placeholder-opacity, 1)); + } + .dark\:hover\:bg-gray-600:hover { --tw-bg-opacity: 1; background-color: rgb(75 85 99 / var(--tw-bg-opacity, 1)); @@ -1653,4 +1668,9 @@ video { --tw-border-opacity: 1; border-color: rgb(59 130 246 / var(--tw-border-opacity, 1)); } + + .dark\:focus\:ring-blue-500:focus { + --tw-ring-opacity: 1; + --tw-ring-color: rgb(59 130 246 / var(--tw-ring-opacity, 1)); + } } diff --git a/views/additem.html b/views/additem.html index 7e17476..83a64e6 100644 --- a/views/additem.html +++ b/views/additem.html @@ -55,7 +55,8 @@ -->
- + +
@@ -67,7 +68,7 @@
- +
diff --git a/views/cart.html b/views/cart.html index ed582a3..3d0ba82 100644 --- a/views/cart.html +++ b/views/cart.html @@ -13,7 +13,7 @@
-
{{ .ShopItem.Name }}
+
{{ .ShopItem.Name }} - {{ .ItemVariant.Name}}
-
{{ .Quantity}} x {{ .ShopItem.Price }}€
+
{{ .Quantity}} x {{ .ItemVariant.Price }}€
diff --git a/views/shopitem.html b/views/shopitem.html index 2170a26..04b401f 100644 --- a/views/shopitem.html +++ b/views/shopitem.html @@ -1,14 +1,15 @@ {{ template "header.html" . }}
+
Product Image
-
+
@@ -24,20 +25,21 @@
{{ end }}
-

{{ .data.shopItem.Name }}

{{ .data.shopItem.Abstract }}

+
+ +
@@ -69,6 +71,7 @@
+