Files
breakpilot-lehrer/school-service/internal/handlers/timetable_export_handlers.go
T
Benjamin Admin 306886a42b Phase 8: CSV + ICS export, print view, MkDocs docs, SBOM + dev-mode auth
Auth (Test-Mode):
  - middleware.AuthMiddleware now takes a devMode flag. In dev,
    requests without Authorization fall back to a deterministic dev
    UUID (00000000-...-001) and role=teacher. ENVIRONMENT=production
    re-enables the strict 401 path.
  - main.go wires devMode = cfg.Environment != "production".
  - page.tsx replaces the red 'Anmeldung noch nicht integriert' banner
    with a softer Testumgebung notice; the manual-token form moves
    behind a nested details block.

Export endpoints (school-service):
  - LoadExportLessons joins tt_lesson with tt_period for wall-clock
    times; one query feeds both CSV and ICS.
  - WriteCSV streams 10 columns including pinned flag.
  - WriteICS emits one VEVENT per lesson anchored to a Monday — caller
    overridable via ?start=YYYY-MM-DD. RFC 5545 escapes for ',', ';',
    '\n' in icsEscape().
  - NextMonday helper for the default anchor.
  - GET /timetable/solutions/:id/export.{csv,ics} handlers attach
    Content-Disposition: attachment so browsers download instead of
    rendering.

Frontend:
  - lib/stundenplan/api.ts downloadSolutionExport() fetches as blob,
    triggers a synthetic <a download> click, and forwards the JWT when
    present.
  - PlanView gains CSV / ICS / Drucken buttons next to the perspective
    selector. The toolbar carries class 'no-print' so window.print()
    yields only the grid.
  - globals.css @media print rule hides chrome, forces white
    background, gives the table proper borders for A4.

Docs:
  - docs-src/services/stundenplan/{index,architecture,constraints,
    solver-tuning,export}.md with nav entry in mkdocs.yml under
    Services → Stundenplaner.
  - sbom/stundenplan/README.md lists manually-verified key dependencies
    and the policy reference. scripts/stundenplan-sbom.sh generates
    full machine-readable inventories via go-licenses + pip-licenses
    + license-checker when those tools are available.

Tests:
  - internal/services/timetable_exports_test.go: 4 unit tests covering
    CSV column layout + quoting, ICS structure + DTSTART formatting,
    icsEscape special chars, NextMonday weekday math.
  - studio-v2/e2e/stundenplan-export.spec.ts split out of the main spec
    file (LOC budget) — 3 tests for button render, CSV download,
    ICS download.
  - mockSchoolApi extended with export.csv + export.ics routes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 08:57:07 +02:00

75 lines
2.4 KiB
Go

package handlers
import (
"net/http"
"time"
"github.com/breakpilot/school-service/internal/services"
"github.com/gin-gonic/gin"
)
// ExportTimetableSolutionCSV streams the lessons of a solution as CSV.
// The download filename includes the solution UUID so multiple plans don't
// clobber each other when saved to disk.
func (h *Handler) ExportTimetableSolutionCSV(c *gin.Context) {
uid := getUserID(c)
if uid == "" {
respondError(c, http.StatusUnauthorized, "User not authenticated")
return
}
solutionID := c.Param("id")
lessons, err := h.timetableService.LoadExportLessons(c.Request.Context(), solutionID, uid)
if err != nil {
respondError(c, http.StatusInternalServerError, "Failed to load lessons: "+err.Error())
return
}
c.Header("Content-Type", "text/csv; charset=utf-8")
c.Header("Content-Disposition", `attachment; filename="stundenplan-`+solutionID+`.csv"`)
c.Writer.WriteHeader(http.StatusOK)
if err := services.WriteCSV(c.Writer, lessons); err != nil {
// Best-effort; headers already flushed.
_ = c.Writer.Flush
}
}
// ExportTimetableSolutionICS emits a single-week iCalendar. The reference
// Monday defaults to the next Monday from "now"; callers can override via
// ?start=YYYY-MM-DD to align to a school year.
func (h *Handler) ExportTimetableSolutionICS(c *gin.Context) {
uid := getUserID(c)
if uid == "" {
respondError(c, http.StatusUnauthorized, "User not authenticated")
return
}
solutionID := c.Param("id")
weekStart := services.NextMonday(time.Now())
if param := c.Query("start"); param != "" {
parsed, err := time.Parse("2006-01-02", param)
if err != nil {
respondError(c, http.StatusBadRequest, "start must be YYYY-MM-DD")
return
}
weekStart = parsed
}
sol, err := h.timetableService.GetSolution(c.Request.Context(), solutionID, uid)
if err != nil {
respondError(c, http.StatusNotFound, "Solution not found")
return
}
lessons, err := h.timetableService.LoadExportLessons(c.Request.Context(), solutionID, uid)
if err != nil {
respondError(c, http.StatusInternalServerError, "Failed to load lessons: "+err.Error())
return
}
c.Header("Content-Type", "text/calendar; charset=utf-8")
c.Header("Content-Disposition", `attachment; filename="stundenplan-`+solutionID+`.ics"`)
c.Writer.WriteHeader(http.StatusOK)
if err := services.WriteICS(c.Writer, lessons, weekStart, sol.Name); err != nil {
// Same best-effort situation as CSV.
_ = c.Writer.Flush
}
}