sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/modul/web/apimenu.go

111 lines
3.5 KiB
Go
Raw Normal View History

2016-09-13 20:00:18 +02:00
package web
import (
2016-09-18 17:03:27 +02:00
"encoding/json"
2016-09-13 20:00:18 +02:00
"net/http"
2016-09-18 17:03:27 +02:00
"strconv"
2016-09-13 20:00:18 +02:00
"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
}
2016-09-18 17:03:27 +02:00
// 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
}