-
Notifications
You must be signed in to change notification settings - Fork 0
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
Draft: Implement new API routes to fetch blog posts by authorIds
and to update a post
#2
base: main
Are you sure you want to change the base?
Conversation
… the authors specified in the authorIds parameter and returns these posts in the JSON response
Correct jsonify() argument and return 200 success code Co-authored-by: Jan B <janhenrikbern@users.noreply.github.com>
…y() arguments and 200 success code
…y() arguments and 200 success code
… information to match specification for JSON Response Body.
…lp subsequent data manipulation. Implement sorting of blog posts.
…hen value for query parameter is in incorrect format. Create global variable for options on which we can sort posts.
…h introducing reverse_boolean variable closer to where we use it. Separate the error checking and the assigning of reverse_boolean.
…when request asks for posts of author id(s) that are not in the user table in the database. Handle case when the GET request is made correctly and there are no posts to return.
I believe /tests/test_posts.py has one test, I had not used type hints before you mentioned them, @janhenrikbern . Helped me to update variable names where I was previously indicating the type in the name to help myself remember. Here are several areas in which I have doubts: Efficiency"Sort the blog posts based on the provided query parameters" and "remove any duplicate blog posts (try to be efficient when doing this)" from the issue
Error handlingThanks @hdenisenko for emphasizing error checking in the past. I restructured my code significantly after thinking through errors, which I was not expecting! In a few cases where different errors could look the same to the user, I wanted to separate where possible to provide clarity.
Organization
I don't know how code reviews would work on a real team so you tell me and look at what interests you? |
api/posts.py
Outdated
author_id = int(author_id_input) | ||
if crud.check_user_exists(author_id): | ||
author_ids.add(author_id) | ||
except: |
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.
i'm not sure what in your try would hit the except - does crud.check_user_exists
return an error?
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, crud.check_user_exists
does not return an error. if the user does not exist, the db query in crud
would return None
per my understanding and crud.check_user_exists
would return False
. thanks for the question! i had to remind myself what error i was checking.
i want to hit the except when the request has an invalid authorIds
query parameter value, for instance when a character other than "," is used to separate ids or when ids are not typed as numbers (see screenshot). i don't know whether testing for an error in author_ids.split(",")
is a great translation of that intent.
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.
an update: crud.check_user_exists
can return an error if int(author_id)
returns an error, e.g., in the case of the bottom request in the screenshot. whether author_id
can be converted to an integer is also part of checking for intended behavior
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.
i suppose we could be more specific to the requester whether the error was that the query parameter value had no integers at all (e.g., "all"), or had integers but incorrectly separated (e.g., "1+2"), or so on
…or route to return according to specification.
…goes with which status code. Make structure of code that handles error checking more consistent across routes.
authorIds
authorIds
and to update a post
sort_by=sort_by, | ||
direction=direction) | ||
|
||
return jsonify({"posts": result}), 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.
I'd recommend returning a flask response object here instead of 2 parameters: https://tedboy.github.io/flask/generated/generated/flask.Response.html
… and return results of those queries
…pers_to_fetch_posts.py is part of the service layer, calls that function, and applies business logic. In db/models/post.py, make code less repetitive and more readable by pulling out direction == desc into its own variable.
…ory layer that contains functions that interact with the database. Improve factoring of code into functions for extensibility, maintability, and readability. Remove print statements from debugging. Make code more concise. Decrease repetitive code. Write comments where helpful. Pass tests.
…op.github.io/sqlite-viewer/ for parsing sqlite files and viewing them online.
#1