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

54 lines
1.5 KiB
Go
Raw Permalink Normal View History

2016-09-11 23:04:26 +02:00
package web
import (
"net/http"
2016-10-11 20:16:24 +02:00
"goji.io/pat"
2016-09-11 23:04:26 +02:00
2016-10-11 20:16:24 +02:00
libapi "dev.sum7.eu/sum7/warehost/lib/api"
2016-09-11 23:04:26 +02:00
)
// DomainList to list domains
2016-12-19 12:24:18 +01:00
func domainList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "domainlist")
2016-09-11 23:04:26 +02:00
var domain []*Domain
2016-10-11 20:16:24 +02:00
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Domains").Find(&domain)
2016-09-11 23:04:26 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, domain, nil)
2016-09-11 23:04:26 +02:00
}
// DomainAdd to add domain
2016-12-19 12:24:18 +01:00
func domainAdd(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "domainadd")
2016-09-11 23:04:26 +02:00
domain := &Domain{
2016-10-11 20:16:24 +02:00
WebsiteID: ctx.Value("websiteid").(int64),
2016-12-19 12:24:18 +01:00
Name: pat.Param(r, "domain"),
2016-09-11 23:04:26 +02:00
}
2016-10-11 20:16:24 +02:00
if err := dbconnection.Create(domain).Error; err != nil {
2016-09-11 23:04:26 +02:00
logger.Error("database: during create website domain: ", err)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-11 23:04:26 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-11 23:04:26 +02:00
}
// DomainDelete to delete domain
2016-12-19 12:24:18 +01:00
func domainDelete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "domaindelete")
2016-09-11 23:04:26 +02:00
domain := &Domain{
2016-10-11 20:16:24 +02:00
WebsiteID: ctx.Value("websiteid").(int64),
2016-12-19 12:24:18 +01:00
Name: pat.Param(r, "domain"),
2016-09-11 23:04:26 +02:00
}
2016-10-11 20:16:24 +02:00
if err := dbconnection.Unscoped().Delete(domain).Error; err != nil {
2016-09-11 23:04:26 +02:00
logger.Error("database: during delete website Domain")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-09-11 23:04:26 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-09-11 23:04:26 +02:00
}