diff --git a/backend/bloom/infra/repositories/repository_vessel.py b/backend/bloom/infra/repositories/repository_vessel.py index c82b5c37..6d25a066 100644 --- a/backend/bloom/infra/repositories/repository_vessel.py +++ b/backend/bloom/infra/repositories/repository_vessel.py @@ -15,6 +15,18 @@ def __init__( ) -> Callable[..., AbstractContextManager]: self.session_factory = session_factory + def get_vessel_types(self, session: Session) -> list[str]: + stmt = select(sql_model.Vessel.type).select_from(sql_model.Vessel).distinct() + return [i for i in session.execute(stmt).scalars()] + + def get_vessel_length_classes(self, session: Session) -> list[str]: + stmt = select(sql_model.Vessel.length_class).select_from(sql_model.Vessel).distinct() + return [i for i in session.execute(stmt).scalars()] + + def get_vessel_countries(self, session: Session) -> list[str]: + stmt = select(sql_model.Vessel.country_iso3).select_from(sql_model.Vessel).distinct() + return [i for i in session.execute(stmt).scalars()] + def get_vessel_by_id(self, session: Session, vessel_id: int) -> Union[Vessel, None]: return session.get(sql_model.Vessel, vessel_id) diff --git a/backend/bloom/routers/v1/vessels.py b/backend/bloom/routers/v1/vessels.py index 3313ae8b..39ad8af7 100644 --- a/backend/bloom/routers/v1/vessels.py +++ b/backend/bloom/routers/v1/vessels.py @@ -13,6 +13,38 @@ router = APIRouter() +@router.get("/vessels/types") +async def list_vessel_types(request: Request, # used by @cache + key: str = Depends(X_API_KEY_HEADER)): + check_apikey(key) + use_cases = UseCases() + vessel_repository = use_cases.vessel_repository() + db = use_cases.db() + with db.session() as session: + return vessel_repository.get_vessel_types(session) + + +@router.get("/vessels/classes") +async def list_vessel_classes(request: Request, # used by @cache + key: str = Depends(X_API_KEY_HEADER)): + check_apikey(key) + use_cases = UseCases() + vessel_repository = use_cases.vessel_repository() + db = use_cases.db() + with db.session() as session: + return vessel_repository.get_vessel_length_classes(session) + +@router.get("/vessels/countries") +async def list_vessel_countries(request: Request, # used by @cache + key: str = Depends(X_API_KEY_HEADER)): + check_apikey(key) + use_cases = UseCases() + vessel_repository = use_cases.vessel_repository() + db = use_cases.db() + with db.session() as session: + return vessel_repository.get_vessel_countries(session) + + @router.get("/vessels") @cache async def list_vessels(request: Request, # used by @cache diff --git a/backend/bloom/routers/v1/zones.py b/backend/bloom/routers/v1/zones.py index 00c4b2c5..96b184c1 100644 --- a/backend/bloom/routers/v1/zones.py +++ b/backend/bloom/routers/v1/zones.py @@ -58,7 +58,7 @@ async def list_zones_summary(request: Request, return result -@router.get("/zones/all/categories") +@router.get("/zones/categories") @cache async def list_zone_categories(request: Request, nocache: bool = False, key: str = Depends(X_API_KEY_HEADER)): check_apikey(key) @@ -66,8 +66,7 @@ async def list_zone_categories(request: Request, nocache: bool = False, key: str zone_repository = use_cases.zone_repository() db = use_cases.db() with db.session() as session: - json_data = [ZoneListView(**z.model_dump_json()) - for z in zone_repository.get_all_zone_categories(session)] + json_data = [z for z in zone_repository.get_all_zone_categories(session)] return json_data