111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"goji.io/pat"
|
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
|
)
|
|
|
|
// PageList give all pages of a website
|
|
func pageList(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
logger := log.GetLog(r, "pagelist")
|
|
var pages []*Page
|
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("URL").Find(&pages)
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, pages, nil)
|
|
}
|
|
|
|
// PageAdd to add a new page
|
|
func pageAdd(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
logger := log.GetLog(r, "pageadd")
|
|
var page Page
|
|
returnerr := libapi.JSONDecoder(w, r, logger, &page)
|
|
if returnerr != nil {
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
|
|
page.WebsiteID = ctx.Value("websiteid").(int64)
|
|
page.URL.WebsiteID = page.WebsiteID
|
|
page.URL.Path = FixPath(page.URL.Path)
|
|
|
|
if err := dbconnection.Create(&page).Error; err != nil {
|
|
logger.Error("database: during create page: ", err)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
|
return
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, true, nil)
|
|
}
|
|
|
|
// PageEdit to edit page
|
|
func pageEdit(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
logger := log.GetLog(r, "pageedit")
|
|
var page Page
|
|
pageid, err := strconv.ParseInt(pat.Param(r, "pageid"), 10, 64)
|
|
if err != nil {
|
|
logger.Warn("invalid pageid, no integer")
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"})
|
|
return
|
|
}
|
|
logger = logger.WithField("id", pageid)
|
|
returnerr := libapi.JSONDecoder(w, r, logger, &page)
|
|
if returnerr != nil {
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
|
|
page.ID = pageid
|
|
page.WebsiteID = ctx.Value("websiteid").(int64)
|
|
page.URL.WebsiteID = page.WebsiteID
|
|
page.URL.Path = FixPath(page.URL.Path)
|
|
|
|
tx := dbconnection.Begin()
|
|
if err := tx.Save(page.URL).Error; err != nil {
|
|
logger.Error("database: during save website url: ", err)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
|
return
|
|
}
|
|
|
|
page.URLID = page.URL.ID
|
|
page.URL = nil
|
|
|
|
if err := tx.Save(page).Error; err != nil {
|
|
logger.Error("database: during save website page: ", err)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
|
return
|
|
}
|
|
tx.Commit()
|
|
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, true, nil)
|
|
}
|
|
|
|
// PageDelete to delete page
|
|
func pageDelete(w http.ResponseWriter, r *http.Request) {
|
|
logger := log.GetLog(r, "pagedelete")
|
|
pageid, err := strconv.ParseInt(pat.Param(r, "pageid"), 10, 64)
|
|
if err != nil {
|
|
logger.Warn("invalid pageid, no integer")
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"})
|
|
return
|
|
}
|
|
logger = logger.WithField("id", pageid)
|
|
page := &Page{}
|
|
dbconnection.Where("id = ?", pageid).Preload("URL").First(page)
|
|
|
|
if err := dbconnection.Unscoped().Delete(page.URL).Error; err != nil {
|
|
logger.Error("database: during delete website page: ", err)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
|
|
return
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, true, nil)
|
|
}
|