56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
"""Scenario API endpoints: health, validate, sample."""
|
|
|
|
import json
|
|
import os
|
|
|
|
from fastapi import APIRouter
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.config import VERSION
|
|
from app.models.event import ScenarioDocument
|
|
from app.models.responses import HealthResponse, ValidationResponse
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/health", response_model=HealthResponse)
|
|
async def health():
|
|
return HealthResponse(version=VERSION)
|
|
|
|
|
|
@router.post("/validate", response_model=ValidationResponse)
|
|
async def validate_scenario(doc: ScenarioDocument):
|
|
"""Validate a ScenarioDocument."""
|
|
errors = []
|
|
|
|
if not doc.blocks:
|
|
errors.append("No blocks defined")
|
|
|
|
if not doc.program_types:
|
|
errors.append("No program types defined")
|
|
|
|
type_ids = {pt.id for pt in doc.program_types}
|
|
for i, block in enumerate(doc.blocks):
|
|
if block.type_id not in type_ids:
|
|
errors.append(f"Block {i+1}: unknown type '{block.type_id}'")
|
|
if block.start == block.end:
|
|
errors.append(f"Block {i+1}: start time must differ from end time")
|
|
# Note: end < start is allowed for overnight blocks (block crosses midnight)
|
|
|
|
return ValidationResponse(valid=len(errors) == 0, errors=errors)
|
|
|
|
|
|
@router.get("/sample")
|
|
async def get_sample():
|
|
"""Return sample ScenarioDocument JSON."""
|
|
sample_path = os.path.join(os.path.dirname(__file__), "..", "static", "sample.json")
|
|
sample_path = os.path.abspath(sample_path)
|
|
|
|
with open(sample_path, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
return JSONResponse(
|
|
content=data,
|
|
headers={"Content-Disposition": "attachment; filename=sample.json"}
|
|
)
|