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

106 lines
3.1 KiB
Go

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