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

139 lines
4.0 KiB
Go
Raw Permalink Normal View History

2016-10-17 14:07:17 +02:00
package host
import (
"net/http"
"strconv"
2016-10-17 14:07:17 +02:00
"goji.io/pat"
2016-10-17 14:07:17 +02:00
libapi "dev.sum7.eu/sum7/warehost/lib/api"
system "dev.sum7.eu/sum7/warehost/system"
2016-10-17 14:07:17 +02:00
)
2016-12-19 12:24:18 +01:00
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)
2016-12-19 12:24:18 +01:00
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
}
2017-06-02 10:15:09 +02:00
database = Database{ID: id}
db := dbconnection.First(&database)
2017-06-02 10:15:09 +02:00
if db.Error != nil || db.RecordNotFound() {
returnerr = &libapi.ErrorResult{Fields: []string{"database"}, Message: "database not found"}
w.WriteHeader(http.StatusNotFound)
}
2017-06-02 10:15:09 +02:00
if !login.Superadmin {
if profil.ID != database.ProfilID {
returnerr = &libapi.ErrorResult{Fields: []string{"profil"}, Message: "not allowed to get database"}
w.WriteHeader(http.StatusForbidden)
}
}
return
}
2016-12-19 12:24:18 +01:00
func databaseList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
login := ctx.Value("login").(*system.Login)
2016-10-17 14:07:17 +02:00
profil := ctx.Value("profil").(*Profil)
logger := log.GetLog(r, "databaselist")
2017-06-02 10:15:09 +02:00
logger = setProfilLog(r, logger)
2016-10-17 14:07:17 +02:00
var database []*Database
if login.Superadmin && r.URL.Query().Get("filter") == "all" {
2016-10-18 20:26:17 +02:00
dbconnection.Preload("Profil").Preload("Profil.Login").Find(&database)
} else {
2016-10-18 20:26:17 +02:00
dbconnection.Where("profil = ?", profil.ID).Preload("Profil").Find(&database)
}
2016-10-17 14:07:17 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, database, nil)
2016-10-17 14:07:17 +02:00
}
2016-12-19 12:24:18 +01:00
func databaseAdd(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-17 14:07:17 +02:00
profil := ctx.Value("profil").(*Profil)
logger := log.GetLog(r, "databaseadd")
2017-06-02 10:15:09 +02:00
logger = logger.WithField("pID", profil.ID)
2016-10-17 14:07:17 +02:00
var databaseRequest Database
2016-12-19 12:24:18 +01:00
returnerr := libapi.JSONDecoder(w, r, logger, &databaseRequest)
2016-10-17 14:07:17 +02:00
if returnerr != nil {
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-10-17 14:07:17 +02:00
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)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
2016-10-17 14:07:17 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-10-17 14:07:17 +02:00
}
2016-12-19 12:24:18 +01:00
func databaseEdit(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "databaseedit")
2017-06-02 10:15:09 +02:00
logger = setProfilLog(r, logger)
2016-12-19 12:24:18 +01:00
database, returnerr := getDatabase(w, r)
if returnerr != nil {
2017-06-02 10:15:09 +02:00
logger.Info(returnerr.Message)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("dbID", database.ID)
var databaseRequest Database
2016-12-19 12:24:18 +01:00
returnerr = libapi.JSONDecoder(w, r, logger, &databaseRequest)
if returnerr != nil {
2016-12-19 12:24:18 +01:00
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)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
}
2016-12-19 12:24:18 +01:00
func databaseDelete(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "databasedelete")
2017-06-02 10:15:09 +02:00
logger = setProfilLog(r, logger)
2016-12-19 12:24:18 +01:00
database, returnerr := getDatabase(w, r)
if returnerr != nil {
2017-06-02 10:15:09 +02:00
logger.Info(returnerr.Message)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("dbID", database.ID)
2016-12-13 11:35:44 +01:00
if err := dbconnection.Unscoped().Delete(database).Error; err != nil {
logger.Error("database: during create host database: ", err)
w.WriteHeader(http.StatusInternalServerError)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
}