27 lines
964 B
Python
27 lines
964 B
Python
"""Departments and their stations (read-only in phase 1)."""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends
|
||
|
|
|
||
|
|
from .deps import Db, Principal, current_principal, get_db
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api", tags=["departments"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/departments")
|
||
|
|
def departments(_: Principal = Depends(current_principal), d: Db = Depends(get_db)) -> list[dict[str, Any]]: # noqa: B008
|
||
|
|
conn = d.conn
|
||
|
|
rows = conn.execute(
|
||
|
|
"SELECT code, slug, name, kind::text FROM departments WHERE active ORDER BY sort, code"
|
||
|
|
).fetchall()
|
||
|
|
stations = conn.execute(
|
||
|
|
"SELECT dept_code, name FROM stations WHERE active ORDER BY dept_code, sort, name"
|
||
|
|
).fetchall()
|
||
|
|
by_dept: dict[str, list[str]] = {}
|
||
|
|
for code, name in stations:
|
||
|
|
by_dept.setdefault(code, []).append(name)
|
||
|
|
return [{"code": r[0], "slug": r[1], "name": r[2], "kind": r[3], "stations": by_dept.get(r[0], [])} for r in rows]
|