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

131 lines
5.9 KiB
Go
Raw Normal View History

2016-09-01 22:19:39 +02:00
package web
import (
"net/http"
"github.com/jinzhu/gorm"
2016-10-11 20:16:24 +02:00
"goji.io"
"goji.io/pat"
"golang.org/x/net/context"
2016-09-01 22:19:39 +02:00
2016-10-11 20:16:24 +02:00
//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"
2016-09-01 22:19:39 +02:00
)
//MODULNAME to get global name for the modul
const MODULNAME = "web"
2016-10-11 20:16:24 +02:00
var dbconnection *gorm.DB
var log *liblog.ModulLog
2016-09-01 22:19:39 +02:00
2016-10-11 20:16:24 +02:00
// 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)))
2016-10-17 14:07:17 +02:00
router.HandleFuncC(pat.Patch(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteEdit))))
2016-10-11 20:16:24 +02:00
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))))
2016-10-17 14:07:17 +02:00
router.HandleFuncC(pat.Patch(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuEdit))))
2016-10-11 20:16:24 +02:00
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))))
2016-10-17 14:07:17 +02:00
router.HandleFuncC(pat.Patch(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageEdit))))
2016-10-11 20:16:24 +02:00
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageDelete))))
2016-09-01 22:19:39 +02:00
}
// Involve to get Website where loggend in user has privilegs
2016-10-11 20:16:24 +02:00
func involve(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*libsystem.Login)
2016-09-01 22:19:39 +02:00
returndata = false
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "involve")
2016-09-01 22:19:39 +02:00
var involved []*Manager
2016-10-11 20:16:24 +02:00
dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved)
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-09-01 22:19:39 +02:00
returndata = involved
return
}
2016-09-03 10:18:46 +02:00
// WebsiteAdd to add a new website
2016-10-11 20:16:24 +02:00
func websiteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*libsystem.Login)
2016-09-01 22:19:39 +02:00
returndata = false
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "websiteadd")
tx := dbconnection.Begin()
2016-09-03 16:30:48 +02:00
var websiteRequest Website
2016-10-17 14:07:17 +02:00
returnerr = libapi.JSONDecoder(r.Body, &websiteRequest, w, logger)
if returnerr != nil {
2016-09-03 16:30:48 +02:00
tx.Rollback()
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
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-09-03 16:30:48 +02:00
return
}
// WebsiteEdit to edit website
2016-10-11 20:16:24 +02:00
func websiteEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
2016-09-03 16:30:48 +02:00
returndata = false
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "websiteedit")
2016-09-03 16:30:48 +02:00
var websiteRequest Website
2016-10-17 14:07:17 +02:00
returnerr = libapi.JSONDecoder(r.Body, &websiteRequest, w, logger)
if returnerr != nil {
2016-09-03 16:30:48 +02:00
return
}
2016-10-17 14:07:17 +02:00
2016-10-11 20:16:24 +02:00
websiteRequest.ID = ctx.Value("websiteid").(int64)
if err := dbconnection.Save(websiteRequest).Error; err != nil {
2016-09-03 16:30:48 +02:00
logger.Error("Database: during edit Website")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-09-03 16:30:48 +02:00
return
}
// WebsiteDelete to delete website
2016-10-11 20:16:24 +02:00
func websiteDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
2016-09-03 16:30:48 +02:00
returndata = false
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "websitedelete")
2016-09-03 16:30:48 +02:00
website := &Website{
2016-10-11 20:16:24 +02:00
ID: ctx.Value("websiteid").(int64),
2016-09-03 16:30:48 +02:00
}
2016-10-11 20:16:24 +02:00
if err := dbconnection.Unscoped().Delete(website).Error; err != nil {
2016-09-03 16:30:48 +02:00
logger.Error("database: during delete website")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-09-01 22:19:39 +02:00
return
}