Skip to content

Commit

Permalink
implement filter functionality TODO: check if exceptions are required…
Browse files Browse the repository at this point in the history
… and write tests #38
  • Loading branch information
asuresh-code committed Oct 25, 2024
1 parent 8b3054c commit dd87490
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
17 changes: 15 additions & 2 deletions object_storage_api/repositories/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,26 @@ def get(self, image_id: str, session: ClientSession = None) -> Optional[ImageOut
return ImageOut(**image)
return None

def list(self, session: ClientSession = None) -> list[ImageOut]:
def list(self, entity_id: Optional[str], primary: Optional[bool], session: ClientSession = None) -> list[ImageOut]:
"""
Retrieve Images from a MongoDB database.
:param session: PyMongo ClientSession to use for database operations.
:return: List of Images or an empty list if no Images are retrieved
"""

images = self._images_collection.find(session=session)
query = {}
if entity_id is not None:
query["entity_id":entity_id]
if primary is not None:
query["primary":primary]

message = "Retrieving all images from the database"
if not query:
logger.info(message)
else:
logger.info("%s matching the provided filter(s)", message)
logger.debug("Provided filter(s): %s", query)

images = self._images_collection.find(query, session=session)
return [ImageOut(**image) for image in images]
20 changes: 17 additions & 3 deletions object_storage_api/routers/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,25 @@ def create_image(
return image_service.create(image_metadata, upload_file)


@router.get(path="", summary="retrieve a list of images", status_code=status.HTTP_200_OK)
@router.get(
path="",
summary="retrieve a list of images",
response_description="a list of images",
status_code=status.HTTP_200_OK,
)
def get_image_list(
image_service: ImageServiceDep,
entity_id: Annotated[str | None, Query()] = None,
primary: Annotated[bool | None, Query()] = None,
) -> list[ImageSchema]:
images = image_service.list()
return (ImageSchema(**image) for image in images)
# pylint: disable=missing-function-docstring
logger.info("Getting images")

if entity_id:
logger.debug("Entity ID filter: '%s'", entity_id)

if primary:
logger.debug("Primary filter: '%s'", primary)
images = image_service.list(entity_id, primary)

return (ImageSchema(**image.model_dump()) for image in images)
6 changes: 3 additions & 3 deletions object_storage_api/services/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import logging
from typing import Annotated
from typing import Annotated, Optional

from bson import ObjectId
from fastapi import Depends, UploadFile
Expand Down Expand Up @@ -75,11 +75,11 @@ def create(self, image_metadata: ImagePostMetadataSchema, upload_file: UploadFil

return ImageSchema(**image_out.model_dump())

def list(self) -> list[ImageOut]:
def list(self, entity_id: Optional[str], primary: Optional[bool]) -> list[ImageOut]:
"""
Retrieve a list of all Images
:return: List of Images or an empty list if no Images are retrieved
"""

return self._image_repository.list()
return self._image_repository.list(entity_id, primary)

0 comments on commit dd87490

Please sign in to comment.