116 lines
3.4 KiB
Go
116 lines
3.4 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"goji.io/pat"
|
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
|
"dev.sum7.eu/sum7/warehost/lib/data"
|
|
)
|
|
|
|
// 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)).Preload("URL").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)).Preload("URL").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 menuEntry.URL != nil {
|
|
menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true}
|
|
menuEntry.URL = nil
|
|
}
|
|
|
|
if err := dbconnection.Create(&menuEntry).Error; err != nil {
|
|
logger.Error("database: during create menu: ", err)
|
|
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 menuEntry.URL != nil {
|
|
menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true}
|
|
menuEntry.URL = nil
|
|
}
|
|
|
|
if err := dbconnection.Save(menuEntry).Error; err != nil {
|
|
logger.Error("database: during edit website menu entry: ", err)
|
|
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: ", err)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
|
return
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, true, nil)
|
|
}
|