2018-05-11 05:41:51 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/query_gen"
|
2018-05-11 05:41:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Menus *DefaultMenuStore
|
|
|
|
|
|
|
|
type DefaultMenuStore struct {
|
|
|
|
menus map[int]*atomic.Value
|
|
|
|
itemStore *DefaultMenuItemStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultMenuStore() *DefaultMenuStore {
|
|
|
|
return &DefaultMenuStore{
|
|
|
|
make(map[int]*atomic.Value),
|
|
|
|
NewDefaultMenuItemStore(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add actual support for multiple menus
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) GetAllMap() (out map[int]*MenuListHolder) {
|
2018-05-11 05:41:51 +00:00
|
|
|
out = make(map[int]*MenuListHolder)
|
2019-07-26 23:29:42 +00:00
|
|
|
for mid, atom := range s.menus {
|
2018-05-11 05:41:51 +00:00
|
|
|
out[mid] = atom.Load().(*MenuListHolder)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) Get(mid int) (*MenuListHolder, error) {
|
|
|
|
aStore, ok := s.menus[mid]
|
2018-05-11 05:41:51 +00:00
|
|
|
if ok {
|
|
|
|
return aStore.Load().(*MenuListHolder), nil
|
|
|
|
}
|
|
|
|
return nil, ErrNoRows
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) Items(mid int) (mlist MenuItemList, err error) {
|
2019-10-28 07:46:14 +00:00
|
|
|
err = qgen.NewAcc().Select("menu_items").Columns("miid,name,htmlID,cssClass,position,path,aria,tooltip,order,tmplName,guestOnly,memberOnly,staffOnly,adminOnly").Where("mid = " + strconv.Itoa(mid)).Orderby("order ASC").Each(func(rows *sql.Rows) error {
|
2019-07-26 23:29:42 +00:00
|
|
|
i := MenuItem{MenuID: mid}
|
|
|
|
err := rows.Scan(&i.ID, &i.Name, &i.HTMLID, &i.CSSClass, &i.Position, &i.Path, &i.Aria, &i.Tooltip, &i.Order, &i.TmplName, &i.GuestOnly, &i.MemberOnly, &i.SuperModOnly, &i.AdminOnly)
|
2018-05-11 05:41:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-26 23:29:42 +00:00
|
|
|
s.itemStore.Add(i)
|
|
|
|
mlist = append(mlist, i)
|
2018-05-11 05:41:51 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return mlist, err
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) Load(mid int) error {
|
|
|
|
mlist, err := s.Items(mid)
|
2018-05-11 05:41:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-13 06:56:59 +00:00
|
|
|
hold := &MenuListHolder{mid, mlist, make(map[int]menuTmpl)}
|
2018-05-11 05:41:51 +00:00
|
|
|
err = hold.Preparse()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
aStore := &atomic.Value{}
|
2018-05-11 05:41:51 +00:00
|
|
|
aStore.Store(hold)
|
2019-07-26 23:29:42 +00:00
|
|
|
s.menus[mid] = aStore
|
2018-05-11 05:41:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) ItemStore() *DefaultMenuItemStore {
|
|
|
|
return s.itemStore
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|