Skip to content

Instantly share code, notes, and snippets.

@s4l1h
Created August 22, 2020 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s4l1h/b3fb07833e458938fee03108a52a3065 to your computer and use it in GitHub Desktop.
Save s4l1h/b3fb07833e458938fee03108a52a3065 to your computer and use it in GitHub Desktop.
package menu
type Menu struct {
gorm.Model
Name string `json:"Name" validate:"required,min=3,max=15" conform:"trim"`
//Desc string
URL string `json:"URL" conform:"trim"` //validate:"required"
Blank bool `json:"Blank"`
Root bool `json:"Root"`
ParentID uint `json:"ParentID"`
SortID uint `json:"SortID"`
RouteName string `json:"RouteName"`
TranslateKey string `json:"TranslateKey"`
ACLKey string `json:"ACLKey"`
ACLGroups system.SQLKeyValue `json:"Groups" sql:"type:jsonb not null default '{}'::jsonb"`
locale.LangModel
}
// TreeJSON model
type TreeJSON struct {
ID uint `json:"id"`
Text string `json:"text"`
Children []*TreeJSON `json:"children,omitempty"`
Attributes *Menu `json:"attributes,omitempty"`
}
// HaveChildren have
func (t *TreeJSON) HaveChildren() bool {
if len(t.Children) > 0 {
return true
}
return false
}
// GetChildren return childrens
func (t *TreeJSON) GetChildren() []*TreeJSON {
return t.Children
}
//
// JSON return datagrid json
func (control *Controller) json(c echo.Context) error {
menus := []*Menu{}
system.GetDB().Order("sort_id asc").Find(&menus)
//new := []*Menu{}
new := []*TreeJSON{}
add := make(map[uint]*TreeJSON)
for _, m := range menus {
j := &TreeJSON{
Text: m.Name,
ID: m.ID,
Attributes: m,
}
add[j.ID] = j
// if m.ID == 1 {
// // m.ParentID = null
// }
}
root := &TreeJSON{
Text: "Root", ID: 0,
Attributes: &Menu{Root: true},
}
add[0] = root
new = append(new, root)
for _, m := range menus {
// new = append(new, add[m.ID])
add[m.ParentID].Children = append(add[m.ParentID].Children, add[m.ID])
// if m.ParentID == 0 {
// new = append(new, add[m.ID])
// add[m.ParentID].Children = append(add[m.ParentID].Children, add[m.ID])
// } else {
// add[m.ParentID].Children = append(add[m.ParentID].Children, add[m.ID])
// }
}
//logrus.Warnf("Menus", menus)
return c.JSON(http.StatusOK, new)
}
func initMenu() {
name := "Menü Yönetimi"
ename := "Menu Manager"
mainID := GetNameID(name)
if mainID == 0 {
mainMenu := &Menu{Name: name, Root: true, ParentID: 1, TranslateKey: "menu.name", ACLKey: "menu.list"}
mainMenu.Langs = make(system.SQLKeyValue)
mainMenu.Langs["tr-TR"] = name
mainMenu.Langs["en-US"] = ename
system.GetDB().Create(mainMenu)
mainID = mainMenu.ID
}
name = "Menü Ekle"
ename = "Create Menu"
if GetNameID(name) == 0 {
subMenu := &Menu{Name: name, ParentID: mainID, TranslateKey: "menu.url.create", RouteName: "menu.create", ACLKey: "menu.create"}
subMenu.Langs = make(system.SQLKeyValue)
subMenu.Langs["tr-TR"] = name
subMenu.Langs["en-US"] = ename
system.GetDB().Create(subMenu)
}
name = "Menü Listesi"
ename = "Menu List"
if GetNameID(name) == 0 {
subMenu := &Menu{Name: name, ParentID: mainID, TranslateKey: "menu.url.list", RouteName: "menu.list", ACLKey: "menu.list"}
subMenu.Langs = make(system.SQLKeyValue)
subMenu.Langs["tr-TR"] = name
subMenu.Langs["en-US"] = ename
system.GetDB().Create(subMenu)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment