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:
42
backend/internal/handler/export.go
Normal file
42
backend/internal/handler/export.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// handleExport gibt alle Trainingsdaten eines Nutzers als CSV zurück.
|
||||
func (h *Handler) handleExport(w http.ResponseWriter, r *http.Request) {
|
||||
uid, err := userID(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := h.store.ExportLogs(uid)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "Export fehlgeschlagen")
|
||||
return
|
||||
}
|
||||
|
||||
filename := "training-export-" + time.Now().Format("2006-01-02") + ".csv"
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
|
||||
|
||||
cw := csv.NewWriter(w)
|
||||
cw.Write([]string{"datum", "uebung", "satz", "gewicht_kg", "wiederholungen", "notiz"})
|
||||
|
||||
for _, row := range rows {
|
||||
cw.Write([]string{
|
||||
row.SessionStarted.Format("2006-01-02 15:04"),
|
||||
row.ExerciseName,
|
||||
strconv.Itoa(row.SetNumber),
|
||||
strconv.FormatFloat(row.WeightKg, 'f', 2, 64),
|
||||
strconv.Itoa(row.Reps),
|
||||
row.Note,
|
||||
})
|
||||
}
|
||||
cw.Flush()
|
||||
}
|
||||
Reference in New Issue
Block a user