-
Notifications
You must be signed in to change notification settings - Fork 1
Complete mongo db integration for get /books/{id}) #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
755060f
3c3665a
bd7e986
9740a99
a41e3e9
1bbf7e9
3412d29
d388151
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||||||||
|
|
||||||||||||
| import copy | ||||||||||||
|
|
||||||||||||
| from bson.objectid import ObjectId | ||||||||||||
| from flask import jsonify, request | ||||||||||||
| from pymongo.errors import ConnectionFailure | ||||||||||||
| from werkzeug.exceptions import HTTPException, NotFound | ||||||||||||
|
|
@@ -88,7 +89,6 @@ def add_book(): | |||||||||||
| host = request.host_url | ||||||||||||
| # Send the host and new book_id to the helper function to generate links | ||||||||||||
| final_book_for_api = append_hostname(book_from_db, host) | ||||||||||||
| print("final_book_for_api", final_book_for_api) | ||||||||||||
|
|
||||||||||||
| # Transform _id to id and remove the internal _id | ||||||||||||
| final_book_for_api["id"] = str(final_book_for_api["_id"]) | ||||||||||||
|
|
@@ -141,20 +141,36 @@ def get_book(book_id): | |||||||||||
| """ | ||||||||||||
| Retrieve a specific book by its unique ID. | ||||||||||||
| """ | ||||||||||||
| if not books: | ||||||||||||
| return jsonify({"error": "Book collection not initialized"}), 500 | ||||||||||||
| # get the collection | ||||||||||||
| collection = get_book_collection() | ||||||||||||
|
|
||||||||||||
| # extract host from the request | ||||||||||||
| if collection is None: | ||||||||||||
| return jsonify({"error": "Book collection not found"}), 500 | ||||||||||||
|
|
||||||||||||
| # sanity check book_id | ||||||||||||
| if not ObjectId.is_valid(book_id): | ||||||||||||
| return jsonify({"error": "Invalid book ID format"}), 400 | ||||||||||||
| obj_id = ObjectId(book_id) | ||||||||||||
|
|
||||||||||||
| # Query db for a non-deleted book | ||||||||||||
| query = {"_id": obj_id, "state": {"$ne": "deleted"}} | ||||||||||||
| # look it up in MongoDB | ||||||||||||
| book = collection.find_one(query) | ||||||||||||
| # also equivalent to Key version | ||||||||||||
| # book = raw_books.find_one(_id=obj_id, state={"$ne": "deleted"}) | ||||||||||||
|
Comment on lines
+159
to
+160
|
||||||||||||
| # also equivalent to Key version | |
| # book = raw_books.find_one(_id=obj_id, state={"$ne": "deleted"}) | |
| # Alternative query to fetch a non-deleted book by its ID | |
| # book = collection.find_one({"_id": obj_id, "state": {"$ne": "deleted"}}) |
Copilot
AI
Jul 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commented-out code should be removed as it serves no purpose and adds confusion. The variable name 'raw_books' doesn't match the actual collection variable name.
| # book = raw_books.find_one(_id=obj_id, state={"$ne": "deleted"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding exception handling around the MongoDB query. Database operations can raise exceptions (ConnectionFailure, ServerSelectionTimeoutError) that should be caught and converted to appropriate HTTP error responses.