package web import ( "encoding/json" "net/http" "strconv" "github.com/astaxie/session" "github.com/julienschmidt/httprouter" libapi "dev.sum7.de/sum7/warehost/lib/api" libsystem "dev.sum7.de/sum7/warehost/system" ) // MenuTree to give the tree of a menu back func (api *API) MenuTree(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := api.log.GetLog(r, "menutree") var menus []*Menu api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Order("position").Find(&menus) returndata = BuildMenuTree(menus) logger.Info("done") return } // MenuAdd to add a new menu entry func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := api.log.GetLog(r, "menuadd") 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 } menuEntry.WebsiteID = websiteid if err := api.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 delete domain func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := api.log.GetLog(r, "menuedit") var menuEntry Menu menuid, err := strconv.ParseInt(ps.ByName("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) 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 } menuEntry.WebsiteID = websiteid menuEntry.ID = menuid if err := api.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 domain func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := api.log.GetLog(r, "menudelete") menuid, err := strconv.ParseInt(ps.ByName("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) menu := &Menu{ WebsiteID: websiteid, ID: menuid, } if err := api.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 }