Skip to content

Commit

Permalink
Merge pull request #9 from Pradip-p/feature/project-retrieval
Browse files Browse the repository at this point in the history
feat: Implement bbox info on project list
  • Loading branch information
nrjadkry authored Jun 26, 2024
2 parents d7d7248 + ceebf44 commit 117d5e8
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 []

Expand Down

0 comments on commit 117d5e8

Please sign in to comment.