add top menu

This commit is contained in:
2026-03-26 13:22:10 +01:00
parent ca27cbea1c
commit c09e525736
10 changed files with 403 additions and 190 deletions

View File

@@ -1236,6 +1236,51 @@
}
}
},
"/api/v1/restricted/menu/get-top-menu": {
"get": {
"tags": ["Menu"],
"summary": "Get top menu",
"description": "Returns the top-level menu items for the current language. Requires authentication.",
"operationId": "getTopMenu",
"security": [
{
"CookieAuth": []
}
],
"responses": {
"200": {
"description": "Top menu retrieved successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ApiResponse"
}
}
}
},
"400": {
"description": "Invalid request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "Not authenticated",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
},
"/api/v1/restricted/meili-search/search": {
"get": {
"tags": ["Search"],

View File

@@ -25,6 +25,7 @@ func MenuHandlerRoutes(r fiber.Router) fiber.Router {
r.Get("/get-menu", handler.GetMenu)
r.Get("/get-routes", handler.GetRouting)
r.Get("/get-top-menu", handler.GetTopMenu)
return r
}
@@ -58,3 +59,18 @@ func (h *MenuHandler) GetRouting(c fiber.Ctx) error {
return c.JSON(response.Make(&menu, 0, i18n.T_(c, response.Message_OK)))
}
func (h *MenuHandler) GetTopMenu(c fiber.Ctx) error {
lang_id, ok := c.Locals("langID").(uint)
if !ok {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
menu, err := h.menuService.GetTopMenu(lang_id)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&menu, len(menu), i18n.T_(c, response.Message_OK)))
}

17
app/model/topMenu.go Normal file
View File

@@ -0,0 +1,17 @@
package model
type B2BTopMenu struct {
MenuID int `gorm:"column:menu_id;primaryKey;autoIncrement"`
Label string `gorm:"column:label;type:longtext;not null;default:'{}'"`
ParentID *int `gorm:"column:parent_id;index:FK_b2b_top_menu_parent_id"`
Params string `gorm:"column:params;type:longtext;not null;default:'{}'"`
Active int8 `gorm:"column:active;type:tinyint;not null;default:1"`
Position int `gorm:"column:position;not null;default:1"`
Parent *B2BTopMenu `gorm:"foreignKey:ParentID;references:MenuID;constraint:OnDelete:RESTRICT,OnUpdate:RESTRICT"`
Children []B2BTopMenu `gorm:"foreignKey:ParentID"`
}
func (B2BTopMenu) TableName() string {
return "b2b_top_menu"
}

View File

@@ -7,6 +7,7 @@ import (
type UIRoutesRepo interface {
GetRoutes(langId uint) ([]model.Route, error)
GetTopMenu(id uint) ([]model.B2BTopMenu, error)
}
type RoutesRepo struct{}
@@ -23,3 +24,14 @@ func (p *RoutesRepo) GetRoutes(langId uint) ([]model.Route, error) {
}
return routes, nil
}
func (p *RoutesRepo) GetTopMenu(id uint) ([]model.B2BTopMenu, error) {
var menus []model.B2BTopMenu
err := db.Get().
Where("active = ?", 1).
Order("parent_id ASC, position ASC").
Find(&menus).Error
return menus, err
}

View File

@@ -3,12 +3,9 @@ package menuService
import (
"sort"
"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/prestadb"
"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/jsonprint"
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
)
@@ -77,17 +74,6 @@ func (s *MenuService) createTree(index int, all_categories *([]model.ScannedCate
}
func (s *MenuService) GetRoutes(id_lang uint) ([]model.Route, error) {
type XX struct {
prestadb.PsProduct
// PsProductLang prestadb.PsProductLang `gorm:"foreignKey:IDProduct;references:IDProduct"`
// PSProductAttribute prestadb.PsProductAttribute `gorm:"foreignKey:IDProduct;references:IDProduct"`
}
product := XX{}
db.Get().Debug().Find(&product, prestadb.PsCategoryProduct{IDProduct: 53})
// fmt.Printf("%v\n nnnnnnnn", product)
jsonprint.Print(product)
return s.routesRepo.GetRoutes(id_lang)
}
@@ -111,3 +97,34 @@ type ByPosition []ChildWithPosition
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) GetTopMenu(id uint) ([]model.B2BTopMenu, error) {
menuMap := make(map[int]*model.B2BTopMenu)
var roots []model.B2BTopMenu
items, err := s.routesRepo.GetTopMenu(id)
if err != nil {
return nil, err
}
// Build the map first
for i := range items {
items[i].Children = []model.B2BTopMenu{}
menuMap[items[i].MenuID] = &items[i]
}
// Then link children to parents
for _, item := range menuMap {
if item.ParentID != nil && *item.ParentID != 0 {
parent, exists := menuMap[*item.ParentID]
if exists {
parent.Children = append(parent.Children, *item)
}
} else {
// This is a root item
roots = append(roots, *item)
}
}
return roots, nil
}