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{ID: id} db := dbconnection.First(&database) if db.Error != nil || db.RecordNotFound() { returnerr = &libapi.ErrorResult{Fields: []string{"database"}, Message: "database not found"} w.WriteHeader(http.StatusNotFound) } 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 } 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") logger = setProfilLog(r, logger) 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") logger = logger.WithField("pID", profil.ID) 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") logger = setProfilLog(r, logger) database, returnerr := getDatabase(w, r) if returnerr != nil { logger.Info(returnerr.Message) 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") logger = setProfilLog(r, logger) database, returnerr := getDatabase(w, r) if returnerr != nil { logger.Info(returnerr.Message) 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) }