[web] finish page
This commit is contained in:
parent
e6cf99db85
commit
8230bd91d9
|
@ -49,6 +49,9 @@ func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *g
|
||||||
router.PUT(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuEdit, sessions, dbconnection))
|
router.PUT(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuEdit, sessions, dbconnection))
|
||||||
router.DELETE(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuDelete, sessions, dbconnection))
|
router.DELETE(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuDelete, sessions, dbconnection))
|
||||||
router.GET(prefix+"/website/:websiteid/page", InvolveWebsiteHandler(api.PageList, sessions, dbconnection))
|
router.GET(prefix+"/website/:websiteid/page", InvolveWebsiteHandler(api.PageList, sessions, dbconnection))
|
||||||
|
router.POST(prefix+"/website/:websiteid/page", InvolveWebsiteHandler(api.PageAdd, sessions, dbconnection))
|
||||||
|
router.PUT(prefix+"/website/:websiteid/page/:pageid", InvolveWebsiteHandler(api.PageEdit, sessions, dbconnection))
|
||||||
|
router.DELETE(prefix+"/website/:websiteid/page/:pageid", InvolveWebsiteHandler(api.PageDelete, sessions, dbconnection))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Involve to get Website where loggend in user has privilegs
|
// Involve to get Website where loggend in user has privilegs
|
||||||
|
|
|
@ -17,7 +17,7 @@ func (api *API) MenuTree(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menutree")
|
logger := api.log.GetLog(r, "menutree")
|
||||||
var menus []*Menu
|
var menus []*Menu
|
||||||
api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Order("position").Find(&menus)
|
api.dbconnection.Where("website = ?", websiteid).Order("position").Find(&menus)
|
||||||
returndata = BuildMenuTree(menus)
|
returndata = BuildMenuTree(menus)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
return
|
return
|
||||||
|
@ -28,7 +28,7 @@ func (api *API) MenuList(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menulist")
|
logger := api.log.GetLog(r, "menulist")
|
||||||
var menus []*Menu
|
var menus []*Menu
|
||||||
api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Order("position").Find(&menus)
|
api.dbconnection.Where("website = ?", websiteid).Order("position").Find(&menus)
|
||||||
returndata = menus
|
returndata = menus
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
return
|
return
|
||||||
|
@ -61,7 +61,7 @@ func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuEdit to delete domain
|
// MenuEdit to edit menu
|
||||||
func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menuedit")
|
logger := api.log.GetLog(r, "menuedit")
|
||||||
|
@ -95,14 +95,14 @@ func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuDelete to delete domain
|
// MenuDelete to delete menu entry
|
||||||
func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "menudelete")
|
logger := api.log.GetLog(r, "menudelete")
|
||||||
menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64)
|
menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
|
||||||
logger.Warn("invalid loginid, no integer")
|
logger.Warn("invalid menuid, no integer")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger = logger.WithField("id", menuid)
|
logger = logger.WithField("id", menuid)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
"github.com/astaxie/session"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
@ -15,8 +17,102 @@ func (api *API) PageList(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := api.log.GetLog(r, "pagelist")
|
logger := api.log.GetLog(r, "pagelist")
|
||||||
var pages []*Page
|
var pages []*Page
|
||||||
api.dbconnection.Where("website = ?", websiteid).Preload("Page").Find(&pages)
|
api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Find(&pages)
|
||||||
returndata = pages
|
returndata = pages
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PageAdd to add a new page
|
||||||
|
func (api *API) PageAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
returndata = false
|
||||||
|
logger := api.log.GetLog(r, "pageadd")
|
||||||
|
var page Page
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&page)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("fetch request")
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
returnerr = &libapi.ErrorResult{
|
||||||
|
Message: "Internal Request Error",
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
page.WebsiteID = websiteid
|
||||||
|
if menu := page.Menu; menu != nil {
|
||||||
|
page.MenuID = page.Menu.ID
|
||||||
|
page.Menu = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := api.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 (api *API) PageEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
returndata = false
|
||||||
|
logger := api.log.GetLog(r, "pageedit")
|
||||||
|
var page Page
|
||||||
|
pageid, err := strconv.ParseInt(ps.ByName("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)
|
||||||
|
err = json.NewDecoder(r.Body).Decode(&page)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("fetch request")
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
returnerr = &libapi.ErrorResult{
|
||||||
|
Message: "Internal Request Error",
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
page.WebsiteID = websiteid
|
||||||
|
page.ID = pageid
|
||||||
|
if menu := page.Menu; menu != nil {
|
||||||
|
page.MenuID = page.Menu.ID
|
||||||
|
page.Menu = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := api.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 (api *API) PageDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
returndata = false
|
||||||
|
logger := api.log.GetLog(r, "pagedelete")
|
||||||
|
pageid, err := strconv.ParseInt(ps.ByName("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: websiteid,
|
||||||
|
ID: pageid,
|
||||||
|
}
|
||||||
|
if err := api.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
|
||||||
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ func (Manager) TableName() string { return "web_manager" }
|
||||||
|
|
||||||
// Media struct
|
// Media struct
|
||||||
type Media struct {
|
type Media struct {
|
||||||
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key"`
|
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key" json:"-"`
|
||||||
Path string `gorm:"type:varchar(255);column:path;primary_key" json:"path"`
|
Path string `gorm:"type:varchar(255);column:path;primary_key" json:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,8 +63,9 @@ func (Menu) TableName() string { return "web_menu" }
|
||||||
// Page struct
|
// Page struct
|
||||||
type Page struct {
|
type Page struct {
|
||||||
ID int64 `gorm:"primary_key"`
|
ID int64 `gorm:"primary_key"`
|
||||||
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website"`
|
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-"`
|
||||||
MenuID int64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE CASCADE ON DELETE CASCADE;column:menu"`
|
MenuID int64 `sql:"type:bigint unique REFERENCES web_menu(id) ON UPDATE CASCADE ON DELETE CASCADE;column:menu" json:"-"`
|
||||||
|
Menu *Menu `json:"menu"`
|
||||||
Title string `gorm:"type:varchar(255);column:title" json:"title"`
|
Title string `gorm:"type:varchar(255);column:title" json:"title"`
|
||||||
Content string `gorm:"type:text;column:content" json:"content"`
|
Content string `gorm:"type:text;column:content" json:"content"`
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue