137 lines
6.2 KiB
Go
137 lines
6.2 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
"goji.io"
|
|
"goji.io/pat"
|
|
"golang.org/x/net/context"
|
|
|
|
//libconfig "dev.sum7.eu/sum7/warehost/config"
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
|
libsystem "dev.sum7.eu/sum7/warehost/system"
|
|
)
|
|
|
|
//MODULNAME to get global name for the modul
|
|
const MODULNAME = "web"
|
|
|
|
var dbconnection *gorm.DB
|
|
var log *liblog.ModulLog
|
|
|
|
// BindAPI sets the routes to the api functions
|
|
func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
|
dbconnection = db
|
|
log = liblog.NewModulLog(MODULNAME)
|
|
|
|
router.HandleFuncC(pat.Get(prefix+"/involve"), libapi.SessionHandler(libsystem.LoginHandler(involve)))
|
|
router.HandleFuncC(pat.Post(prefix+"/website"), libapi.SessionHandler(libsystem.LoginHandler(websiteAdd)))
|
|
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteEdit))))
|
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteDelete))))
|
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/permission"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionList))))
|
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionAdd))))
|
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionDelete))))
|
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainList))))
|
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainAdd))))
|
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainDelete))))
|
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuTree))))
|
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu/list"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuList))))
|
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuAdd))))
|
|
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuEdit))))
|
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuDelete))))
|
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageList))))
|
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageAdd))))
|
|
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageEdit))))
|
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageDelete))))
|
|
}
|
|
|
|
// Involve to get Website where loggend in user has privilegs
|
|
func involve(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
login := ctx.Value("login").(*libsystem.Login)
|
|
returndata = false
|
|
logger := log.GetLog(r, "involve")
|
|
var involved []*Manager
|
|
dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved)
|
|
logger.Info("done")
|
|
returndata = involved
|
|
return
|
|
}
|
|
|
|
// WebsiteAdd to add a new website
|
|
func websiteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
login := ctx.Value("login").(*libsystem.Login)
|
|
returndata = false
|
|
logger := log.GetLog(r, "websiteadd")
|
|
tx := dbconnection.Begin()
|
|
var websiteRequest Website
|
|
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
logger.Error("fetch request")
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
return
|
|
}
|
|
website := &Website{Name: websiteRequest.Name}
|
|
if err := tx.Create(website).Error; err != nil {
|
|
tx.Rollback()
|
|
logger.Error("error during Website")
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
return
|
|
}
|
|
|
|
if err := tx.Create(&Manager{LoginID: login.ID, WebsiteID: website.ID}).Error; err != nil {
|
|
tx.Rollback()
|
|
logger.Error("error during Manager")
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
returndata = true
|
|
logger.Info("done")
|
|
return
|
|
}
|
|
|
|
// WebsiteEdit to edit website
|
|
func websiteEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := log.GetLog(r, "websiteedit")
|
|
var websiteRequest Website
|
|
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
|
if err != nil {
|
|
logger.Error("fetch request")
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
return
|
|
}
|
|
websiteRequest.ID = ctx.Value("websiteid").(int64)
|
|
if err := dbconnection.Save(websiteRequest).Error; err != nil {
|
|
logger.Error("Database: during edit Website")
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
return
|
|
}
|
|
returndata = true
|
|
logger.Info("done")
|
|
return
|
|
}
|
|
|
|
// WebsiteDelete to delete website
|
|
func websiteDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := log.GetLog(r, "websitedelete")
|
|
website := &Website{
|
|
ID: ctx.Value("websiteid").(int64),
|
|
}
|
|
if err := dbconnection.Unscoped().Delete(website).Error; err != nil {
|
|
logger.Error("database: during delete website")
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
return
|
|
}
|
|
returndata = true
|
|
logger.Info("done")
|
|
return
|
|
}
|