Added Delete Sim
This commit is contained in:
@@ -150,7 +150,7 @@ func getSimulations(w http.ResponseWriter, r *http.Request) {
|
|||||||
func getSimulationDetails(w http.ResponseWriter, r *http.Request) {
|
func getSimulationDetails(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
if r.Method == "OPTIONS" {
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -178,6 +178,11 @@ func getSimulationDetails(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.Method == "DELETE" && len(pathParts) == 1 {
|
||||||
|
deleteSimulation(w, r, idStr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" {
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
@@ -244,6 +249,44 @@ func updateSimulationTime(w http.ResponseWriter, r *http.Request, idStr string)
|
|||||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
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) {
|
func renameSimulation(w http.ResponseWriter, r *http.Request, idStr string) {
|
||||||
var reqBody struct {
|
var reqBody struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|||||||
BIN
server/server
Executable file
BIN
server/server
Executable file
Binary file not shown.
@@ -218,6 +218,27 @@
|
|||||||
saveError = "";
|
saveError = "";
|
||||||
uploadFiles = null;
|
uploadFiles = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleDelete() {
|
||||||
|
if (
|
||||||
|
!confirm(
|
||||||
|
"Are you sure you want to permanently delete this simulation and all its associated media files?",
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const res = await fetch(`/api/simulations/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
// Force client-side redirect back to cleanly wiped root
|
||||||
|
window.location.href = "/";
|
||||||
|
} else {
|
||||||
|
const text = await res.text();
|
||||||
|
alert("Failed to delete the simulation: " + text);
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<a href="/" class="back-link"><< Back to Index</a>
|
<a href="/" class="back-link"><< Back to Index</a>
|
||||||
@@ -233,6 +254,7 @@
|
|||||||
<button class="edit-btn" onclick={() => (isEditing = true)}
|
<button class="edit-btn" onclick={() => (isEditing = true)}
|
||||||
>Edit</button
|
>Edit</button
|
||||||
>
|
>
|
||||||
|
<button class="delete-btn" onclick={handleDelete}>Delete</button>
|
||||||
{:else}
|
{:else}
|
||||||
<div class="edit-form">
|
<div class="edit-form">
|
||||||
<h2>Edit Simulation</h2>
|
<h2>Edit Simulation</h2>
|
||||||
@@ -469,7 +491,8 @@
|
|||||||
}
|
}
|
||||||
.edit-btn,
|
.edit-btn,
|
||||||
.save-btn,
|
.save-btn,
|
||||||
.cancel-btn {
|
.cancel-btn,
|
||||||
|
.delete-btn {
|
||||||
padding: 6px 15px;
|
padding: 6px 15px;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
@@ -483,6 +506,15 @@
|
|||||||
.cancel-btn:hover {
|
.cancel-btn:hover {
|
||||||
background-color: #d0d0d0;
|
background-color: #d0d0d0;
|
||||||
}
|
}
|
||||||
|
.delete-btn {
|
||||||
|
background-color: #ffcccc;
|
||||||
|
color: #990000;
|
||||||
|
border-color: #990000;
|
||||||
|
margin-left: auto;
|
||||||
|
}
|
||||||
|
.delete-btn:hover {
|
||||||
|
background-color: #ff9999;
|
||||||
|
}
|
||||||
|
|
||||||
.edit-form {
|
.edit-form {
|
||||||
border: 1px dashed #000;
|
border: 1px dashed #000;
|
||||||
|
|||||||
Reference in New Issue
Block a user