sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/modul/web/api.go

136 lines
7.0 KiB
Go

package web
import (
"net/http"
"github.com/jinzhu/gorm"
"goji.io"
"goji.io/pat"
//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.HandleFunc(pat.Get(prefix+"/involve"), libapi.SessionHandler(libsystem.LoginHandler(involve)))
router.HandleFunc(pat.Post(prefix+"/website"), libapi.SessionHandler(libsystem.LoginHandler(websiteAdd)))
router.HandleFunc(pat.Patch(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteEdit))))
router.HandleFunc(pat.Delete(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteDelete))))
router.HandleFunc(pat.Get(prefix+"/website/:websiteid/permission"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionList))))
router.HandleFunc(pat.Post(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionAdd))))
router.HandleFunc(pat.Delete(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionDelete))))
router.HandleFunc(pat.Get(prefix+"/website/:websiteid/domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainList))))
router.HandleFunc(pat.Post(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainAdd))))
router.HandleFunc(pat.Delete(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainDelete))))
router.HandleFunc(pat.Get(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuTree))))
router.HandleFunc(pat.Get(prefix+"/website/:websiteid/menu/list"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuList))))
router.HandleFunc(pat.Post(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuAdd))))
router.HandleFunc(pat.Patch(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuEdit))))
router.HandleFunc(pat.Delete(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuDelete))))
router.HandleFunc(pat.Get(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageList))))
router.HandleFunc(pat.Post(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageAdd))))
router.HandleFunc(pat.Patch(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageEdit))))
router.HandleFunc(pat.Delete(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageDelete))))
router.HandleFunc(pat.Get(prefix+"/website/:websiteid/blog"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogList))))
router.HandleFunc(pat.Post(prefix+"/website/:websiteid/blog"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogAdd))))
router.HandleFunc(pat.Patch(prefix+"/website/:websiteid/blog/:blogid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogEdit))))
router.HandleFunc(pat.Delete(prefix+"/website/:websiteid/blog/:blogid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogDelete))))
router.HandleFunc(pat.Get(prefix+"/website/:websiteid/blog/:blogid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogShow))))
router.HandleFunc(pat.Post(prefix+"/website/:websiteid/blog/:blogid/post"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogpostAdd))))
router.HandleFunc(pat.Patch(prefix+"/website/:websiteid/blog/:blogid/post/:blogpostid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogpostEdit))))
router.HandleFunc(pat.Delete(prefix+"/website/:websiteid/blog/:blogid/post/:blogpostid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(blogpostDelete))))
}
// Involve to get Website where loggend in user has privilegs
func involve(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
login := ctx.Value("login").(*libsystem.Login)
logger := log.GetLog(r, "involve")
var involved []*Manager
dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved)
logger.Info("done")
libapi.JSONWrite(w, r, involved, nil)
}
// WebsiteAdd to add a new website
func websiteAdd(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
login := ctx.Value("login").(*libsystem.Login)
logger := log.GetLog(r, "websiteadd")
tx := dbconnection.Begin()
var websiteRequest Website
returnerr := libapi.JSONDecoder(w, r, logger, &websiteRequest)
if returnerr != nil {
tx.Rollback()
libapi.JSONWrite(w, r, false, returnerr)
return
}
website := &Website{Name: websiteRequest.Name}
if err := tx.Create(website).Error; err != nil {
tx.Rollback()
logger.Error("error during Website")
libapi.JSONWrite(w, r, false, &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")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
return
}
tx.Commit()
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}
// WebsiteEdit to edit website
func websiteEdit(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := log.GetLog(r, "websiteedit")
var websiteRequest Website
returnerr := libapi.JSONDecoder(w, r, logger, &websiteRequest)
if returnerr != nil {
libapi.JSONWrite(w, r, false, returnerr)
return
}
websiteRequest.ID = ctx.Value("websiteid").(int64)
if err := dbconnection.Save(websiteRequest).Error; err != nil {
logger.Error("Database: during edit Website")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}
// WebsiteDelete to delete website
func websiteDelete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
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")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}