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/host/apidomain.go

157 lines
4.2 KiB
Go
Raw Normal View History

2016-10-17 11:54:35 +02:00
package host
import (
"net/http"
"strconv"
"strings"
2016-10-17 11:54:35 +02:00
"goji.io/pat"
2016-10-17 11:54:35 +02:00
"golang.org/x/net/context"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
system "dev.sum7.eu/sum7/warehost/system"
2016-10-17 11:54:35 +02:00
)
func getDomain(ctx context.Context, w http.ResponseWriter) (domain Domain, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*system.Login)
profil := ctx.Value("profil").(*Profil)
id, err := strconv.ParseInt(pat.Param(ctx, "domainid"), 10, 64)
if err != nil {
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
if login.Superadmin {
dbconnection.Where("id = ?", id).Find(&domain)
} else {
dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&domain)
}
if domain.ID <= 0 {
returnerr = &libapi.ErrorResult{Fields: []string{"domain"}, Message: "not found"}
2016-10-21 21:32:30 +02:00
w.WriteHeader(http.StatusNotFound)
}
return
}
2016-10-17 11:54:35 +02:00
func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*system.Login)
2016-10-17 11:54:35 +02:00
profil := ctx.Value("profil").(*Profil)
returndata = false
logger := log.GetLog(r, "domainlist")
var domain []*Domain
if login.Superadmin && r.URL.Query().Get("filter") == "all" {
2016-10-18 20:26:17 +02:00
dbconnection.Preload("Profil").Preload("Profil.Login").Find(&domain)
} else {
dbconnection.Where("profil = ?", profil.ID).Find(&domain)
}
2016-10-17 11:54:35 +02:00
logger.Info("done")
returndata = domain
return
}
2016-10-22 21:20:21 +02:00
func domainShow(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "domainlist")
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
logger.Info("done")
returndata = domain
return
}
2016-10-17 14:07:17 +02:00
func domainAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
2016-10-17 11:54:35 +02:00
profil := ctx.Value("profil").(*Profil)
returndata = false
2016-10-17 14:07:17 +02:00
logger := log.GetLog(r, "domainadd")
var domainRequest Domain
returnerr = libapi.JSONDecoder(r.Body, &domainRequest, w, logger)
if returnerr != nil {
return
}
domain := &Domain{
ProfilID: profil.ID,
FQDN: domainRequest.FQDN,
Mail: domainRequest.Mail,
Web: domainRequest.Web,
}
if err := dbconnection.Create(domain).Error; err != nil {
if strings.Contains(err.Error(), "duplicate key") {
logger.Warning("exists already")
return
}
logger.Error("database: during create host domain: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func domainEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*system.Login)
returndata = false
logger := log.GetLog(r, "domainedit")
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
var domainRequest Domain
returnerr = libapi.JSONDecoder(r.Body, &domainRequest, w, logger)
if returnerr != nil {
return
}
domain.FQDN = domainRequest.FQDN
domain.Mail = domainRequest.Mail
domain.Web = domainRequest.Web
if login.Superadmin {
domain.Active = domainRequest.Active
}
if err := dbconnection.Save(domain).Error; err != nil {
logger.Error("database: during modify host domain: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func domainDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "domaindelete")
domainRequest, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
if err := dbconnection.Unscoped().Delete(domainRequest).Error; err != nil {
2016-10-17 14:07:17 +02:00
logger.Error("database: during create host domain: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
2016-10-17 11:54:35 +02:00
logger.Info("done")
return
}