Skip to content

Commit

Permalink
Add endpoint to retrieve pictures associated with an inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
snakedye committed Dec 18, 2024
1 parent 2fa132b commit 5d8d334
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/controllers/inspections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
get_user_analysis_by_verified,
register_analysis,
)
from datastore.db.queries.picture import (
get_picture_set_pictures
)
from fertiscan import update_inspection as db_update_inspection
from fertiscan.db.queries.inspection import (
InspectionNotFoundError as DBInspectionNotFoundError,
Expand Down Expand Up @@ -70,6 +73,24 @@ async def read_all_inspections(cp: ConnectionPool, user: User):

return inspections

async def get_pictures(cp: ConnectionPool, user: User, id: UUID | str):
"""
Retrieves the pictures associated with a user by inspection ID.
"""
if not user.id:
raise MissingUserAttributeError("User ID is required for fetching inspections.")
if not id:
raise ValueError("Inspection ID is required for fetching inspection details.")
if not isinstance(id, UUID):
id = UUID(id)

with cp.connection() as conn, conn.cursor() as cursor:
try:
inspection = await get_full_inspection_json(cursor, id, user.id)
return await get_picture_set_pictures(cursor, inspection.picture_set_id) # TODO: This function will be deprecated
except DBInspectionNotFoundError as e:
log_error(e)
raise InspectionNotFoundError(f"{e}") from e

async def read_inspection(cp: ConnectionPool, user: User, id: UUID | str):
"""
Expand Down
15 changes: 15 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
read_all_inspections,
read_inspection,
update_inspection,
get_pictures,
)
from app.controllers.users import sign_up
from app.dependencies import (
Expand Down Expand Up @@ -106,6 +107,20 @@ async def get_inspection(
status_code=HTTPStatus.NOT_FOUND, detail="Inspection not found"
)

@app.get("/inspections/{id}/pictures", tags=["Inspections"])
async def get_pictures(

Check failure on line 111 in app/main.py

View workflow job for this annotation

GitHub Actions / lint-test / lint-test

Ruff (F811)

app/main.py:111:11: F811 Redefinition of unused `get_pictures` from line 18
cp: Annotated[ConnectionPool, Depends(get_connection_pool)],
user: Annotated[User, Depends(fetch_user)],
id: UUID,
):
try:
return await get_pictures(cp, user, id)
except InspectionNotFoundError:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Inspection not found"
)



@app.post("/inspections", tags=["Inspections"], response_model=Inspection)
async def post_inspection(
Expand Down

0 comments on commit 5d8d334

Please sign in to comment.