gosora/common/menu_store.go
Azareal a287ccc3d6 WIP Conversations templates.
WIP Conversations structs.
WIP Unbound conversations routes.
Shorten more variable names.
Eliminate a few characters here and there.
Track down widget tick errors more easily.

Add the convos_head title phrase.
Add the convo_head title phrase.
Add the unit phrase.
Add the account_menu_messages phrase.
Add the convos_head phrase.
Add the convo_head phrase.
Add the create_convo_head phrase.
Add the create_convo_button phrase.
Add the profile_send_message phrase.
Add the panel_dashboard_disk_unknown phrase.
2019-07-27 09:29:42 +10:00

76 lines
1.8 KiB
Go

package common
import (
"database/sql"
"strconv"
"sync/atomic"
"github.com/Azareal/Gosora/query_gen"
)
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
func (s *DefaultMenuStore) GetAllMap() (out map[int]*MenuListHolder) {
out = make(map[int]*MenuListHolder)
for mid, atom := range s.menus {
out[mid] = atom.Load().(*MenuListHolder)
}
return out
}
func (s *DefaultMenuStore) Get(mid int) (*MenuListHolder, error) {
aStore, ok := s.menus[mid]
if ok {
return aStore.Load().(*MenuListHolder), nil
}
return nil, ErrNoRows
}
func (s *DefaultMenuStore) Items(mid int) (mlist MenuItemList, err error) {
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 {
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)
if err != nil {
return err
}
s.itemStore.Add(i)
mlist = append(mlist, i)
return nil
})
return mlist, err
}
func (s *DefaultMenuStore) Load(mid int) error {
mlist, err := s.Items(mid)
if err != nil {
return err
}
hold := &MenuListHolder{mid, mlist, make(map[int]menuTmpl)}
err = hold.Preparse()
if err != nil {
return err
}
aStore := &atomic.Value{}
aStore.Store(hold)
s.menus[mid] = aStore
return nil
}
func (s *DefaultMenuStore) ItemStore() *DefaultMenuItemStore {
return s.itemStore
}