From fd46f35023912b8c0265908c6cae62ae50add658 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sun, 13 Apr 2025 23:32:38 +0200 Subject: [PATCH] add tagview --- controllers/userController.go | 11 ++++++++++- main.go | 1 + repositories/shopItemRepository.go | 30 +++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/controllers/userController.go b/controllers/userController.go index 802a7ce..ed4b1b7 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -213,7 +213,16 @@ func (rc *UserController) MainView(c *gin.Context) { "shopItems": shopItems, }) - fmt.Println(data) + c.HTML(http.StatusOK, "index.html", data) +} + +func (rc *UserController) TagView(c *gin.Context) { + shopItems, _ := repositories.ShopItems.GetByTagId(c.Param("id")) + + data := CreateSessionData(c, gin.H{ + "title": "shopItem Page", + "shopItems": shopItems, + }) c.HTML(http.StatusOK, "index.html", data) } diff --git a/main.go b/main.go index 73f9215..13680ba 100644 --- a/main.go +++ b/main.go @@ -88,6 +88,7 @@ func main() { viewRoutes.GET("/tags", authValidator.RequireAuth, shopItemController.TagView) viewRoutes.POST("/tags/:id", authValidator.RequireAuth, shopItemController.TagHandler) + viewRoutes.GET("/tags/:id", userController.TagView) viewRoutes.POST("/tags", authValidator.RequireAuth, shopItemController.AddTagHandler) viewRoutes.GET("/cart", cartItemController.CartItemView) viewRoutes.POST("/cart", cartItemController.AddItemHandler) diff --git a/repositories/shopItemRepository.go b/repositories/shopItemRepository.go index efd53dd..a787efb 100644 --- a/repositories/shopItemRepository.go +++ b/repositories/shopItemRepository.go @@ -1,19 +1,19 @@ package repositories -import( - "strconv" +import ( "gorm.io/gorm" + "strconv" "git.dynamicdiscord.de/kalipso/zineshop/models" -) +) type ShopItemRepository interface { - Create(models.ShopItem) (models.ShopItem, error) + Create(models.ShopItem) (models.ShopItem, error) GetAll() ([]models.ShopItem, error) GetAllPublic() ([]models.ShopItem, error) GetById(string) (models.ShopItem, error) + GetByTagId(string) ([]models.ShopItem, error) GetVariantById(string) (models.ItemVariant, error) - //GetByTagId(string) ([]models.ShopItem, error) Update(models.ShopItem) (models.ShopItem, error) DeleteById(string) error } @@ -31,7 +31,7 @@ func NewGORMShopItemRepository(db *gorm.DB) ShopItemRepository { func (r *GORMShopItemRepository) Create(shopItem models.ShopItem) (models.ShopItem, error) { result := r.DB.Create(&shopItem) if result.Error != nil { - return models.ShopItem{}, result.Error + return models.ShopItem{}, result.Error } return shopItem, nil @@ -68,6 +68,23 @@ func (r *GORMShopItemRepository) GetById(id string) (models.ShopItem, error) { return shopItem, nil } +func (r *GORMShopItemRepository) GetByTagId(id string) ([]models.ShopItem, error) { + tagId, err := strconv.Atoi(id) + + if err != nil { + return nil, err + } + + var shopItems []models.ShopItem + result := r.DB.Joins("JOIN item_tags ON item_tags.shop_item_id = shop_items.id").Where("item_tags.tag_id = ?", tagId).Preload("Tags").Preload("Variants").Find(&shopItems) + + if result.Error != nil { + return nil, result.Error + } + + return shopItems, nil +} + func (r *GORMShopItemRepository) GetVariantById(id string) (models.ItemVariant, error) { itemVariantId, err := strconv.Atoi(id) @@ -85,7 +102,6 @@ func (r *GORMShopItemRepository) GetVariantById(id string) (models.ItemVariant, 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 {