2016-09-27 10:27:21 +02:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-09-27 11:13:01 +02:00
|
|
|
"strconv"
|
2016-09-27 10:27:21 +02:00
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
"goji.io/pat"
|
2016-09-27 10:27:21 +02:00
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
2016-09-27 10:27:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// PageList give all pages of a website
|
2016-12-19 12:24:18 +01:00
|
|
|
func pageList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "pagelist")
|
2016-09-27 10:27:21 +02:00
|
|
|
var pages []*Page
|
2016-10-11 20:16:24 +02:00
|
|
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Menu").Find(&pages)
|
2016-09-27 10:27:21 +02:00
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, pages, nil)
|
2016-09-27 10:27:21 +02:00
|
|
|
}
|
2016-09-27 11:13:01 +02:00
|
|
|
|
|
|
|
// PageAdd to add a new page
|
2016-12-19 12:24:18 +01:00
|
|
|
func pageAdd(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "pageadd")
|
2016-09-27 11:13:01 +02:00
|
|
|
var page Page
|
2016-12-19 12:24:18 +01:00
|
|
|
returnerr := libapi.JSONDecoder(w, r, logger, &page)
|
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-27 11:13:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
page.WebsiteID = ctx.Value("websiteid").(int64)
|
2016-09-27 11:13:01 +02:00
|
|
|
if menu := page.Menu; menu != nil {
|
|
|
|
page.MenuID = page.Menu.ID
|
|
|
|
page.Menu = nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
if err := dbconnection.Create(&page).Error; err != nil {
|
2016-09-27 11:13:01 +02:00
|
|
|
logger.Error("database: during create page")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
2016-09-27 11:13:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, true, nil)
|
2016-09-27 11:13:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PageEdit to edit page
|
2016-12-19 12:24:18 +01:00
|
|
|
func pageEdit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "pageedit")
|
2016-09-27 11:13:01 +02:00
|
|
|
var page Page
|
2016-12-19 12:24:18 +01:00
|
|
|
pageid, err := strconv.ParseInt(pat.Param(r, "pageid"), 10, 64)
|
2016-09-27 11:13:01 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn("invalid pageid, no integer")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"})
|
2016-09-27 11:13:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger = logger.WithField("id", pageid)
|
2016-12-19 12:24:18 +01:00
|
|
|
returnerr := libapi.JSONDecoder(w, r, logger, &page)
|
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-27 11:13:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-10-11 20:16:24 +02:00
|
|
|
page.WebsiteID = ctx.Value("websiteid").(int64)
|
2016-09-27 11:13:01 +02:00
|
|
|
page.ID = pageid
|
|
|
|
if menu := page.Menu; menu != nil {
|
|
|
|
page.MenuID = page.Menu.ID
|
|
|
|
page.Menu = nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
if err := dbconnection.Save(page).Error; err != nil {
|
2016-09-27 11:13:01 +02:00
|
|
|
logger.Error("database: during delete website page")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
2016-09-27 11:13:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, true, nil)
|
2016-09-27 11:13:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PageDelete to delete page
|
2016-12-19 12:24:18 +01:00
|
|
|
func pageDelete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "pagedelete")
|
2016-12-19 12:24:18 +01:00
|
|
|
pageid, err := strconv.ParseInt(pat.Param(r, "pageid"), 10, 64)
|
2016-09-27 11:13:01 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn("invalid pageid, no integer")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"})
|
2016-09-27 11:13:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger = logger.WithField("id", pageid)
|
|
|
|
page := &Page{
|
2016-10-11 20:16:24 +02:00
|
|
|
WebsiteID: ctx.Value("websiteid").(int64),
|
2016-09-27 11:13:01 +02:00
|
|
|
ID: pageid,
|
|
|
|
}
|
2016-10-11 20:16:24 +02:00
|
|
|
if err := dbconnection.Unscoped().Delete(page).Error; err != nil {
|
2016-09-27 11:13:01 +02:00
|
|
|
logger.Error("database: during delete website page")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
2016-09-27 11:13:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, true, nil)
|
2016-09-27 11:13:01 +02:00
|
|
|
}
|