Add delete session functionality

- DELETE /api/v1/sessions/{id}: only closed sessions, user-scoped
- Returns 404 if not found/wrong user, 409 if session still open
- Deletes session_logs first, then session (no CASCADE)
- Frontend: trash button per session in SessionList (closed sessions only)
- Confirm dialog before delete, toast feedback, list reloads after

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Christoph K.
2026-03-23 20:50:48 +01:00
parent 6d7d353ea2
commit 833ad04a6f
6 changed files with 147 additions and 27 deletions

View File

@@ -42,6 +42,7 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/v1/sessions", h.handleListSessions)
mux.HandleFunc("GET /api/v1/sessions/{id}", h.handleGetSession)
mux.HandleFunc("PUT /api/v1/sessions/{id}/end", h.handleEndSession)
mux.HandleFunc("DELETE /api/v1/sessions/{id}", h.handleDeleteSession)
// Session Logs
mux.HandleFunc("POST /api/v1/sessions/{id}/logs", h.handleCreateLog)

View File

@@ -101,6 +101,38 @@ func (h *Handler) handleEndSession(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, session)
}
// handleDeleteSession handles DELETE /api/v1/sessions/{id}.
// Löscht eine abgeschlossene Session samt aller Logs. Offene Sessions werden
// mit 409 abgelehnt. Sessions anderer Nutzer oder nicht vorhandene Sessions
// antworten mit 404.
func (h *Handler) handleDeleteSession(w http.ResponseWriter, r *http.Request) {
uid, err := userID(r)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
id, err := pathID(r, "id")
if err != nil {
writeError(w, http.StatusBadRequest, "Ungültige ID")
return
}
err = h.store.DeleteSession(id, uid)
if err != nil {
if strings.Contains(err.Error(), "SESSION_NOT_FOUND") {
writeError(w, http.StatusNotFound, "Session nicht gefunden")
return
}
if strings.Contains(err.Error(), "SESSION_OPEN") {
writeError(w, http.StatusConflict, "Nur abgeschlossene Sessions können gelöscht werden")
return
}
writeError(w, http.StatusInternalServerError, "Fehler beim Löschen der Session")
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) handleCreateLog(w http.ResponseWriter, r *http.Request) {
sessionID, err := pathID(r, "id")
if err != nil {