Add /stop_shape/{stop_id} endpoint

This commit is contained in:
2023-04-13 21:19:01 +02:00
parent 62a9000ec2
commit 61097fe9e2

View File

@@ -9,8 +9,8 @@ from fastapi.staticfiles import StaticFiles
from rich import print from rich import print
from backend.db import db from backend.db import db
from backend.models import Line, Stop, StopArea
from backend.idfm_interface import Destinations as IdfmDestinations, IdfmInterface from backend.idfm_interface import Destinations as IdfmDestinations, IdfmInterface
from backend.models import Line, Stop, StopArea, StopShape
from backend.schemas import ( from backend.schemas import (
Line as LineSchema, Line as LineSchema,
TransportMode, TransportMode,
@@ -18,6 +18,7 @@ from backend.schemas import (
NextPassages as NextPassagesSchema, NextPassages as NextPassagesSchema,
Stop as StopSchema, Stop as StopSchema,
StopArea as StopAreaSchema, StopArea as StopAreaSchema,
StopShape as StopShapeSchema,
) )
API_KEY = environ.get("API_KEY") API_KEY = environ.get("API_KEY")
@@ -223,3 +224,34 @@ async def get_stop_destinations(
destinations = await idfm_interface.get_destinations(stop_id) destinations = await idfm_interface.get_destinations(stop_id)
return destinations 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)