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
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"
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)
2016-12-19 12:24:18 +01:00
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))))
2016-09-01 22:19:39 +02:00
}
// Involve to get Website where loggend in user has privilegs
2016-12-19 12:24:18 +01:00
func involve(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
login := ctx.Value("login").(*libsystem.Login)
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-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, involved, nil)
2016-09-01 22:19:39 +02:00
}
2016-09-03 10:18:46 +02:00
// WebsiteAdd to add a new website
2016-12-19 12:24:18 +01:00
func websiteAdd(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
login := ctx.Value("login").(*libsystem.Login)
logger := log.GetLog(r, "websiteadd")
tx := dbconnection.Begin()
2016-09-03 16:30:48 +02:00
var websiteRequest Website
2016-12-19 12:24:18 +01:00
returnerr := libapi.JSONDecoder(w, r, logger, &websiteRequest)
2016-10-17 14:07:17 +02:00
if returnerr != nil {
2016-09-03 16:30:48 +02:00
tx.Rollback()
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-09-03 16:30:48 +02:00
return
}
website := &Website{Name: websiteRequest.Name}
if err := tx.Create(website).Error; err != nil {
tx.Rollback()
logger.Error("error during Website")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-03 16:30:48 +02:00
return
}
if err := tx.Create(&Manager{LoginID: login.ID, WebsiteID: website.ID}).Error; err != nil {
tx.Rollback()
logger.Error("error during Manager")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-03 16:30:48 +02:00
return
}
tx.Commit()
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-03 16:30:48 +02:00
}
// WebsiteEdit to edit website
2016-12-19 12:24:18 +01:00
func websiteEdit(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
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-12-19 12:24:18 +01:00
returnerr := libapi.JSONDecoder(w, r, logger, &websiteRequest)
2016-10-17 14:07:17 +02:00
if returnerr != nil {
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
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")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-03 16:30:48 +02:00
return
}
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-03 16:30:48 +02:00
}
// WebsiteDelete to delete website
2016-12-19 12:24:18 +01:00
func websiteDelete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
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")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-03 16:30:48 +02:00
return
}
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-01 22:19:39 +02:00
}