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"
|
|
|
|
"golang.org/x/net/context"
|
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-10-11 20:16:24 +02:00
|
|
|
func pageList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-27 10:27:21 +02:00
|
|
|
returndata = false
|
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
|
|
|
returndata = pages
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
2016-09-27 11:13:01 +02:00
|
|
|
|
|
|
|
// PageAdd to add a new page
|
2016-10-11 20:16:24 +02:00
|
|
|
func pageAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-27 11:13:01 +02:00
|
|
|
returndata = false
|
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-10-17 14:07:17 +02:00
|
|
|
returnerr = libapi.JSONDecoder(r.Body, &page, w, logger)
|
|
|
|
if returnerr != nil {
|
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")
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PageEdit to edit page
|
2016-10-11 20:16:24 +02:00
|
|
|
func pageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-27 11:13:01 +02:00
|
|
|
returndata = false
|
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-10-11 20:16:24 +02:00
|
|
|
pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64)
|
2016-09-27 11:13:01 +02:00
|
|
|
if err != nil {
|
|
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}
|
|
|
|
logger.Warn("invalid pageid, no integer")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger = logger.WithField("id", pageid)
|
2016-10-17 14:07:17 +02:00
|
|
|
returnerr = libapi.JSONDecoder(r.Body, &page, w, logger)
|
|
|
|
if returnerr != nil {
|
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")
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PageDelete to delete page
|
2016-10-11 20:16:24 +02:00
|
|
|
func pageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-09-27 11:13:01 +02:00
|
|
|
returndata = false
|
2016-10-11 20:16:24 +02:00
|
|
|
logger := log.GetLog(r, "pagedelete")
|
|
|
|
pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64)
|
2016-09-27 11:13:01 +02:00
|
|
|
if err != nil {
|
|
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}
|
|
|
|
logger.Warn("invalid pageid, no integer")
|
|
|
|
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")
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|