-
Notifications
You must be signed in to change notification settings - Fork 55
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
Alf & Reid - Spruce #56
base: master
Are you sure you want to change the base?
Conversation
…y local computer, to overcome bug in test_update_customer
…ctionary rather than a nested dicitonary, now passing final wave 1 customer test
… guard clause rather than a try except to determine whether customer_id is a valid type
…he rental model to remove the rental id
…, video, rental) files
…eleted old comments
…items (a pagination attribute) to the for loop
…te resolves the error message we were getting earlier
…t that we can eventually use in our docstrings
…ate a specific response when ithere is missing data in the request body
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.
Great work Alf and Reid! Y'all met all the learning goals for this project and passed all the tests.
I provided feedback on how y'all can refactor by omitting redundant logic and list comprehension.
Project grade: Green 🟢 !
if "postal_code" not in request_body.keys(): | ||
response = { | ||
"details": "Request body must include postal_code." | ||
} | ||
elif "name" not in request_body.keys(): | ||
response = { | ||
"details": "Request body must include name." | ||
} | ||
elif "phone" not in request_body.keys(): | ||
response = { | ||
"details": "Request body must include phone." | ||
} |
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.
keys are the default iterator for dictionaries so we can actually omit the keys()
method and this conditional will work the same way.
if "postal_code" not in request_body.keys(): | |
response = { | |
"details": "Request body must include postal_code." | |
} | |
elif "name" not in request_body.keys(): | |
response = { | |
"details": "Request body must include name." | |
} | |
elif "phone" not in request_body.keys(): | |
response = { | |
"details": "Request body must include phone." | |
} | |
if "postal_code" not in request_body: | |
response = { | |
"details": "Request body must include postal_code." | |
} | |
elif "name" not in request_body: | |
response = { | |
"details": "Request body must include name." | |
} | |
elif "phone" not in request_body: | |
response = { | |
"details": "Request body must include phone." | |
} |
if customer_query == "name": | ||
customers = Customer.query.order_by(Customer.name.asc()).paginate(page=page, per_page=num_per_page, error_out=False) | ||
#when error out is false, page and per_page default to 1 and 20 respectively | ||
#so we can paginate, and if no p and n parameters are passed in that's no problem because the defaults are passed in | ||
else: | ||
customers = Customer.query.order_by(Customer.customer_id.asc()).paginate(page=page, per_page=num_per_page, error_out=False) | ||
|
||
response = [customer.to_dict() for customer in customers.items] | ||
#because no matter what 'customers' is paginated (either with the parameters passed in or the default parameters) | ||
#when we loop we can use pagination objects .items attribute to loop over the individual items themselves | ||
#we use .items with no () because 'items' is an attribute of the pagination CLASS, not a method or a function | ||
|
||
return jsonify(response), 200 |
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.
Great use of pagination!
customers_bp = Blueprint("customers", __name__, url_prefix="/customers") | ||
|
||
#HELPER FUNCTION | ||
def find_specific_key_error(request_body): |
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.
Great helper function!
return jsonify(response), 400 | ||
|
||
|
||
@customers_bp.route("/<customer_id>", methods=["DELETE"]) |
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.
👍
return jsonify(response), 200 | ||
|
||
|
||
@customers_bp.route("/<customer_id>/rentals", methods=["GET"]) |
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.
👍
if "title" not in request_body.keys(): | ||
response = { | ||
"details" : "Request body must include title." | ||
} | ||
elif "release_date" not in request_body.keys(): | ||
response = { | ||
"details" : "Request body must include release_date." | ||
} | ||
elif "total_inventory" not in request_body.keys(): | ||
response = { | ||
"details" : "Request body must include total_inventory." | ||
} |
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.
keys are the default iterator for dictionaries so we can actually omit the keys() method and this conditional will work the same way.
|
||
return jsonify(response), 400 | ||
|
||
@videos_bp.route("/<video_id>", methods=["GET"]) |
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.
👍
if not video: | ||
return jsonify({"message" : f"Video {video_id} was not found"}), 404 | ||
|
||
else: | ||
db.session.delete(video) | ||
db.session.commit() | ||
return jsonify( | ||
{ | ||
"id": video.video_id, #this worked but video_id didnt?? | ||
'details': (f'Video {video.video_id} successfully deleted') | ||
} | ||
|
||
), 200 |
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.
The else
clause isn't necessary here, especially if we're following the guard clause pattern. We can think of if
statements as if and only if this expression is true then execute this code, otherwise move on to the rest of the function body
.
if not video: | |
return jsonify({"message" : f"Video {video_id} was not found"}), 404 | |
else: | |
db.session.delete(video) | |
db.session.commit() | |
return jsonify( | |
{ | |
"id": video.video_id, #this worked but video_id didnt?? | |
'details': (f'Video {video.video_id} successfully deleted') | |
} | |
), 200 | |
if not video: | |
return jsonify({"message" : f"Video {video_id} was not found"}), 404 | |
db.session.delete(video) | |
db.session.commit() | |
return jsonify( | |
{ | |
"id": video.video_id, #this worked but video_id didnt?? | |
'details': (f'Video {video.video_id} successfully deleted') | |
} | |
), 200 |
else: | ||
|
||
video.title = request_body["title"] | ||
video.release_date = request_body["release_date"] | ||
video.total_inventory = request_body["total_inventory"] | ||
|
||
db.session.commit() | ||
|
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.
See comment about else
clauses above.
except KeyError: | ||
return jsonify(None), 400 | ||
|
||
@videos_bp.route("/<video_id>/rentals", methods=["GET"]) |
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.
👍
No description provided.