add get-breadcrumb endpoint
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package menuService
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sort"
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||
@@ -112,6 +113,69 @@ func (a ByPosition) Len() int { return len(a) }
|
||||
func (a ByPosition) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a ByPosition) Less(i, j int) bool { return a[i].Position < a[j].Position }
|
||||
|
||||
func (s *MenuService) GetBreadcrumb(root_category_id uint, start_category_id uint, id_lang uint) ([]model.CategoryInBreadcrumb, error) {
|
||||
all_categories, err := s.categoriesRepo.GetAllCategories(id_lang)
|
||||
if err != nil {
|
||||
return []model.CategoryInBreadcrumb{}, err
|
||||
}
|
||||
|
||||
breadcrumb := []model.CategoryInBreadcrumb{}
|
||||
|
||||
start_index := 0
|
||||
start_found := false
|
||||
for i := 0; i < len(all_categories); i++ {
|
||||
if all_categories[i].CategoryID == start_category_id {
|
||||
start_index = i
|
||||
start_found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !start_found {
|
||||
return []model.CategoryInBreadcrumb{}, responseErrors.ErrStartCategoryNotFound
|
||||
}
|
||||
|
||||
// map category ids to indices
|
||||
id_to_index := make(map[uint]int)
|
||||
for i := 0; i < len(all_categories); i++ {
|
||||
all_categories[i].Visited = false
|
||||
id_to_index[all_categories[i].CategoryID] = i
|
||||
}
|
||||
|
||||
// do a simple graph traversal, always jumping from node to its parent
|
||||
index := start_index
|
||||
success := true
|
||||
for {
|
||||
if all_categories[index].Visited {
|
||||
success = false
|
||||
break
|
||||
}
|
||||
all_categories[index].Visited = true
|
||||
|
||||
var next_category model.CategoryInBreadcrumb
|
||||
next_category.CategoryID = all_categories[index].CategoryID
|
||||
next_category.Name = all_categories[index].Name
|
||||
breadcrumb = append(breadcrumb, next_category)
|
||||
|
||||
if all_categories[index].CategoryID == root_category_id {
|
||||
break
|
||||
}
|
||||
next_index, ok := id_to_index[all_categories[index].ParentID]
|
||||
if !ok {
|
||||
success = false
|
||||
break
|
||||
}
|
||||
index = next_index
|
||||
}
|
||||
|
||||
slices.Reverse(breadcrumb)
|
||||
|
||||
if !success {
|
||||
return breadcrumb, responseErrors.ErrRootNeverReached
|
||||
}
|
||||
|
||||
return breadcrumb, nil
|
||||
}
|
||||
|
||||
func (s *MenuService) GetTopMenu(id uint) ([]*model.B2BTopMenu, error) {
|
||||
items, err := s.routesRepo.GetTopMenu(id)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user