This commit is contained in:
2026-03-23 14:04:53 +01:00
parent 528f12b065
commit 0f494d3abd
21 changed files with 515 additions and 264 deletions

View File

@@ -5,23 +5,26 @@ import (
"git.ma-al.com/goc_daniel/b2b/app/model"
"git.ma-al.com/goc_daniel/b2b/app/repos/categoriesRepo"
routesRepo "git.ma-al.com/goc_daniel/b2b/app/repos/routesRepo"
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
)
type MenuService struct {
categoriesRepo categoriesRepo.UICategoriesRepo
routesRepo routesRepo.UIRoutesRepo
}
func New() *MenuService {
return &MenuService{
categoriesRepo: categoriesRepo.New(),
routesRepo: routesRepo.New(),
}
}
func (s *MenuService) GetMenu(id_lang uint) (model.Category, error) {
func (s *MenuService) GetMenu(id_lang uint) (*model.Category, error) {
all_categories, err := s.categoriesRepo.GetAllCategories(id_lang)
if err != nil {
return model.Category{}, err
return &model.Category{}, err
}
// find the root
@@ -35,7 +38,7 @@ func (s *MenuService) GetMenu(id_lang uint) (model.Category, error) {
}
}
if !root_found {
return model.Category{}, responseErrors.ErrNoRootFound
return &model.Category{}, responseErrors.ErrNoRootFound
}
// now create the children and reorder them according to position
@@ -57,25 +60,31 @@ func (s *MenuService) GetMenu(id_lang uint) (model.Category, error) {
// finally, create the tree
tree := s.createTree(root_index, &all_categories, &children_indices)
return tree, nil
return &tree, nil
}
func (s *MenuService) createTree(index int, all_categories *([]model.ScannedCategory), children_indices *(map[int][]ChildWithPosition)) model.Category {
node := s.scannedToNormalCategory((*all_categories)[index])
for i := 0; i < len((*children_indices)[index]); i++ {
node.Subcategories = append(node.Subcategories, s.createTree((*children_indices)[index][i].Index, all_categories, children_indices))
node.Children = append(node.Children, s.createTree((*children_indices)[index][i].Index, all_categories, children_indices))
}
return node
}
func (s *MenuService) GetRoutes(id_lang uint) ([]model.Route, error) {
return s.routesRepo.GetRoutes(id_lang)
}
func (s *MenuService) scannedToNormalCategory(scanned model.ScannedCategory) model.Category {
var normal model.Category
normal.Active = scanned.Active
// normal.Active = scanned.Active
normal.CategoryID = scanned.CategoryID
normal.Name = scanned.Name
normal.Subcategories = []model.Category{}
normal.Label = scanned.Name
// normal.Active = scanned.Active == 1
normal.Params = model.CategpryParams{CategoryID: normal.CategoryID, LinkRewrite: scanned.LinkRewrite, Locale: scanned.IsoCode}
normal.Children = []model.Category{}
return normal
}