From 51b94f9bf8db61cb59a54de40762521b61fc4894 Mon Sep 17 00:00:00 2001 From: "herve.le-bars" Date: Fri, 22 Nov 2024 22:21:36 +0100 Subject: [PATCH 1/2] feat: 274 api listes valeurs types, classes, countries --- .../infra/repositories/repository_vessel.py | 12 +++++++ backend/bloom/routers/v1/vessels.py | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+) 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 From 6b8ccf5933e4f5061894cd4e0264f9bc608543cf Mon Sep 17 00:00:00 2001 From: "herve.le-bars" Date: Fri, 22 Nov 2024 22:28:51 +0100 Subject: [PATCH 2/2] mod: renommae /zones/all/categorie => /zones/categories --- backend/bloom/routers/v1/zones.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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