add tags
This commit is contained in:
@@ -11,6 +11,7 @@ import(
|
||||
var(
|
||||
ShopItems ShopItemRepository
|
||||
Users UserRepository
|
||||
Tags TagRepository
|
||||
)
|
||||
|
||||
func InitRepositories() {
|
||||
@@ -19,11 +20,12 @@ func InitRepositories() {
|
||||
panic("failed to connect to database")
|
||||
}
|
||||
|
||||
err = db.AutoMigrate(&models.ShopItem{}, &models.User{}, &models.Booking{})
|
||||
err = db.AutoMigrate(&models.ShopItem{}, &models.User{}, &models.Tag{})
|
||||
if err != nil {
|
||||
panic("failed to migrate database")
|
||||
}
|
||||
|
||||
ShopItems = NewGORMShopItemRepository(db)
|
||||
Users = NewGORMUserRepository(db)
|
||||
Tags = NewGORMTagRepository(db)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ type ShopItemRepository interface {
|
||||
GetAll() ([]models.ShopItem, error)
|
||||
GetAllPublic() ([]models.ShopItem, error)
|
||||
GetById(string) (models.ShopItem, error)
|
||||
//GetByTagId(string) ([]models.ShopItem, error)
|
||||
Update(models.ShopItem) (models.ShopItem, error)
|
||||
DeleteById(string) error
|
||||
}
|
||||
@@ -37,14 +38,14 @@ func (r *GORMShopItemRepository) Create(shopItem models.ShopItem) (models.ShopIt
|
||||
|
||||
func (r *GORMShopItemRepository) GetAll() ([]models.ShopItem, error) {
|
||||
var shopItems []models.ShopItem
|
||||
result := r.DB.Find(&shopItems)
|
||||
result := r.DB.Preload("Tags").Find(&shopItems)
|
||||
|
||||
return shopItems, result.Error
|
||||
}
|
||||
|
||||
func (r *GORMShopItemRepository) GetAllPublic() ([]models.ShopItem, error) {
|
||||
var shopItems []models.ShopItem
|
||||
result := r.DB.Where("is_public = 1").Find(&shopItems)
|
||||
result := r.DB.Preload("Tags").Where("is_public = 1").Find(&shopItems)
|
||||
|
||||
return shopItems, result.Error
|
||||
|
||||
@@ -58,7 +59,7 @@ func (r *GORMShopItemRepository) GetById(id string) (models.ShopItem, error) {
|
||||
}
|
||||
|
||||
var shopItem models.ShopItem
|
||||
result := r.DB.First(&shopItem, uint(shopItemId))
|
||||
result := r.DB.Preload("Tags").First(&shopItem, uint(shopItemId))
|
||||
|
||||
if result.Error != nil {
|
||||
return models.ShopItem{}, result.Error
|
||||
|
||||
Reference in New Issue
Block a user