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

137 lines
5.4 KiB
Go
Raw Normal View History

2016-09-01 22:19:39 +02:00
package web
import (
2016-09-03 16:30:48 +02:00
"encoding/json"
2016-09-01 22:19:39 +02:00
"net/http"
"github.com/astaxie/session"
"github.com/jinzhu/gorm"
"github.com/julienschmidt/httprouter"
libconfig "dev.sum7.de/sum7/warehost/config"
libapi "dev.sum7.de/sum7/warehost/lib/api"
log "dev.sum7.de/sum7/warehost/lib/log"
libsystem "dev.sum7.de/sum7/warehost/system"
)
//MODULNAME to get global name for the modul
const MODULNAME = "web"
//API keep data in module global
type API struct {
config *libconfig.Config
sessions *session.Manager
dbconnection *gorm.DB
log *log.ModulLog
}
// NewAPI sets the routes to the api functions
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router, prefix string) {
api := &API{
config: config,
sessions: sessions,
dbconnection: dbconnection,
log: log.NewModulLog(MODULNAME),
}
router.GET(prefix+"/involve", libsystem.LoginHandler(api.Involve, sessions))
2016-09-03 16:30:48 +02:00
router.POST(prefix+"/website", libsystem.LoginHandler(api.WebsiteAdd, sessions))
router.PUT(prefix+"/website/:websiteid", InvolveWebsiteHandler(api.WebsiteEdit, sessions, dbconnection))
router.DELETE(prefix+"/website/:websiteid", InvolveWebsiteHandler(api.WebsiteDelete, sessions, dbconnection))
2016-09-11 18:40:33 +02:00
router.GET(prefix+"/website/:websiteid/permission", InvolveWebsiteHandler(api.PermissionList, sessions, dbconnection))
router.POST(prefix+"/website/:websiteid/permission/:loginid", InvolveWebsiteHandler(api.PermissionAdd, sessions, dbconnection))
router.DELETE(prefix+"/website/:websiteid/permission/:loginid", InvolveWebsiteHandler(api.PermissionDelete, sessions, dbconnection))
2016-09-11 23:04:26 +02:00
router.GET(prefix+"/website/:websiteid/domain", InvolveWebsiteHandler(api.DomainList, sessions, dbconnection))
router.POST(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainAdd, sessions, dbconnection))
router.DELETE(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainDelete, sessions, dbconnection))
2016-09-13 20:00:18 +02:00
router.GET(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuTree, sessions, dbconnection))
2016-09-18 17:03:27 +02:00
router.POST(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuAdd, sessions, dbconnection))
router.PUT(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuEdit, sessions, dbconnection))
router.DELETE(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuDelete, sessions, dbconnection))
2016-09-01 22:19:39 +02:00
}
// Involve to get Website where loggend in user has privilegs
func (api *API) Involve(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.log.GetLog(r, "involve")
var involved []*Manager
api.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-09-01 22:19:39 +02:00
func (api *API) WebsiteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.log.GetLog(r, "websiteadd")
2016-09-03 16:30:48 +02:00
tx := api.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
2016-09-11 18:40:33 +02:00
logger.Info("done")
2016-09-03 16:30:48 +02:00
return
}
// WebsiteEdit to edit website
func (api *API) WebsiteEdit(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.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 = websiteid
if err := api.dbconnection.Save(websiteRequest).Error; err != nil {
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
func (api *API) WebsiteDelete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := api.log.GetLog(r, "websitedelete")
website := &Website{
ID: websiteid,
}
if err := api.dbconnection.Unscoped().Delete(website).Error; err != nil {
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
}