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

package web
import (
"encoding/json"
"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))
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))
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))
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))
router.GET(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuTree, sessions, dbconnection))
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))
}
// 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)
logger.Info("done")
returndata = involved
return
}
// WebsiteAdd to add a new website
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")
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
logger.Info("done")
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
logger.Info("done")
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
logger.Info("done")
return
}