Skip to content

Commit

Permalink
Implement update api layers TODO: error handling, docstrings, tests #35
Browse files Browse the repository at this point in the history
  • Loading branch information
asuresh-code committed Dec 3, 2024
1 parent df779f0 commit a3457c0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
19 changes: 19 additions & 0 deletions object_storage_api/repositories/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,22 @@ def list(self, entity_id: Optional[str], primary: Optional[bool], session: Clien

images = self._images_collection.find(query, session=session)
return [ImageOut(**image) for image in images]

def update(self, image_id: str, image: ImageIn, session: ClientSession = None) -> ImageOut:
"""
Updates an image from a MongoDB database.
:param image_id: The ID of the image to update.
:param image: The new image metadata.
:param session: PyMongo ClientSession to use for database operations.
:return: List of images or an empty list if no images are retrieved.
"""

logger.info("Updating image metadata with ID: %s", image_id)
image_id = CustomObjectId(image_id)

self._images_collection.update_one(
{"_id": image_id}, {"$set": image.model_dump(by_alias=True)}, session=session
)
image = self.get(image_id=str(image_id), session=session)
return image
13 changes: 12 additions & 1 deletion object_storage_api/routers/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from fastapi import APIRouter, Depends, File, Form, Query, UploadFile, status

from object_storage_api.schemas.image import ImagePostMetadataSchema, ImageSchema
from object_storage_api.schemas.image import ImagePatchMetadataSchema, ImagePostMetadataSchema, ImageSchema
from object_storage_api.services.image import ImageService

logger = logging.getLogger()
Expand Down Expand Up @@ -67,3 +67,14 @@ def get_images(
logger.debug("Primary filter: '%s'", primary)

return image_service.list(entity_id, primary)


@router.patch(
path="/{image_id}",
summary="Update image",
response_description="Updated Image",
)
def partial_update_image(image_service: ImageServiceDep, image_id: str, image: ImagePatchMetadataSchema) -> ImageSchema:
logger.info("Updating images")

return image_service.update(image_id, image)
11 changes: 9 additions & 2 deletions object_storage_api/schemas/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@
from object_storage_api.schemas.mixins import CreatedModifiedSchemaMixin


class ImagePostMetadataSchema(BaseModel):
class ImagePatchMetadataSchema(BaseModel):
"""
Base schema model for an image.
"""

entity_id: str = Field(description="ID of the entity the image relates to")
title: Optional[str] = Field(default=None, description="Title of the image")
description: Optional[str] = Field(default=None, description="Description of the image")


class ImagePostMetadataSchema(ImagePatchMetadataSchema):
"""
Base schema model for an image.
"""

entity_id: str = Field(description="ID of the entity the image relates to")


class ImageSchema(CreatedModifiedSchemaMixin, ImagePostMetadataSchema):
"""
Schema model for an image get request response.
Expand Down
20 changes: 19 additions & 1 deletion object_storage_api/services/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from object_storage_api.core.image import generate_thumbnail_base64_str
from object_storage_api.models.image import ImageIn
from object_storage_api.repositories.image import ImageRepo
from object_storage_api.schemas.image import ImagePostMetadataSchema, ImageSchema
from object_storage_api.schemas.image import ImagePatchMetadataSchema, ImagePostMetadataSchema, ImageSchema
from object_storage_api.stores.image import ImageStore

logger = logging.getLogger()
Expand Down Expand Up @@ -85,3 +85,21 @@ def list(self, entity_id: Optional[str] = None, primary: Optional[bool] = None)
"""
images = self._image_repository.list(entity_id, primary)
return [ImageSchema(**image.model_dump()) for image in images]

def update(self, image_id: str, image: ImagePatchMetadataSchema) -> ImageSchema:
"""
Update an image based on its ID.
:param image_id: The ID of the image to updtae.
:param image: The new update data for the image.
:return: List of images or an empty list if no images are retrieved.
"""
update_data = image.model_dump(exclude_unset=True)

stored_image = self._image_repository.get(image_id=image_id)

updated_image = self._image_repository.update(
image_id=image_id, image=ImageIn(**{**stored_image.model_dump(), **update_data})
)

return ImageSchema(**updated_image.model_dump())

0 comments on commit a3457c0

Please sign in to comment.