78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package categoryrepo
|
|
|
|
import (
|
|
"git.ma-al.com/goc_daniel/b2b/app/db"
|
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
|
"git.ma-al.com/goc_daniel/b2b/app/model/dbmodel"
|
|
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
|
)
|
|
|
|
type UICategoryRepo interface {
|
|
GetCategoryTranslations(ids []uint, idLang uint) (map[uint]string, error)
|
|
RetrieveMenuCategories(idLang uint) ([]model.ScannedCategory, error)
|
|
}
|
|
|
|
type CategoryRepo struct{}
|
|
|
|
func New() UICategoryRepo {
|
|
return &CategoryRepo{}
|
|
}
|
|
|
|
func (r *CategoryRepo) GetCategoryTranslations(ids []uint, idLang uint) (map[uint]string, error) {
|
|
if len(ids) == 0 {
|
|
return map[uint]string{}, nil
|
|
}
|
|
|
|
type result struct {
|
|
IDCategory int32
|
|
Name string
|
|
}
|
|
|
|
var results []result
|
|
if err := db.Get().
|
|
Model(dbmodel.PsCategoryLang{}).
|
|
Select("ps_category_lang.id_category, COALESCE(ps_category_lang.name, '') as name").
|
|
Where("ps_category_lang.id_category IN ?", ids).
|
|
Where("ps_category_lang.id_lang = ?", idLang).
|
|
Scan(&results).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
translations := make(map[uint]string, len(results))
|
|
for _, res := range results {
|
|
translations[uint(res.IDCategory)] = res.Name
|
|
}
|
|
|
|
return translations, nil
|
|
}
|
|
|
|
func (r *CategoryRepo) RetrieveMenuCategories(idLang uint) ([]model.ScannedCategory, error) {
|
|
var allCategories []model.ScannedCategory
|
|
|
|
categoryTbl := (&dbmodel.PsCategory{}).TableName()
|
|
categoryLangTbl := (&dbmodel.PsCategoryLang{}).TableName()
|
|
categoryShopTbl := (&dbmodel.PsCategoryShop{}).TableName()
|
|
langTbl := (&dbmodel.PsLang{}).TableName()
|
|
|
|
err := db.Get().
|
|
Model(dbmodel.PsCategory{}).
|
|
Select(`
|
|
ps_category.id_category AS category_id,
|
|
ps_category_lang.name AS name,
|
|
ps_category.active AS active,
|
|
ps_category_shop.position AS position,
|
|
ps_category.id_parent AS id_parent,
|
|
ps_category.is_root_category AS is_root_category,
|
|
ps_category_lang.link_rewrite AS link_rewrite,
|
|
ps_lang.iso_code AS iso_code
|
|
`).
|
|
Joins(`LEFT JOIN `+categoryLangTbl+` ON `+categoryLangTbl+`.id_category = `+categoryTbl+`.id_category AND `+categoryLangTbl+`.id_shop = ? AND `+categoryLangTbl+`.id_lang = ?`,
|
|
constdata.SHOP_ID, idLang).
|
|
Joins(`LEFT JOIN `+categoryShopTbl+` ON `+categoryShopTbl+`.id_category = `+categoryTbl+`.id_category AND `+categoryShopTbl+`.id_shop = ?`,
|
|
constdata.SHOP_ID).
|
|
Joins(`JOIN ` + langTbl + ` ON ` + langTbl + `.id_lang = ` + categoryLangTbl + `.id_lang`).
|
|
Scan(&allCategories).Error
|
|
|
|
return allCategories, err
|
|
}
|