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

121 lines
3.6 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
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
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
}
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)
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
}
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
}