From 3031be736f8038dc16da5bf8465700e301395ca4 Mon Sep 17 00:00:00 2001 From: "herve.le-bars" Date: Fri, 22 Nov 2024 23:02:45 +0100 Subject: [PATCH] feat: ajout endpoint /api/v1/vessels/trackedCount --- backend/bloom/infra/repositories/repository_vessel.py | 6 ++++++ backend/bloom/routers/v1/vessels.py | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/backend/bloom/infra/repositories/repository_vessel.py b/backend/bloom/infra/repositories/repository_vessel.py index 6d25a066..f3a4a842 100644 --- a/backend/bloom/infra/repositories/repository_vessel.py +++ b/backend/bloom/infra/repositories/repository_vessel.py @@ -15,6 +15,12 @@ def __init__( ) -> Callable[..., AbstractContextManager]: self.session_factory = session_factory + + def get_vessel_tracked_count(self, session: Session) -> int: + stmt = select(func.count(sql_model.Vessel.id)).select_from(sql_model.Vessel)\ + .distinct().where(sql_model.Vessel.tracking_activated == True) + return session.execute(stmt).scalar() + 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()] diff --git a/backend/bloom/routers/v1/vessels.py b/backend/bloom/routers/v1/vessels.py index 39ad8af7..24d7242c 100644 --- a/backend/bloom/routers/v1/vessels.py +++ b/backend/bloom/routers/v1/vessels.py @@ -13,6 +13,16 @@ router = APIRouter() +@router.get("/vessels/trackedCount") +async def list_vessel_tracked(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_tracked_count(session) + @router.get("/vessels/types") async def list_vessel_types(request: Request, # used by @cache key: str = Depends(X_API_KEY_HEADER)):