From 3649b565157434af94224da20d0c5bfce6dd6b9f Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Tue, 13 Sep 2016 20:00:18 +0200 Subject: [PATCH] [web] menu tree --- modul/web/api.go | 1 + modul/web/apimenu.go | 22 ++++++++++++++++++++++ modul/web/models.go | 7 ++++--- webroot | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 modul/web/apimenu.go diff --git a/modul/web/api.go b/modul/web/api.go index bcf8cd1..7439e79 100644 --- a/modul/web/api.go +++ b/modul/web/api.go @@ -43,6 +43,7 @@ func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *g router.GET(prefix+"/website/:websiteid/domain", InvolveWebsiteHandler(api.DomainList, sessions, dbconnection)) router.POST(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainAdd, sessions, dbconnection)) router.DELETE(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainDelete, sessions, dbconnection)) + router.GET(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuTree, sessions, dbconnection)) } // Involve to get Website where loggend in user has privilegs diff --git a/modul/web/apimenu.go b/modul/web/apimenu.go new file mode 100644 index 0000000..c2e4e6c --- /dev/null +++ b/modul/web/apimenu.go @@ -0,0 +1,22 @@ +package web + +import ( + "net/http" + + "github.com/astaxie/session" + "github.com/julienschmidt/httprouter" + + libapi "dev.sum7.de/sum7/warehost/lib/api" + libsystem "dev.sum7.de/sum7/warehost/system" +) + +// MenuTree to give the tree of a menu back +func (api *API) MenuTree(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := api.log.GetLog(r, "menutree") + var menus []*Menu + api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Order("position").Find(&menus) + returndata = BuildMenuTree(menus) + logger.Info("done") + return +} diff --git a/modul/web/models.go b/modul/web/models.go index ffea555..c75a397 100644 --- a/modul/web/models.go +++ b/modul/web/models.go @@ -49,11 +49,12 @@ func (Media) TableName() string { return "web_media" } // Menu struct type Menu struct { ID int64 `gorm:"primary_key"` - WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website"` + WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-"` Name string `gorm:"type:varchar(255);column:name" json:"name"` Shorturl string `gorm:"type:varchar(255);column:url" json:"url"` - ParentID sql.NullInt64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu"` - Children []*Menu `json:"menu"` + ParentID sql.NullInt64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu" json:"parentid"` + Position int `sql:"type:int;column:position" json:"position"` + Children []*Menu `json:"children"` } // TableName of struct diff --git a/webroot b/webroot index 58aab8d..7f94fc7 160000 --- a/webroot +++ b/webroot @@ -1 +1 @@ -Subproject commit 58aab8dfd889cc4b5101524e8340c0f1321d7faa +Subproject commit 7f94fc78be45b3bba60da3b7b40c37e6aeccabe2