diff --git a/backend/main.py b/backend/main.py index ebf50be..7387c1f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -9,8 +9,8 @@ from fastapi.staticfiles import StaticFiles from rich import print from backend.db import db -from backend.models import Line, Stop, StopArea from backend.idfm_interface import Destinations as IdfmDestinations, IdfmInterface +from backend.models import Line, Stop, StopArea, StopShape from backend.schemas import ( Line as LineSchema, TransportMode, @@ -18,6 +18,7 @@ from backend.schemas import ( NextPassages as NextPassagesSchema, Stop as StopSchema, StopArea as StopAreaSchema, + StopShape as StopShapeSchema, ) API_KEY = environ.get("API_KEY") @@ -223,3 +224,34 @@ async def get_stop_destinations( destinations = await idfm_interface.get_destinations(stop_id) return destinations + + +# TODO: Rename endpoint -> /stop/{stop_id}/shape +@app.get("/stop_shape/{stop_id}") +async def get_stop_shape(stop_id: int) -> StopShapeSchema | None: + connection_area = None + + if (stop := await Stop.get_by_id(stop_id)) is not None: + connection_area = stop.connection_area + + elif (stop_area := await StopArea.get_by_id(stop_id)) is not None: + connection_areas = {stop.connection_area for stop in stop_area.stops} + connection_areas_len = len(connection_areas) + if connection_areas_len == 1: + connection_area = connection_areas.pop() + + else: + prefix = "More than one" if connection_areas_len else "No" + msg = f"{prefix} connection area has been found for stop area #{stop_id}" + raise HTTPException(status_code=500, detail=msg) + + if ( + connection_area is not None + and (shape := await StopShape.get_by_id(connection_area.id)) is not None + ): + return StopShapeSchema( + id=shape.id, type=shape.type, bbox=shape.bounding_box, points=shape.points + ) + + msg = f"No shape found for stop {stop_id}" + raise HTTPException(status_code=404, detail=msg)