Phase 9a: Schulkalender — Bundesland-Auswahl + Monatsansicht mit Ferien
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 29s
CI / test-go-edu-search (push) Successful in 27s
CI / test-python-klausur (push) Failing after 2m50s
CI / test-python-agent-core (push) Successful in 18s
CI / test-nodejs-website (push) Successful in 21s
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 29s
CI / test-go-edu-search (push) Successful in 27s
CI / test-python-klausur (push) Failing after 2m50s
CI / test-python-agent-core (push) Successful in 18s
CI / test-nodejs-website (push) Successful in 21s
Backend (school-service):
- cal_public_event (region, event_type, name_de, name_en, start/end,
UNIQUE(region, event_type, name_de, start_date)) — global snapshot.
- cal_school_config (user_id PRIMARY KEY, bundesland, school year dates).
- cal_school_event — Schul-eigene Termine; CRUD folgt in 9b.
- GET /calendar/holidays?region=&from=&to= — Range-Query against
cal_public_event, ordered by start_date.
- GET / PUT /calendar/config — upsert Bundesland per User.
- SeedFromSnapshot reads internal/seed/calendar_holidays.json on every
boot; idempotent via the unique constraint. Async goroutine so the
HTTP server starts immediately even if the seed file is large.
Data source:
- scripts/calendar-snapshot.sh ruft openholidaysapi.org fuer alle 16
Bundeslaender x 3 Schuljahre und schreibt
school-service/internal/seed/calendar_holidays.json (854 Events,
Stand Schuljahre 2026-2028).
- Dockerfile kopiert das seed/-Verzeichnis ins Image, damit die
Container-Datenbank beim ersten Start gefuellt wird.
Frontend (studio-v2):
- /schulkalender Page mit Gradient + Blobs wie /stundenplan und
/korrektur — gleicher Visual-Style.
- BundeslandWizard: zeigt alle 16 Laender als Dropdown, speichert
bei Klick die Config und switcht zur Monatsansicht.
- MonthView: 6-Wochen-Grid Mo-So, Feiertage rose-toned, Schulferien
amber-toned, heutiges Datum mit Indigo-Ring. Prev/Next/Heute
Navigation.
- lib/schulkalender/api.ts re-uses the stundenplan JWT helper so
auth-mode wechselt nicht.
- Sidebar bekommt einen Schulkalender-Eintrag (Icon mit Datum-Dots,
Pfad /schulkalender) in allen 26 Sprachen.
Tests:
- Go: 3 neue Validator-Tests (Bundesland len=5, EventType oneof,
Pflichtfelder). 77 Tests gesamt, alle gruen.
- Playwright: e2e/schulkalender.spec.ts mit Wizard, Save-Flow,
MonthView-Render, Heute-Button, Sidebar-Link. Hermetisch via
mockCalendarApi.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/breakpilot/school-service/internal/models"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// CalendarService owns the cal_* tables and the read of the seed snapshot
|
||||
// on first boot. Holidays are global (no owner) — same data for every
|
||||
// school in a given Bundesland.
|
||||
type CalendarService struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewCalendarService(db *pgxpool.Pool) *CalendarService {
|
||||
return &CalendarService{db: db}
|
||||
}
|
||||
|
||||
// SeedFromSnapshot reads internal/seed/calendar_holidays.json and bulk-inserts
|
||||
// every row that doesn't already exist (idempotent via the unique constraint
|
||||
// on region+event_type+name_de+start_date). Called once at server start.
|
||||
func (s *CalendarService) SeedFromSnapshot(ctx context.Context, path string) error {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("calendar seed file not found at %s — skipping", path)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("read snapshot: %w", err)
|
||||
}
|
||||
|
||||
var events []models.PublicEvent
|
||||
if err := json.Unmarshal(raw, &events); err != nil {
|
||||
return fmt.Errorf("parse snapshot: %w", err)
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
inserted := 0
|
||||
for _, e := range events {
|
||||
ct, err := tx.Exec(ctx, `
|
||||
INSERT INTO cal_public_event (region, event_type, name_de, name_en, start_date, end_date, source)
|
||||
VALUES ($1, $2, $3, NULLIF($4, ''), $5::date, $6::date, 'OpenHolidaysAPI')
|
||||
ON CONFLICT (region, event_type, name_de, start_date) DO NOTHING
|
||||
`, e.Region, e.EventType, e.NameDe, e.NameEn, e.StartDate, e.EndDate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert event: %w", err)
|
||||
}
|
||||
inserted += int(ct.RowsAffected())
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("calendar seed: %d new events inserted (of %d in snapshot)", inserted, len(events))
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListHolidays returns all public + school holidays for the given region
|
||||
// between from..to (YYYY-MM-DD inclusive).
|
||||
func (s *CalendarService) ListHolidays(ctx context.Context, region, from, to string) ([]models.PublicEvent, error) {
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT id, region, event_type, name_de, COALESCE(name_en, ''),
|
||||
start_date::text, end_date::text, COALESCE(source, ''), created_at
|
||||
FROM cal_public_event
|
||||
WHERE region = $1
|
||||
AND end_date >= $2::date
|
||||
AND start_date <= $3::date
|
||||
ORDER BY start_date, event_type, name_de
|
||||
`, region, from, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []models.PublicEvent
|
||||
for rows.Next() {
|
||||
var e models.PublicEvent
|
||||
if err := rows.Scan(&e.ID, &e.Region, &e.EventType, &e.NameDe, &e.NameEn,
|
||||
&e.StartDate, &e.EndDate, &e.Source, &e.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GetConfig returns the per-user calendar config (Bundesland etc.) or nil
|
||||
// if the user has not configured one yet.
|
||||
func (s *CalendarService) GetConfig(ctx context.Context, userID string) (*models.SchoolCalendarConfig, error) {
|
||||
var c models.SchoolCalendarConfig
|
||||
err := s.db.QueryRow(ctx, `
|
||||
SELECT user_id, bundesland, school_year_start::text, school_year_end::text, created_at, updated_at
|
||||
FROM cal_school_config WHERE user_id = $1
|
||||
`, userID).Scan(&c.UserID, &c.Bundesland, &c.SchoolYearStart, &c.SchoolYearEnd, &c.CreatedAt, &c.UpdatedAt)
|
||||
if err != nil {
|
||||
// pgx returns no-rows error; caller maps to 404.
|
||||
return nil, err
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// UpsertConfig inserts or updates the Bundesland selection.
|
||||
func (s *CalendarService) UpsertConfig(ctx context.Context, userID string, req *models.UpsertSchoolCalendarConfigRequest) (*models.SchoolCalendarConfig, error) {
|
||||
var c models.SchoolCalendarConfig
|
||||
err := s.db.QueryRow(ctx, `
|
||||
INSERT INTO cal_school_config (user_id, bundesland, school_year_start, school_year_end)
|
||||
VALUES ($1, $2, NULLIF($3, '')::date, NULLIF($4, '')::date)
|
||||
ON CONFLICT (user_id) DO UPDATE
|
||||
SET bundesland = EXCLUDED.bundesland,
|
||||
school_year_start = EXCLUDED.school_year_start,
|
||||
school_year_end = EXCLUDED.school_year_end,
|
||||
updated_at = NOW()
|
||||
RETURNING user_id, bundesland, school_year_start::text, school_year_end::text, created_at, updated_at
|
||||
`, userID, req.Bundesland, strOrEmpty(req.SchoolYearStart), strOrEmpty(req.SchoolYearEnd)).
|
||||
Scan(&c.UserID, &c.Bundesland, &c.SchoolYearStart, &c.SchoolYearEnd, &c.CreatedAt, &c.UpdatedAt)
|
||||
return &c, err
|
||||
}
|
||||
|
||||
func strOrEmpty(s *string) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return *s
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/breakpilot/school-service/internal/models"
|
||||
)
|
||||
|
||||
func TestUpsertSchoolCalendarConfigRequest_Validation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
req models.UpsertSchoolCalendarConfigRequest
|
||||
wantErr bool
|
||||
}{
|
||||
{"valid NI", models.UpsertSchoolCalendarConfigRequest{Bundesland: "DE-NI"}, false},
|
||||
{"empty bundesland", models.UpsertSchoolCalendarConfigRequest{Bundesland: ""}, true},
|
||||
{"too long", models.UpsertSchoolCalendarConfigRequest{Bundesland: "DE-NIE"}, true},
|
||||
{"too short", models.UpsertSchoolCalendarConfigRequest{Bundesland: "DE"}, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if (validate.Struct(tt.req) != nil) != tt.wantErr {
|
||||
t.Errorf("unexpected validation outcome for %s", tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSchoolEventRequest_Validation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
req models.CreateSchoolEventRequest
|
||||
wantErr bool
|
||||
}{
|
||||
{"valid fortbildung", models.CreateSchoolEventRequest{
|
||||
Title: "SCHILF", EventType: "fortbildung",
|
||||
StartDate: "2026-10-01", EndDate: "2026-10-01",
|
||||
}, false},
|
||||
{"missing title", models.CreateSchoolEventRequest{
|
||||
EventType: "fortbildung", StartDate: "2026-10-01", EndDate: "2026-10-01",
|
||||
}, true},
|
||||
{"invalid event type", models.CreateSchoolEventRequest{
|
||||
Title: "X", EventType: "wedding",
|
||||
StartDate: "2026-10-01", EndDate: "2026-10-01",
|
||||
}, true},
|
||||
{"missing dates", models.CreateSchoolEventRequest{
|
||||
Title: "X", EventType: "schulfeier",
|
||||
}, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if (validate.Struct(tt.req) != nil) != tt.wantErr {
|
||||
t.Errorf("unexpected validation outcome for %s", tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCalendarService_Constructs(t *testing.T) {
|
||||
s := NewCalendarService(nil)
|
||||
if s == nil {
|
||||
t.Fatal("expected non-nil service")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user