add tagview
This commit is contained in:
@@ -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