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

110 lines
3.3 KiB
Go

package web
import (
"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
returnerr = libapi.JSONDecoder(r.Body, &menuEntry, w, logger)
if returnerr != nil {
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)
returnerr = libapi.JSONDecoder(r.Body, &menuEntry, w, logger)
if returnerr != nil {
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
}