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

115 lines
3.3 KiB
Go
Raw Normal View History

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"
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-12-19 12:24:18 +01:00
func menuTree(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "menutree")
2016-09-13 20:00:18 +02:00
var menus []*Menu
2017-02-20 18:58:53 +01:00
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("URL").Order("position").Find(&menus)
2016-09-13 20:00:18 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, BuildMenuTree(menus), nil)
2016-09-13 20:00:18 +02:00
}
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-12-19 12:24:18 +01:00
func menuList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "menulist")
2016-09-27 10:27:21 +02:00
var menus []*Menu
2017-02-20 18:58:53 +01:00
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("URL").Order("position").Find(&menus)
2016-09-27 10:27:21 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, menus, nil)
2016-09-27 10:27:21 +02:00
}
2016-09-18 17:03:27 +02:00
// MenuAdd to add a new menu entry
2016-12-19 12:24:18 +01:00
func menuAdd(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
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-12-19 12:24:18 +01:00
returnerr := libapi.JSONDecoder(w, r, logger, &menuEntry)
2016-10-17 14:07:17 +02:00
if returnerr != nil {
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-09-18 17:03:27 +02:00
return
}
2016-10-11 20:16:24 +02:00
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
2017-02-20 18:58:53 +01:00
if menuEntry.URL != nil {
menuEntry.URLID = &menuEntry.URL.ID
menuEntry.URL = nil
}
2016-09-18 17:03:27 +02:00
2016-10-11 20:16:24 +02:00
if err := dbconnection.Create(&menuEntry).Error; err != nil {
2017-02-20 18:58:53 +01:00
logger.Error("database: during create menu: ", err)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-18 17:03:27 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-18 17:03:27 +02:00
}
2016-09-27 11:13:01 +02:00
// MenuEdit to edit menu
2016-12-19 12:24:18 +01:00
func menuEdit(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
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-12-19 12:24:18 +01:00
menuid, err := strconv.ParseInt(pat.Param(r, "menuid"), 10, 64)
2016-09-18 17:03:27 +02:00
if err != nil {
logger.Warn("invalid loginid, no integer")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"})
2016-09-18 17:03:27 +02:00
return
}
logger = logger.WithField("id", menuid)
2016-12-19 12:24:18 +01:00
returnerr := libapi.JSONDecoder(w, r, logger, &menuEntry)
2016-10-17 14:07:17 +02:00
if returnerr != nil {
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
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
2017-02-20 18:58:53 +01:00
if menuEntry.URL != nil {
menuEntry.URLID = &menuEntry.URL.ID
menuEntry.URL = nil
}
2016-10-11 20:16:24 +02:00
if err := dbconnection.Save(menuEntry).Error; err != nil {
2017-02-20 18:58:53 +01:00
logger.Error("database: during edit website menu entry: ", err)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-18 17:03:27 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-18 17:03:27 +02:00
}
2016-09-27 11:13:01 +02:00
// MenuDelete to delete menu entry
2016-12-19 12:24:18 +01:00
func menuDelete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "menudelete")
2016-12-19 12:24:18 +01:00
menuid, err := strconv.ParseInt(pat.Param(r, "menuid"), 10, 64)
2016-09-18 17:03:27 +02:00
if err != nil {
2016-09-27 11:13:01 +02:00
logger.Warn("invalid menuid, no integer")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"})
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 {
2017-02-20 18:58:53 +01:00
logger.Error("database: during delete website menu entry: ", err)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-18 17:03:27 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-18 17:03:27 +02:00
}