Implement workspace deletion

This commit is contained in:
2024-10-27 21:19:42 +01:00
parent b679af08e7
commit ba4a0dadca
4 changed files with 153 additions and 16 deletions

View File

@@ -143,24 +143,57 @@ func DeleteWorkspace(db *db.DB) http.HandlerFunc {
return
}
workspace, err := db.GetWorkspaceByID(workspaceID)
// Check if this is the user's last workspace
workspaces, err := db.GetWorkspacesByUserID(userID)
if err != nil {
http.Error(w, "Workspace not found", http.StatusNotFound)
http.Error(w, "Failed to get workspaces", http.StatusInternalServerError)
return
}
if workspace.UserID != userID {
http.Error(w, "Unauthorized access to workspace", http.StatusForbidden)
if len(workspaces) <= 1 {
http.Error(w, "Cannot delete the last workspace", http.StatusBadRequest)
return
}
if err := db.DeleteWorkspace(workspaceID); err != nil {
// Find another workspace to set as last
var nextWorkspaceID int
for _, ws := range workspaces {
if ws.ID != workspaceID {
nextWorkspaceID = ws.ID
break
}
}
// Start transaction
tx, err := db.Begin()
if err != nil {
http.Error(w, "Failed to start transaction", http.StatusInternalServerError)
return
}
defer tx.Rollback()
// Update last workspace ID first
err = db.UpdateLastWorkspaceTx(tx, userID, nextWorkspaceID)
if err != nil {
http.Error(w, "Failed to update last workspace", http.StatusInternalServerError)
return
}
// Delete the workspace
err = db.DeleteWorkspaceTx(tx, workspaceID)
if err != nil {
http.Error(w, "Failed to delete workspace", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Workspace deleted successfully"))
// Commit transaction
if err = tx.Commit(); err != nil {
http.Error(w, "Failed to commit transaction", http.StatusInternalServerError)
return
}
// Return the next workspace ID in the response so frontend knows where to redirect
respondJSON(w, map[string]int{"nextWorkspaceId": nextWorkspaceID})
}
}

View File

@@ -1,6 +1,7 @@
package db
import (
"database/sql"
"novamd/internal/models"
)
@@ -149,3 +150,13 @@ func (db *DB) DeleteWorkspace(id int) error {
_, err := db.Exec("DELETE FROM workspaces WHERE id = ?", id)
return err
}
func (db *DB) DeleteWorkspaceTx(tx *sql.Tx, id int) error {
_, err := tx.Exec("DELETE FROM workspaces WHERE id = ?", id)
return err
}
func (db *DB) UpdateLastWorkspaceTx(tx *sql.Tx, userID, workspaceID int) error {
_, err := tx.Exec("UPDATE users SET last_workspace_id = ? WHERE id = ?", workspaceID, userID)
return err
}