2016-09-13 20:00:18 +02:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-09-18 17:03:27 +02:00
|
|
|
"strconv"
|
2016-09-13 20:00:18 +02:00
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
"goji.io/pat"
|
|
|
|
"golang.org/x/net/context"
|
2016-09-13 20:00:18 +02:00
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
2016-09-13 20:00:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// MenuTree to give the tree of a menu back
|
2016-10-11 20:16:24 +02:00
|
|
|
func menuTree(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-13 20:00:18 +02:00
|
|
|
returndata = false
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "menutree")
|
2016-09-13 20:00:18 +02:00
|
|
|
var menus []*Menu
|
2016-10-11 20:16:24 +02:00
|
|
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus)
|
2016-09-13 20:00:18 +02:00
|
|
|
returndata = BuildMenuTree(menus)
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
2016-09-18 17:03:27 +02:00
|
|
|
|
2016-09-27 10:27:21 +02:00
|
|
|
// MenuList give all menu entries of a website
|
2016-10-11 20:16:24 +02:00
|
|
|
func menuList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-27 10:27:21 +02:00
|
|
|
returndata = false
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "menulist")
|
2016-09-27 10:27:21 +02:00
|
|
|
var menus []*Menu
|
2016-10-11 20:16:24 +02:00
|
|
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus)
|
2016-09-27 10:27:21 +02:00
|
|
|
returndata = menus
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:03:27 +02:00
|
|
|
// MenuAdd to add a new menu entry
|
2016-10-11 20:16:24 +02:00
|
|
|
func menuAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-18 17:03:27 +02:00
|
|
|
returndata = false
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "menuadd")
|
2016-09-18 17:03:27 +02:00
|
|
|
var menuEntry Menu
|
2016-10-17 14:07:17 +02:00
|
|
|
returnerr = libapi.JSONDecoder(r.Body, &menuEntry, w, logger)
|
|
|
|
if returnerr != nil {
|
2016-09-18 17:03:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
2016-09-18 17:03:27 +02:00
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
if err := dbconnection.Create(&menuEntry).Error; err != nil {
|
2016-09-18 17:03:27 +02:00
|
|
|
logger.Error("database: during create menu")
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-27 11:13:01 +02:00
|
|
|
// MenuEdit to edit menu
|
2016-10-11 20:16:24 +02:00
|
|
|
func menuEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-18 17:03:27 +02:00
|
|
|
returndata = false
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "menuedit")
|
2016-09-18 17:03:27 +02:00
|
|
|
var menuEntry Menu
|
2016-10-11 20:16:24 +02:00
|
|
|
menuid, err := strconv.ParseInt(pat.Param(ctx, "menuid"), 10, 64)
|
2016-09-18 17:03:27 +02:00
|
|
|
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)
|
2016-10-17 14:07:17 +02:00
|
|
|
returnerr = libapi.JSONDecoder(r.Body, &menuEntry, w, logger)
|
|
|
|
if returnerr != nil {
|
2016-09-18 17:03:27 +02:00
|
|
|
return
|
|
|
|
}
|
2016-10-11 20:16:24 +02:00
|
|
|
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
2016-09-18 17:03:27 +02:00
|
|
|
menuEntry.ID = menuid
|
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
if err := dbconnection.Save(menuEntry).Error; err != nil {
|
2016-09-18 17:03:27 +02:00
|
|
|
logger.Error("database: during delete website menu entry")
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-27 11:13:01 +02:00
|
|
|
// MenuDelete to delete menu entry
|
2016-10-11 20:16:24 +02:00
|
|
|
func menuDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-18 17:03:27 +02:00
|
|
|
returndata = false
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "menudelete")
|
|
|
|
menuid, err := strconv.ParseInt(pat.Param(ctx, "menuid"), 10, 64)
|
2016-09-18 17:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
2016-09-27 11:13:01 +02:00
|
|
|
logger.Warn("invalid menuid, no integer")
|
2016-09-18 17:03:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger = logger.WithField("id", menuid)
|
|
|
|
menu := &Menu{
|
2016-10-11 20:16:24 +02:00
|
|
|
WebsiteID: ctx.Value("websiteid").(int64),
|
2016-09-18 17:03:27 +02:00
|
|
|
ID: menuid,
|
|
|
|
}
|
2016-10-11 20:16:24 +02:00
|
|
|
if err := dbconnection.Unscoped().Delete(menu).Error; err != nil {
|
2016-09-18 17:03:27 +02:00
|
|
|
logger.Error("database: during delete website menu entry")
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|