This commit is contained in:
2025-03-05 10:39:58 +01:00
parent a65ba9c98c
commit 1ccfc620d0
10 changed files with 108 additions and 35 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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"`

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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));
}
}

View File

@@ -55,7 +55,8 @@
-->
<div>
<input type="hidden" id="variant-name1" name="varian-name[]" value="B/W" required>
<input type="hidden" name="category" value="Zine" required>
<input type="hidden" id="variant-name1" name="variant-name[]" value="B/W" required>
<div class="flex items-center justify-between">
<label for="variant-value1" class="block text-sm/6 font-medium text-gray-900">Price B/W</label>
</div>
@@ -67,7 +68,7 @@
<div>
<input type="hidden" id="variant-name2" name="varian-name[]" value="Colored" required>
<input type="hidden" id="variant-name2" name="variant-name[]" value="Colored" required>
<div class="flex items-center justify-between">
<label for="variant-value2" class="block text-sm/6 font-medium text-gray-900">Price Colored</label>
</div>

View File

@@ -13,7 +13,7 @@
</div>
<div class="col-span-12 lg:col-span-10 detail w-full lg:pl-3">
<div class="flex items-center justify-between w-full mb-4">
<h5 class="font-manrope font-bold text-2xl leading-9 text-gray-900">{{ .ShopItem.Name }}</h5>
<h5 class="font-manrope font-bold text-2xl leading-9 text-gray-900">{{ .ShopItem.Name }} - {{ .ItemVariant.Name}}</h5>
<form action="/cart/delete" method="POST">
<input type="hidden" id="{{ .ID }}" name="id" value="{{ .ID }}">
<button type="submit" class="rounded-full group flex items-center justify-center focus-within:outline-red-500">
@@ -55,7 +55,7 @@
</svg>
</button>
</div>
<h6 class="text-indigo-600 font-manrope font-bold text-2xl leading-9 text-right">{{ .Quantity}} x {{ .ShopItem.Price }}€</h6>
<h6 class="text-indigo-600 font-manrope font-bold text-2xl leading-9 text-right">{{ .Quantity}} x {{ .ItemVariant.Price }}€</h6>
</div>
</form>
</div>

View File

@@ -1,14 +1,15 @@
{{ template "header.html" . }}
<div class="bg-gray-100 dark:bg-gray-800 py-8">
<form action="/cart" method="POST">
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex flex-col md:flex-row -mx-4">
<div class="md:flex-1 px-4">
<div class="h-[460px] rounded-lg bg-gray-300 dark:bg-gray-700 mb-4">
<img class="w-full h-full object-cover" src="/{{ .data.shopItem.Image}}" alt="Product Image">
</div>
<form action="/cart" method="POST">
<div class="flex -mx-2 mb-4">
<input type="hidden" id="{{ .data.shopItem.ID}}" name="ShopItemId" value="{{ .data.shopItem.ID }}">
<div class="w-1/3 px-2">
<button type="submit" class="w-full bg-gray-900 dark:bg-gray-600 text-white py-2 px-4 rounded-full font-bold hover:bg-gray-800 dark:hover:bg-gray-700">Add to Cart</button>
@@ -24,20 +25,21 @@
</div>
{{ end }}
</div>
</form>
</div>
<div class="md:flex-1 px-4">
<h2 class="text-2xl font-bold text-gray-800 dark:text-white mb-2">{{ .data.shopItem.Name }}</h2>
<p class="text-gray-600 dark:text-gray-300 text-sm mb-4">
{{ .data.shopItem.Abstract }}
</p>
<div class="flex mb-4">
<label for="ItemVariantId" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"></label>
<select name="ItemVariantId" id="ItemVariantId" required class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option selected value="">Choose a variant</option>
{{ range .data.shopItem.Variants }}
<div class="mr-4">
<span class="font-bold text-gray-700 dark:text-gray-300">{{ .Name }}:</span>
<span class="text-gray-600 dark:text-gray-300">{{ .Price }}€</span>
</div>
<option value="{{ .ID }}">{{ .Name }} - {{ .Price }}€</option>
{{ end }}
</select>
</div>
<div class="flex mb-4">
@@ -69,6 +71,7 @@
</div>
</div>
</div>
</form>
</div>