Skip to content

Commit 922a90a

Browse files
committed
simplify PUT endpoint callback
1 parent b8a7927 commit 922a90a

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

routes.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def list_books(request: Request):
2222
books = list(request.app.database["books"].find(limit=100))
2323
return books
2424

25+
2526
@router.get("/{id}", response_description="Get a single book by id", response_model=Book)
2627
def find_book(id: str, request: Request):
2728
if (book := request.app.database["books"].find_one({"_id": id})) is not None:
@@ -39,11 +40,8 @@ def update_book(id: str, request: Request, book: BookUpdate = Body(...)):
3940
{"_id": id}, {"$set": book}
4041
)
4142

42-
if update_result.modified_count == 1:
43-
if (
44-
updated_book := request.app.database["books"].find_one({"_id": id})
45-
) is not None:
46-
return updated_book
43+
if update_result.modified_count == 0:
44+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Book with ID {id} not found")
4745

4846
if (
4947
existing_book := request.app.database["books"].find_one({"_id": id})

0 commit comments

Comments
 (0)