fix top menu
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
type B2BTopMenu struct {
|
type B2BTopMenu struct {
|
||||||
MenuID int `gorm:"column:menu_id;primaryKey;autoIncrement"`
|
MenuID int `gorm:"column:menu_id;primaryKey;autoIncrement" json:"menu_id"`
|
||||||
Label string `gorm:"column:label;type:longtext;not null;default:'{}'"`
|
Label string `gorm:"column:label;type:longtext;not null;default:'{}'" json:"label"`
|
||||||
ParentID *int `gorm:"column:parent_id;index:FK_b2b_top_menu_parent_id"`
|
ParentID *int `gorm:"column:parent_id;index:FK_b2b_top_menu_parent_id" json:"parent_id,omitempty"`
|
||||||
Params string `gorm:"column:params;type:longtext;not null;default:'{}'"`
|
Params string `gorm:"column:params;type:longtext;not null;default:'{}'" json:"params"`
|
||||||
Active int8 `gorm:"column:active;type:tinyint;not null;default:1"`
|
Active int8 `gorm:"column:active;type:tinyint;not null;default:1" json:"active"`
|
||||||
Position int `gorm:"column:position;not null;default:1"`
|
Position int `gorm:"column:position;not null;default:1" json:"position"`
|
||||||
|
|
||||||
Parent *B2BTopMenu `gorm:"foreignKey:ParentID;references:MenuID;constraint:OnDelete:RESTRICT,OnUpdate:RESTRICT"`
|
Parent *B2BTopMenu `gorm:"foreignKey:ParentID;references:MenuID;constraint:OnDelete:RESTRICT,OnUpdate:RESTRICT" json:"parent,omitempty"`
|
||||||
Children []B2BTopMenu `gorm:"foreignKey:ParentID"`
|
Children []B2BTopMenu `gorm:"foreignKey:ParentID" json:"children,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (B2BTopMenu) TableName() string {
|
func (B2BTopMenu) TableName() string {
|
||||||
|
|||||||
@@ -99,32 +99,40 @@ 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 (a ByPosition) Less(i, j int) bool { return a[i].Position < a[j].Position }
|
||||||
|
|
||||||
func (s *MenuService) GetTopMenu(id uint) ([]model.B2BTopMenu, error) {
|
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)
|
items, err := s.routesRepo.GetTopMenu(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the map first
|
if len(items) == 0 {
|
||||||
for i := range items {
|
return []model.B2BTopMenu{}, nil
|
||||||
items[i].Children = []model.B2BTopMenu{}
|
|
||||||
menuMap[items[i].MenuID] = &items[i]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then link children to parents
|
// Build a map with empty children slices
|
||||||
for _, item := range menuMap {
|
itemMap := make(map[int]model.B2BTopMenu, len(items))
|
||||||
if item.ParentID != nil && *item.ParentID != 0 {
|
for i := range items {
|
||||||
parent, exists := menuMap[*item.ParentID]
|
items[i].Children = []model.B2BTopMenu{}
|
||||||
if exists {
|
itemMap[items[i].MenuID] = items[i]
|
||||||
parent.Children = append(parent.Children, *item)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build the tree
|
||||||
|
var roots []model.B2BTopMenu
|
||||||
|
for _, item := range itemMap {
|
||||||
|
if item.ParentID == nil || *item.ParentID == 0 {
|
||||||
|
roots = append(roots, itemMap[item.MenuID])
|
||||||
} else {
|
} else {
|
||||||
// This is a root item
|
parentID := *item.ParentID
|
||||||
roots = append(roots, *item)
|
if parent, exists := itemMap[parentID]; exists {
|
||||||
|
parent.Children = append(parent.Children, item)
|
||||||
|
itemMap[parentID] = parent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update roots with children
|
||||||
|
for i := range roots {
|
||||||
|
roots[i] = itemMap[roots[i].MenuID]
|
||||||
|
}
|
||||||
|
|
||||||
return roots, nil
|
return roots, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,192 +46,28 @@ CREATE TABLE IF NOT EXISTS b2b_top_menu (
|
|||||||
INSERT IGNORE INTO `b2b_top_menu` (`menu_id`, `label`, `parent_id`, `params`)
|
INSERT IGNORE INTO `b2b_top_menu` (`menu_id`, `label`, `parent_id`, `params`)
|
||||||
VALUES
|
VALUES
|
||||||
-- ROOT
|
-- ROOT
|
||||||
(
|
(1, JSON_COMPACT('{"name":"root","trans":{"pl":"Menu główne","en":"Main Menu","de":"Hauptmenü"}}'), NULL, '{}'),
|
||||||
1,
|
|
||||||
'{
|
|
||||||
"name": "root",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Menu główne",
|
|
||||||
"en": "Main Menu",
|
|
||||||
"de": "Hauptmenü"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
NULL,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
-- LEVEL 1
|
-- LEVEL 1
|
||||||
(
|
(2, JSON_COMPACT('{"name":"dashboard","trans":{"pl":"Panel","en":"Dashboard","de":"Dashboard"}}'), 1, '{}'),
|
||||||
2,
|
(3, JSON_COMPACT('{"name":"orders","trans":{"pl":"Zamówienia","en":"Orders","de":"Bestellungen"}}'), 1, '{}'),
|
||||||
'{
|
(4, JSON_COMPACT('{"name":"customers","trans":{"pl":"Klienci","en":"Customers","de":"Kunden"}}'), 1, '{}'),
|
||||||
"name": "dashboard",
|
(5, JSON_COMPACT('{"name":"products","trans":{"pl":"Produkty","en":"Products","de":"Produkte"}}'), 1, '{}'),
|
||||||
"trans": {
|
(6, JSON_COMPACT('{"name":"reports","trans":{"pl":"Raporty","en":"Reports","de":"Berichte"}}'), 1, '{}'),
|
||||||
"pl": "Panel",
|
|
||||||
"en": "Dashboard",
|
|
||||||
"de": "Dashboard"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
1,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
3,
|
|
||||||
'{
|
|
||||||
"name": "orders",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Zamówienia",
|
|
||||||
"en": "Orders",
|
|
||||||
"de": "Bestellungen"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
1,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
4,
|
|
||||||
'{
|
|
||||||
"name": "customers",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Klienci",
|
|
||||||
"en": "Customers",
|
|
||||||
"de": "Kunden"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
1,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
5,
|
|
||||||
'{
|
|
||||||
"name": "products",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Produkty",
|
|
||||||
"en": "Products",
|
|
||||||
"de": "Produkte"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
1,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
6,
|
|
||||||
'{
|
|
||||||
"name": "reports",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Raporty",
|
|
||||||
"en": "Reports",
|
|
||||||
"de": "Berichte"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
1,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
-- LEVEL 2 (Orders)
|
-- LEVEL 2 (Orders)
|
||||||
(
|
(7, JSON_COMPACT('{"name":"order_list","trans":{"pl":"Lista zamówień","en":"Order List","de":"Bestellliste"}}'), 3, '{}'),
|
||||||
7,
|
(8, JSON_COMPACT('{"name":"pending_orders","trans":{"pl":"Oczekujące zamówienia","en":"Pending Orders","de":"Ausstehende Bestellungen"}}'), 3, '{}'),
|
||||||
'{
|
(9, JSON_COMPACT('{"name":"carts","trans":{"pl":"Koszyki","en":"Carts","de":"Warenkörbe"}}'), 3, '{}'),
|
||||||
"name": "order_list",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Lista zamówień",
|
|
||||||
"en": "Order List",
|
|
||||||
"de": "Bestellliste"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
3,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
8,
|
|
||||||
'{
|
|
||||||
"name": "pending_orders",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Oczekujące zamówienia",
|
|
||||||
"en": "Pending Orders",
|
|
||||||
"de": "Ausstehende Bestellungen"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
3,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
9,
|
|
||||||
'{
|
|
||||||
"name": "carts",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Koszyki",
|
|
||||||
"en": "Carts",
|
|
||||||
"de": "Warenkörbe"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
3,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
-- LEVEL 2 (Products)
|
-- LEVEL 2 (Products)
|
||||||
(
|
(10, JSON_COMPACT('{"name":"product_list","trans":{"pl":"Lista produktów","en":"Product List","de":"Produktliste"}}'), 5, '{}'),
|
||||||
10,
|
(11, JSON_COMPACT('{"name":"categories","trans":{"pl":"Kategorie","en":"Categories","de":"Kategorien"}}'), 5, '{}'),
|
||||||
'{
|
(12, JSON_COMPACT('{"name":"inventory","trans":{"pl":"Magazyn","en":"Inventory","de":"Lagerbestand"}}'), 5, '{}'),
|
||||||
"name": "product_list",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Lista produktów",
|
|
||||||
"en": "Product List",
|
|
||||||
"de": "Produktliste"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
5,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
11,
|
|
||||||
'{
|
|
||||||
"name": "categories",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Kategorie",
|
|
||||||
"en": "Categories",
|
|
||||||
"de": "Kategorien"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
5,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
12,
|
|
||||||
'{
|
|
||||||
"name": "inventory",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Magazyn",
|
|
||||||
"en": "Inventory",
|
|
||||||
"de": "Lagerbestand"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
5,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
-- LEVEL 2 (Customers)
|
-- LEVEL 2 (Customers)
|
||||||
(
|
(13, JSON_COMPACT('{"name":"customer_list","trans":{"pl":"Lista klientów","en":"Customer List","de":"Kundenliste"}}'), 4, '{}'),
|
||||||
13,
|
(14, JSON_COMPACT('{"name":"customer_groups","trans":{"pl":"Grupy klientów","en":"Customer Groups","de":"Kundengruppen"}}'), 4, '{}');
|
||||||
'{
|
|
||||||
"name": "customer_list",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Lista klientów",
|
|
||||||
"en": "Customer List",
|
|
||||||
"de": "Kundenliste"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
4,
|
|
||||||
'{}'
|
|
||||||
),
|
|
||||||
(
|
|
||||||
14,
|
|
||||||
'{
|
|
||||||
"name": "customer_groups",
|
|
||||||
"trans": {
|
|
||||||
"pl": "Grupy klientów",
|
|
||||||
"en": "Customer Groups",
|
|
||||||
"de": "Kundengruppen"
|
|
||||||
}
|
|
||||||
}',
|
|
||||||
4,
|
|
||||||
'{}'
|
|
||||||
);
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
|
|
||||||
DROP TABLE IF EXISTS b2b_routes;
|
DROP TABLE IF EXISTS b2b_routes;
|
||||||
|
|||||||
Reference in New Issue
Block a user