Skip to content

Commit

Permalink
feat(backend): get api for project's geometry log (#2090)
Browse files Browse the repository at this point in the history
* feat: create get api for project's geometry log

* refactor: better naming of endpoints adding proper docstrings
  • Loading branch information
Sujanadh authored Jan 15, 2025
1 parent 63cd741 commit 5e8753b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/backend/app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,25 @@ async def create(
new_geomlog = await cur.fetchone()
return new_geomlog

@classmethod
async def all(cls, db: Connection, project_id: int) -> Optional[list[Self]]:
"""Retrieve geometry logs from a project."""
async with db.cursor(row_factory=class_row(cls)) as cur:
await cur.execute(
"""
SELECT * FROM geometrylog WHERE project_id=%(project_id)s;
""",
{"project_id": project_id},
)
if cur.rowcount == 0:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"""
No geometry log with project_id {project_id}
""",
)
return await cur.fetchall()

@classmethod
async def delete(
cls,
Expand Down
32 changes: 28 additions & 4 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,6 @@ async def update_project(

@router.post("/{project_id}/upload-task-boundaries")
async def upload_project_task_boundaries(
project_id: int,
db: Annotated[Connection, Depends(db_conn)],
project_user_dict: Annotated[ProjectUserDict, Depends(project_manager)],
task_geojson: UploadFile = File(...),
Expand All @@ -1084,6 +1083,7 @@ async def upload_project_task_boundaries(
Returns:
JSONResponse: JSON containing success message.
"""
project_id = project_user_dict.get("project").id
tasks_featcol = parse_geojson_file_to_featcol(await task_geojson.read())
await check_crs(tasks_featcol)
# We only want to allow polygon geometries
Expand Down Expand Up @@ -1241,7 +1241,6 @@ async def download_project_boundary(

@router.get("/{project_id}/download_tasks")
async def download_task_boundaries(
project_id: int,
db: Annotated[Connection, Depends(db_conn)],
project_user: Annotated[ProjectUserDict, Depends(mapper)],
):
Expand All @@ -1266,7 +1265,7 @@ async def download_task_boundaries(
return Response(content=out, headers=headers)


@router.post("/{project_id}/geometries")
@router.post("/{project_id}/geometry/records")
async def create_geom_log(
geom_log: project_schemas.GeometryLogIn,
current_user: Annotated[ProjectUserDict, Depends(mapper)],
Expand Down Expand Up @@ -1310,7 +1309,32 @@ async def create_geom_log(
return geometries


@router.delete("/{project_id}/geometries")
@router.get(
"{project_id}/geometry/records", response_model=list[project_schemas.GeometryLogIn]
)
async def read_geom_logs(
db: Annotated[Connection, Depends(db_conn)],
project_user: Annotated[ProjectUserDict, Depends(mapper)],
):
"""Retrieve all geometry logs for a specific project.
This endpoint fetches geometry records.
- Bad submitted feature and
- new feature drawn in a project
Args:
db: The database connection.
project_user: The currently authenticated project user details.
Returns:
list[project_schemas.GeometryLogIn]: A list of geometry log entries.
"""
project_id = project_user.get("project").id
geometries = await DbGeometryLog.all(db, project_id)
return geometries


@router.delete("/{project_id}/geometry/records/{geom_id}")
async def delete_geom_log(
geom_id: str,
project_user: Annotated[
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def parse_input_geometry(
if value is None:
return None
featcol = geojson_to_featcol(value)
return featcol.get("features")[0].get("geometry")
return featcol.get("features")[0]


class ProjectInBase(DbProject):
Expand Down

0 comments on commit 5e8753b

Please sign in to comment.