Skip to content

Commit

Permalink
create delete endpoint TODO: add docstring comments and testing #38
Browse files Browse the repository at this point in the history
  • Loading branch information
asuresh-code committed Nov 27, 2024
1 parent 005e416 commit 23626f8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
9 changes: 9 additions & 0 deletions object_storage_api/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ class InvalidImageFileError(BaseAPIException):

status_code = 422
response_detail = "File given is not a valid image"


class MissingRecordError(DatabaseError):
"""
A specific database record was requested but could not be found.
"""

status_code = 422
response_detail = "Requested Record was not found"
15 changes: 15 additions & 0 deletions object_storage_api/repositories/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from object_storage_api.core.custom_object_id import CustomObjectId
from object_storage_api.core.database import DatabaseDep
from object_storage_api.core.exceptions import InvalidObjectIdError, MissingRecordError
from object_storage_api.models.image import ImageIn, ImageOut

logger = logging.getLogger()
Expand Down Expand Up @@ -82,3 +83,17 @@ 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 delete(self, image_id: str, session: ClientSession = None) -> str:
try:
new_image_id = CustomObjectId(image_id)
except InvalidObjectIdError as exc:
exc.response_detail = f"Invalid image_id given: {image_id}"
raise exc
response = self._images_collection.find_one_and_delete(
filter={"_id": new_image_id}, projection={"object_key": True}, session=session
)
if response is None:
exc = MissingRecordError(f"Requested Image was not found: Image ID {image_id}")
raise exc
return response["object_key"]
12 changes: 11 additions & 1 deletion object_storage_api/routers/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from typing import Annotated, Optional

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

from object_storage_api.schemas.image import ImagePostMetadataSchema, ImageSchema
from object_storage_api.services.image import ImageService
Expand Down Expand Up @@ -67,3 +67,13 @@ def get_images(
logger.debug("Primary filter: '%s'", primary)

return image_service.list(entity_id, primary)


@router.delete(
path="/{image_id}", summary="Delete an image by its ID", response_description="Successfully deleted image"
)
def delete_image(
image_service: ImageServiceDep,
image_id: Annotated[str, Path(description="The ID of the image that is to be deleted")],
) -> None:
image_service.delete(image_id)
4 changes: 4 additions & 0 deletions object_storage_api/services/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ 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 delete(self, image_id: str) -> None:
object_key = self._image_repository.delete(image_id)
self._image_store.delete(object_key)
6 changes: 6 additions & 0 deletions object_storage_api/stores/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ def upload(self, image_id: str, image_metadata: ImagePostMetadataSchema, upload_
)

return object_key

def delete(self, object_key: str) -> None:
s3_client.delete_object(
Bucket=object_storage_config.bucket_name.get_secret_value(),
Key=object_key,
)

0 comments on commit 23626f8

Please sign in to comment.