diff --git a/backend/python/app/routers/route_routes.py b/backend/python/app/routers/route_routes.py index 778b069..5f2314c 100644 --- a/backend/python/app/routers/route_routes.py +++ b/backend/python/app/routers/route_routes.py @@ -6,7 +6,7 @@ from app.dependencies.auth import require_driver from app.models import get_session -from app.models.route import RouteWithDateRead +from app.models.route import Route, RouteWithDateRead from app.services.implementations.route_service import RouteService # Initialize service @@ -38,6 +38,35 @@ async def get_routes( return routes +@router.get("/{route_id}", response_model=Route, status_code=status.HTTP_200_OK) +async def get_route( + route_id: UUID, + session: AsyncSession = Depends(get_session), +) -> Route: + """ + Get a route by its unique identifier. + + Parameters: + route_id (UUID): The unique identifier of the route to delete. + session (AsyncSession): The database session dependency. + + Authentication: + Requires the user to be authenticated as a driver. + + Returns: + None. Responds with HTTP 200 OK on successful get. + + Raises: + HTTPException: + - 404 Not Found: If the route with the specified ID does not exist. + - 500 Server Error + """ + + # TODO: the auth here does not work, I think this is an auth issue + route = await route_service.get_route(session, route_id) + return route + + @router.delete("/{route_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_route( route_id: UUID, diff --git a/backend/python/app/services/implementations/route_service.py b/backend/python/app/services/implementations/route_service.py index f012f95..9dc1c3d 100644 --- a/backend/python/app/services/implementations/route_service.py +++ b/backend/python/app/services/implementations/route_service.py @@ -2,6 +2,7 @@ from datetime import datetime from uuid import UUID +from fastapi import HTTPException from sqlalchemy import and_, exists from sqlalchemy import select as sql_select from sqlalchemy.ext.asyncio import AsyncSession @@ -92,6 +93,25 @@ async def get_routes( for row in rows ] + async def get_route(self, session: AsyncSession, route_id: UUID) -> Route: + try: + """Get route by ID""" + statement = select(Route).where(Route.route_id == route_id) + result = await session.execute(statement) + route = result.scalars().first() + + if not route: + self.logger.error(f"Route with id {route_id} not found") + raise ValueError(f"Route with id {route_id} not found") + + return route + except Exception as error: + self.logger.exception("Failed to get route %s", route_id) + await session.rollback() + raise HTTPException( + status_code=500, detail="Failed to retrieve route." + ) from error + async def delete_route(self, session: AsyncSession, route_id: UUID) -> bool: """Delete route by ID""" try: @@ -110,4 +130,5 @@ async def delete_route(self, session: AsyncSession, route_id: UUID) -> bool: except Exception as error: self.logger.error(f"Failed to delete route {route_id}: {error!s}") await session.rollback() + # TODO: do we really want to return the raw error raise error