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
|
2017-02-20 18:58:53 +01:00
|
|
|
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("URL").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)
|
2017-02-20 18:58:53 +01:00
|
|
|
page.URL.WebsiteID = page.WebsiteID
|
|
|
|
page.URL.Path = FixPath(page.URL.Path)
|
2016-09-27 11:13:01 +02:00
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
if err := dbconnection.Create(&page).Error; err != nil {
|
2017-02-20 18:58:53 +01:00
|
|
|
logger.Error("database: during create page: ", err)
|
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
|
|
|
|
}
|
2017-02-20 18:58:53 +01:00
|
|
|
|
2016-09-27 11:13:01 +02:00
|
|
|
page.ID = pageid
|
2017-02-20 18:58:53 +01:00
|
|
|
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
|
2016-09-27 11:13:01 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 18:58:53 +01:00
|
|
|
page.URLID = page.URL.ID
|
|
|
|
page.URL = nil
|
|
|
|
|
|
|
|
if err := tx.Save(page).Error; err != nil {
|
|
|
|
logger.Error("database: during save website page: ", err)
|
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
|
|
|
|
}
|
2017-02-20 18:58:53 +01:00
|
|
|
tx.Commit()
|
|
|
|
|
2016-09-27 11:13:01 +02:00
|
|
|
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) {
|
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)
|
2017-02-20 18:58:53 +01:00
|
|
|
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)
|
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
|
|
|
}
|