From ceebf4473dce1b54df40377016c4629b8bc5ce43 Mon Sep 17 00:00:00 2001 From: Pradip-p Date: Wed, 26 Jun 2024 11:19:43 +0545 Subject: [PATCH] feat: Implement bbox info on project list --- src/backend/app/projects/project_crud.py | 53 ++++++++++++++---------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index b851310f..136d5c8b 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -8,26 +8,41 @@ import shapely.wkb as wkblib from shapely.geometry import shape from fastapi import HTTPException -from app.utils import merge_multipolygon +from app.utils import geometry_to_geojson, merge_multipolygon from fmtm_splitter.splitter import split_by_square from fastapi.concurrency import run_in_threadpool from app.db import database from fastapi import Depends +from asyncio import gather async def get_project_by_id( db: Session = Depends(database.get_db), project_id: Optional[int] = None ) -> db_models.DbProject: """Get a single project by id.""" - if not project_id: - return None + db_project = ( + db.query(db_models.DbProject) + .filter(db_models.DbProject.id == project_id) + .first() + ) + return await convert_to_app_project(db_project) - db_project = db.query(db_models.DbProject).filter(db_models.DbProject.id == project_id).first() +async def convert_to_app_project(db_project: db_models.DbProject): + """Legacy function to convert db models --> Pydantic. + + TODO refactor to use Pydantic model methods instead. + """ if not db_project: - raise HTTPException( - status_code=HTTPStatus.NOT_FOUND, - detail=f"Project with ID {project_id} does not exist", + log.debug("convert_to_app_project called, but no project provided") + return None + + app_project = db_project + + if db_project.outline: + app_project.outline_geojson = geometry_to_geojson( + db_project.outline, {"id": db_project.id}, db_project.id ) - return db_project + app_project.tasks = db_project.tasks + return app_project async def get_projects( db: Session, @@ -47,24 +62,20 @@ async def get_projects( async def convert_to_app_projects( db_projects: List[db_models.DbProject], -) -> List[project_schemas.ProjectInfo]: +) -> List[project_schemas.ProjectOut]: """Legacy function to convert db models --> Pydantic. TODO refactor to use Pydantic model methods instead. """ if db_projects and len(db_projects) > 0: - filtered_projects = [ - { - "id": project.id, - "name": project.name, - "short_description": project.short_description, - "description": project.description, - "per_task_instructions": project.per_task_instructions, - "outline": None - } - for project in db_projects if project is not None - ] - return [project_schemas.ProjectOut(**project) for project in filtered_projects] + + async def convert_project(project): + return await convert_to_app_project(project) + + app_projects = await gather( + *[convert_project(project) for project in db_projects] + ) + return [project for project in app_projects if project is not None] else: return []