sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[web] menu fertig?

This commit is contained in:
Martin Geno 2016-09-18 17:03:27 +02:00
parent 3649b56515
commit 7837259a62
3 changed files with 92 additions and 1 deletions

View File

@ -44,6 +44,9 @@ func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *g
router.POST(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainAdd, 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.DELETE(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainDelete, sessions, dbconnection))
router.GET(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuTree, sessions, dbconnection)) router.GET(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuTree, sessions, dbconnection))
router.POST(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuAdd, sessions, dbconnection))
router.PUT(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuEdit, sessions, dbconnection))
router.DELETE(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuDelete, sessions, dbconnection))
} }
// Involve to get Website where loggend in user has privilegs // Involve to get Website where loggend in user has privilegs

View File

@ -1,7 +1,9 @@
package web package web
import ( import (
"encoding/json"
"net/http" "net/http"
"strconv"
"github.com/astaxie/session" "github.com/astaxie/session"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
@ -20,3 +22,89 @@ func (api *API) MenuTree(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
logger.Info("done") logger.Info("done")
return return
} }
// MenuAdd to add a new menu entry
func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.log.GetLog(r, "menuadd")
var menuEntry Menu
err := json.NewDecoder(r.Body).Decode(&menuEntry)
if err != nil {
logger.Error("fetch request")
http.Error(w, err.Error(), http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
return
}
menuEntry.WebsiteID = websiteid
if err := api.dbconnection.Create(&menuEntry).Error; err != nil {
logger.Error("database: during create menu")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
logger.Info("done")
return
}
// MenuEdit to delete domain
func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.log.GetLog(r, "menuedit")
var menuEntry Menu
menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64)
if err != nil {
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
logger.Warn("invalid loginid, no integer")
return
}
logger = logger.WithField("id", menuid)
err = json.NewDecoder(r.Body).Decode(&menuEntry)
if err != nil {
logger.Error("fetch request")
http.Error(w, err.Error(), http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
return
}
menuEntry.WebsiteID = websiteid
menuEntry.ID = menuid
if err := api.dbconnection.Save(menuEntry).Error; err != nil {
logger.Error("database: during delete website menu entry")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
logger.Info("done")
return
}
// MenuDelete to delete domain
func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.log.GetLog(r, "menudelete")
menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64)
if err != nil {
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
logger.Warn("invalid loginid, no integer")
return
}
logger = logger.WithField("id", menuid)
menu := &Menu{
WebsiteID: websiteid,
ID: menuid,
}
if err := api.dbconnection.Unscoped().Delete(menu).Error; err != nil {
logger.Error("database: during delete website menu entry")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
logger.Info("done")
return
}

@ -1 +1 @@
Subproject commit 7f94fc78be45b3bba60da3b7b40c37e6aeccabe2 Subproject commit 85feebfc7e3aa658fa210ad1d38d85d402f0c767