add tagview
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
1
main.go
1
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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user