133 lines
3.7 KiB
Go
133 lines
3.7 KiB
Go
package host
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"goji.io/pat"
|
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
|
system "dev.sum7.eu/sum7/warehost/system"
|
|
)
|
|
|
|
func getDatabase(w http.ResponseWriter, r *http.Request) (database Database, returnerr *libapi.ErrorResult) {
|
|
ctx := r.Context()
|
|
login := ctx.Value("login").(*system.Login)
|
|
profil := ctx.Value("profil").(*Profil)
|
|
id, err := strconv.ParseInt(pat.Param(r, "databaseid"), 10, 64)
|
|
if err != nil {
|
|
returnerr = &libapi.ErrorResult{
|
|
Message: "Internal Request Error",
|
|
}
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
database = Database{}
|
|
|
|
if login.Superadmin {
|
|
dbconnection.Where("ID = ?", id).Find(&database)
|
|
} else {
|
|
dbconnection.Where(map[string]int64{"ID": id, "profil": profil.ID}).Find(&database)
|
|
}
|
|
|
|
if database.ID <= 0 {
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"database"}, Message: "not found"}
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
return
|
|
}
|
|
|
|
func databaseList(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
login := ctx.Value("login").(*system.Login)
|
|
profil := ctx.Value("profil").(*Profil)
|
|
logger := log.GetLog(r, "databaselist")
|
|
var database []*Database
|
|
if login.Superadmin && r.URL.Query().Get("filter") == "all" {
|
|
dbconnection.Preload("Profil").Preload("Profil.Login").Find(&database)
|
|
} else {
|
|
dbconnection.Where("profil = ?", profil.ID).Preload("Profil").Find(&database)
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, database, nil)
|
|
}
|
|
|
|
func databaseAdd(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
profil := ctx.Value("profil").(*Profil)
|
|
logger := log.GetLog(r, "databaseadd")
|
|
|
|
var databaseRequest Database
|
|
returnerr := libapi.JSONDecoder(w, r, logger, &databaseRequest)
|
|
if returnerr != nil {
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
|
|
database := &Database{
|
|
ProfilID: profil.ID,
|
|
Password: databaseRequest.Password,
|
|
Comment: databaseRequest.Comment,
|
|
}
|
|
|
|
if err := dbconnection.Create(database).Error; err != nil {
|
|
logger.Error("database: during create host database: ", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
|
|
return
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, true, nil)
|
|
}
|
|
|
|
func databaseEdit(w http.ResponseWriter, r *http.Request) {
|
|
logger := log.GetLog(r, "databaseedit")
|
|
|
|
database, returnerr := getDatabase(w, r)
|
|
if returnerr != nil {
|
|
logger.Info("not found")
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
logger = logger.WithField("dbID", database.ID)
|
|
|
|
var databaseRequest Database
|
|
returnerr = libapi.JSONDecoder(w, r, logger, &databaseRequest)
|
|
if returnerr != nil {
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
}
|
|
|
|
database.Password = databaseRequest.Password
|
|
database.Comment = databaseRequest.Comment
|
|
|
|
if err := dbconnection.Save(database).Error; err != nil {
|
|
logger.Error("database: during modify host database: ", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
|
|
return
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, true, nil)
|
|
}
|
|
|
|
func databaseDelete(w http.ResponseWriter, r *http.Request) {
|
|
logger := log.GetLog(r, "databasedelete")
|
|
|
|
database, returnerr := getDatabase(w, r)
|
|
if returnerr != nil {
|
|
logger.Info("not found")
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
logger = logger.WithField("dbID", database.ID)
|
|
|
|
if err := dbconnection.Unscoped().Delete(database).Error; err != nil {
|
|
logger.Error("database: during create host database: ", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
|
|
return
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, true, nil)
|
|
}
|