sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[web] finish page

This commit is contained in:
Martin Geno 2016-09-27 11:13:01 +02:00
parent e6cf99db85
commit 8230bd91d9
4 changed files with 109 additions and 9 deletions

View File

@ -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.DELETE(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuDelete, 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

View File

@ -17,7 +17,7 @@ func (api *API) MenuTree(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
returndata = false
logger := api.log.GetLog(r, "menutree")
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)
logger.Info("done")
return
@ -28,7 +28,7 @@ func (api *API) MenuList(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
returndata = false
logger := api.log.GetLog(r, "menulist")
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
logger.Info("done")
return
@ -61,7 +61,7 @@ func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
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) {
returndata = false
logger := api.log.GetLog(r, "menuedit")
@ -95,14 +95,14 @@ func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P
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) {
returndata = false
logger := api.log.GetLog(r, "menudelete")
menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64)
if err != nil {
returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"}
logger.Warn("invalid loginid, no integer")
logger.Warn("invalid menuid, no integer")
return
}
logger = logger.WithField("id", menuid)

View File

@ -1,7 +1,9 @@
package web
import (
"encoding/json"
"net/http"
"strconv"
"github.com/astaxie/session"
"github.com/julienschmidt/httprouter"
@ -15,8 +17,102 @@ func (api *API) PageList(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
returndata = false
logger := api.log.GetLog(r, "pagelist")
var pages []*Page
api.dbconnection.Where("website = ?", websiteid).Preload("Page").Find(&pages)
api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Find(&pages)
returndata = pages
logger.Info("done")
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
}

View File

@ -39,7 +39,7 @@ func (Manager) TableName() string { return "web_manager" }
// 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"`
}
@ -63,8 +63,9 @@ func (Menu) TableName() string { return "web_menu" }
// Page struct
type Page struct {
ID int64 `gorm:"primary_key"`
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website"`
MenuID int64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE CASCADE ON DELETE CASCADE;column:menu"`
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-"`
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"`
Content string `gorm:"type:text;column:content" json:"content"`
}