Add multi-user support with export feature

- New users table (migration 004) with user_id on exercises, training_sets, sessions
- User CRUD endpoints (GET/POST /api/v1/users, DELETE /api/v1/users/{id})
- All existing endpoints scoped to X-User-ID header
- CSV export endpoint (GET /api/v1/export) for completed sessions
- UserGate in PageShell: blocks app until a user is selected
- Settings page for managing users (create, switch, delete)
- BottomNav/Sidebar updated with settings navigation
- Fix: nil pointer panic in handleDeleteUser on success path
- Fix: export download now uses fetch with X-User-ID header instead of window.location.href

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Christoph K.
2026-03-21 23:55:51 +01:00
parent bff85908c3
commit a954f2c59d
24 changed files with 793 additions and 95 deletions

View File

@@ -2,6 +2,7 @@ package handler
import (
"encoding/json"
"errors"
"krafttrainer/internal/store"
"net/http"
"strconv"
@@ -19,6 +20,11 @@ func New(store *store.Store) *Handler {
// RegisterRoutes registriert alle API-Routen am ServeMux.
func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
// Users (kein X-User-ID Header nötig)
mux.HandleFunc("GET /api/v1/users", h.handleListUsers)
mux.HandleFunc("POST /api/v1/users", h.handleCreateUser)
mux.HandleFunc("DELETE /api/v1/users/{id}", h.handleDeleteUser)
// Exercises
mux.HandleFunc("GET /api/v1/exercises", h.handleListExercises)
mux.HandleFunc("POST /api/v1/exercises", h.handleCreateExercise)
@@ -46,6 +52,9 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/v1/exercises/{id}/last-log", h.handleGetLastLog)
mux.HandleFunc("GET /api/v1/exercises/{id}/history", h.handleGetExerciseHistory)
mux.HandleFunc("GET /api/v1/stats/overview", h.handleGetStatsOverview)
// Export
mux.HandleFunc("GET /api/v1/export", h.handleExport)
}
// --- Hilfsfunktionen ---
@@ -81,3 +90,16 @@ func queryInt(r *http.Request, name string, defaultVal int) int {
}
return n
}
// userID liest die X-User-ID aus dem Request-Header.
func userID(r *http.Request) (int64, error) {
v := r.Header.Get("X-User-ID")
if v == "" {
return 0, errors.New("X-User-ID Header fehlt")
}
id, err := strconv.ParseInt(v, 10, 64)
if err != nil || id <= 0 {
return 0, errors.New("ungültige User-ID")
}
return id, nil
}