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/apimail.go

142 lines
3.7 KiB
Go
Raw Normal View History

2016-10-21 21:32:30 +02:00
package host
import (
"net/http"
"strconv"
"strings"
"goji.io/pat"
"golang.org/x/net/context"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
system "dev.sum7.eu/sum7/warehost/system"
)
func getMail(ctx context.Context, w http.ResponseWriter) (mail Mail, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*system.Login)
profil := ctx.Value("profil").(*Profil)
id, err := strconv.ParseInt(pat.Param(ctx, "mailid"), 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(&mail)
} else {
dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Find(&mail)
}
if mail.ID <= 0 {
returnerr = &libapi.ErrorResult{Fields: []string{"mail"}, Message: "not found"}
w.WriteHeader(http.StatusNotFound)
}
return
}
func mailList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "maillist")
var mail []*Mail
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
dbconnection.Where("domain = ?", domain.ID).Preload("Domain").Find(&mail)
2016-10-21 21:32:30 +02:00
logger.Info("done")
returndata = mail
return
}
func mailAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "mailadd")
var mailRequest Mail
returnerr = libapi.JSONDecoder(r.Body, &mailRequest, w, logger)
if returnerr != nil {
return
}
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
mail := &Mail{
DomainID: domain.ID,
Name: mailRequest.Name,
//Forward: mailRequest.Forward,
LoginID: mailRequest.LoginID,
}
if err := dbconnection.Create(mail).Error; err != nil {
if strings.Contains(err.Error(), "duplicate key") {
logger.Warning("exists already")
return
}
logger.Error("database: during create host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func mailEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "mailedit")
mail, returnerr := getMail(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
var mailRequest Mail
returnerr = libapi.JSONDecoder(r.Body, &mailRequest, w, logger)
if returnerr != nil {
return
}
mail.Name = mailRequest.Name
//mail.Forward = mailRequest.Forward
mail.LoginID = mailRequest.LoginID
if err := dbconnection.Save(mail).Error; err != nil {
logger.Error("database: during modify host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func mailDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "maildelete")
mailRequest, returnerr := getMail(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
if err := dbconnection.Unscoped().Delete(mailRequest).Error; err != nil {
logger.Error("database: during create host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}