121 lines
3.6 KiB
Go
121 lines
3.6 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"goji.io/pat"
|
|
"golang.org/x/net/context"
|
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
|
)
|
|
|
|
// MenuTree to give the tree of a menu back
|
|
func menuTree(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := log.GetLog(r, "menutree")
|
|
var menus []*Menu
|
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus)
|
|
returndata = BuildMenuTree(menus)
|
|
logger.Info("done")
|
|
return
|
|
}
|
|
|
|
// MenuList give all menu entries of a website
|
|
func menuList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := log.GetLog(r, "menulist")
|
|
var menus []*Menu
|
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus)
|
|
returndata = menus
|
|
logger.Info("done")
|
|
return
|
|
}
|
|
|
|
// MenuAdd to add a new menu entry
|
|
func menuAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := 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 = ctx.Value("websiteid").(int64)
|
|
|
|
if err := 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 edit menu
|
|
func menuEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := log.GetLog(r, "menuedit")
|
|
var menuEntry Menu
|
|
menuid, err := strconv.ParseInt(pat.Param(ctx, "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 = ctx.Value("websiteid").(int64)
|
|
menuEntry.ID = menuid
|
|
|
|
if err := 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 menu entry
|
|
func menuDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := log.GetLog(r, "menudelete")
|
|
menuid, err := strconv.ParseInt(pat.Param(ctx, "menuid"), 10, 64)
|
|
if err != nil {
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
|
logger.Warn("invalid menuid, no integer")
|
|
return
|
|
}
|
|
logger = logger.WithField("id", menuid)
|
|
menu := &Menu{
|
|
WebsiteID: ctx.Value("websiteid").(int64),
|
|
ID: menuid,
|
|
}
|
|
if err := 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
|
|
}
|