-
Notifications
You must be signed in to change notification settings - Fork 71
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
Scissors - Araceli #68
base: master
Are you sure you want to change the base?
Conversation
def to_json(self): | ||
return { | ||
"id": self.goal_id, | ||
"title": self.title, | ||
} |
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 is a great helper function, but I suspect it wasn't working as you expected because the tab level is outside of the class.
def to_dict(self): | ||
return { | ||
"id": self.task_id, | ||
"title": self.title, | ||
"description": self.description, | ||
"is_complete": bool(self.completed_at) | ||
} | ||
|
||
def to_dict_goal(self): | ||
return { | ||
"id": self.task_id, | ||
"goal_id": self.goal_id, | ||
"title": self.title, | ||
"description": self.description, | ||
"is_complete": bool(self.completed_at) | ||
} |
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.
Same issue as Goal, but these are great helper functions!
|
||
return jsonify(response), 400 | ||
|
||
else: |
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.
It's not necessary to include this else
statement. If the if
statement passes, the function will return and the code after this point will not run.
if sort_query == "asc": | ||
tasks = Task.query.order_by(Task.title.asc()) | ||
|
||
elif sort_query == "desc": | ||
tasks = Task.query.order_by(Task.title.desc()) | ||
|
||
else: | ||
tasks = Task.query.all() |
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.
Nice!
return jsonify(tasks_response), 200 | ||
|
||
|
||
@tasks_bp.route("/<task_id>", methods=["GET", "PUT", "DELETE"], strict_slashes=False) |
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.
In the "" route above (lines 17 & 37), the methods are split into separate functions, while here they're combined into the same method. Both ways are fine, but for readability, I recommend following the same patterns throughout your code.
|
||
if request.method == "GET": | ||
if task is None: | ||
return make_response(f"404 Not Found", 404) |
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.
Having an error message is great, but I would suggest having a more meaningful error message, something like 'Task with ID X not found'. Also this test is something you are doing for all of the methods - it could be moved up before the checks for the type of method so it only needs to be done once.
else: | ||
one_task = to_dict(task) | ||
|
||
return {"task": one_task} |
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.
For readability I recommend using the same format to create your responses throughout your project. Lines 56, 66 and 71 all use a different format. Flask handles all of these formats gracefully, but having multiple formats can make it seem that different things are happening when in fact they are all more or less the same. Also, I recommend explicitly setting a status code everywhere so that you can accurately predict what your API will do in each situation.
updated_task = { | ||
"id": task.task_id, | ||
"title": task.title, | ||
"description": task.description, | ||
"is_complete": bool(task.completed_at) | ||
} |
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 would be a great place to use use the to_dict helper function.
return make_response(f"404 Not Found", 404) | ||
|
||
else: | ||
one_task = to_dict(task) |
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.
Here is where the last test fails - if the task has a valid goal_id, it should include the goal_id in the json output. One way to handle this is:
if task.goal_id:
one_task = to_dict_goal(task)
else:
one_task = to_dict(task)
# for task in tasks: | ||
# tasks_list.append(to_dict_goal(task)) | ||
|
||
task_list = [to_dict_goal(task) for task in tasks] |
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.
Nice list comprehension!
Great work on this project, you hit all the learning goals. Nice work with your to_json & to_dict helper functions! |
No description provided.