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/apipage.go

107 lines
3.0 KiB
Go

package web
import (
"net/http"
"strconv"
"goji.io/pat"
"golang.org/x/net/context"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
)
// PageList give all pages of a website
func pageList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "pagelist")
var pages []*Page
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Menu").Find(&pages)
returndata = pages
logger.Info("done")
return
}
// PageAdd to add a new page
func pageAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "pageadd")
var page Page
returnerr = libapi.JSONDecoder(r.Body, &page, w, logger)
if returnerr != nil {
return
}
page.WebsiteID = ctx.Value("websiteid").(int64)
if menu := page.Menu; menu != nil {
page.MenuID = page.Menu.ID
page.Menu = nil
}
if err := dbconnection.Create(&page).Error; err != nil {
logger.Error("database: during create page")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
logger.Info("done")
return
}
// PageEdit to edit page
func pageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "pageedit")
var page Page
pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64)
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)
returnerr = libapi.JSONDecoder(r.Body, &page, w, logger)
if returnerr != nil {
return
}
page.WebsiteID = ctx.Value("websiteid").(int64)
page.ID = pageid
if menu := page.Menu; menu != nil {
page.MenuID = page.Menu.ID
page.Menu = nil
}
if err := dbconnection.Save(page).Error; err != nil {
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
func pageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "pagedelete")
pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64)
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{
WebsiteID: ctx.Value("websiteid").(int64),
ID: pageid,
}
if err := dbconnection.Unscoped().Delete(page).Error; err != nil {
logger.Error("database: during delete website page")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
logger.Info("done")
return
}