Added Delete Sim

This commit is contained in:
2026-02-21 22:41:27 -05:00
parent 30c76bb939
commit d29747e9fc
3 changed files with 77 additions and 2 deletions

View File

@@ -150,7 +150,7 @@ func getSimulations(w http.ResponseWriter, r *http.Request) {
func getSimulationDetails(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")
w.WriteHeader(http.StatusOK)
return
}
@@ -178,6 +178,11 @@ func getSimulationDetails(w http.ResponseWriter, r *http.Request) {
return
}
if r.Method == "DELETE" && len(pathParts) == 1 {
deleteSimulation(w, r, idStr)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
@@ -244,6 +249,44 @@ func updateSimulationTime(w http.ResponseWriter, r *http.Request, idStr string)
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}
func deleteSimulation(w http.ResponseWriter, r *http.Request, idStr string) {
var name string
err := db.QueryRow("SELECT name FROM simulations WHERE id = ?", idStr).Scan(&name)
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "Simulation not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// Attempt dropping resource map hooks first
_, err = db.Exec("DELETE FROM resources WHERE simulation_id = ?", idStr)
if err != nil {
log.Println("Warning: Error deleting resources map from DB:", err)
}
_, err = db.Exec("DELETE FROM simulations WHERE id = ?", idStr)
if err != nil {
log.Println("Error deleting simulation from DB:", err)
http.Error(w, "Failed to delete from database", http.StatusInternalServerError)
return
}
// Physically destroy data payload mapped onto it
dirPath := filepath.Join("../results", name)
if _, statErr := os.Stat(dirPath); statErr == nil {
err = os.RemoveAll(dirPath)
if err != nil {
log.Println("Warning: Failed fully deleting physical directory items:", err)
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}
func renameSimulation(w http.ResponseWriter, r *http.Request, idStr string) {
var reqBody struct {
Name string `json:"name"`