Skip to content

Commit

Permalink
Refactored 'endpoints' module: added support of MongoDB to 'show_book…
Browse files Browse the repository at this point in the history
…s' and 'delete_book' endpoints
  • Loading branch information
iduseev committed Sep 15, 2023
1 parent 6647190 commit 7c47b17
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions backend/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,14 @@ def show_books(
"Detected incoming GET request to /books endpoint from the"
"client with IP %s ...", client_host
)
return list(default_book_shelf.values())[: limit]
# find and retrieve each book in the collection
cursor = ma_books_collection.find()
# convert the cursor to the list with Book models, considering the limit
all_books = []
for book_in_db in cursor:
book = Book(*book_in_db)
all_books.append(book)
return all_books[: limit]


@app.post(
Expand Down Expand Up @@ -412,7 +419,7 @@ def add_book(

except Exception as e:
TB = traceback.format_exc()
logger.exception(f"Exception occurred: {e}\ntraceback: {TB}")
logger.exception("Exception occurred: %s\ntraceback: %x", e, TB)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=Error(
Expand All @@ -435,9 +442,9 @@ def add_book(
dependencies=[Depends(oauth2_scheme)]
)
def delete_book(
request: Request,
request: Request,
book_name: str = Path(..., title="Required book name to be deleted", example="Shantaram")
) -> Union[Message, NoReturn]:
) -> Union[Message, JSONResponse]:
"""
Deletes a book from the book shelf by given book name
Expand All @@ -447,29 +454,45 @@ def delete_book(
:type token: Annotated[str, Depends(oauth2_scheme)]
:param book_name: Path parameter, book name gotten from the route
:type book_name: str, optional
:raises HTTPException: exception with status_code HTTP_404_NOT_FOUND raised in case the given book name does not exist on the book shelf
:raises HTTPException: exception with status_code HTTP_404_NOT_FOUND
raised in case the given book name does not exist on the book shelf
:return: message about successful book deletion from the book shelf
:rtype: Union[NoReturn, Message]
:rtype: Union[JSONResponse, Message]
"""
client_host = request.client.host
logger.debug(
"Detected incoming DELETE request to /books/delete endpoint"
"from the client with IP %s ...", client_host
)
# todo add authorization via JWT token
# todo add working with MongoDB
if book_name not in [book.book_name for book in default_book_shelf.values()]:
raise HTTPException(
# todo add authorization via JWT token

logger.debug(
"Book with book_name %s is requested to be deleted from the DB",
book_name
)
# delete returnable key-value pair deletion after extraction from DB
deletion_result = ma_books_collection.delete_db_entry(
index_name="book_name",
entry_id=book_name
)
# deletable_entry can be either valid data entry by the key "issue_id"
# from the DB or None if entry is not found
if deletion_result.deleted_count == 0:
logger.warning(
"The book %s was not found in the book shelf!",
book_name
)
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"The book {book_name} was not found in the book shelf!"
content=Error(
message=f"No data entry found with book_name {book_name}!",
status_code=status.HTTP_404_NOT_FOUND
).dict()
)
for book_id, book in default_book_shelf.items():
if book_name == book.book_name:
logger.debug(
"Book %s with assigned book ID %x is found on the book"
"shelf and is to be deleted ...", book_name, book_id
)
del default_book_shelf[book_id]
return Message(
message=f"Book {book_name} was successfully deleted from the book shelf!"
)
logger.info(
"Book %s is found on the book shelf and is to be deleted ...",
book_name
)
return Message(
message=f"Book {book_name} was successfully deleted from the book shelf!"
)

0 comments on commit 7c47b17

Please sign in to comment.