-
Notifications
You must be signed in to change notification settings - Fork 15
C15-Accelerate Naomi Quinones #15
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
base: master
Are you sure you want to change the base?
Conversation
@@ -3,4 +3,5 @@ | |||
|
|||
|
|||
class Goal(db.Model): | |||
goal_id = db.Column(db.Integer, primary_key=True) | |||
goal_id = db.Column(db.Integer, primary_key=True,autoincrement=True) | |||
title = db.Column(db.Text) |
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.
add tasks and use db.relationship to connect this model to the task model
title = db.Column(db.String) | ||
description = db.Column(db.String) | ||
completed_at = db.Column(db.DateTime, nullable=True) | ||
|
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.
add goal_id with a foreign key goal.goal_id
to finish the connection between the models
"id": task.task_id, | ||
"title": task.title, | ||
"description": task.description, | ||
"is_complete": False if not task.completed_at else True, |
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 way to add the check
"task": { | ||
"id": new_task.task_id, | ||
"title": new_task.title, | ||
"description": new_task.description, | ||
"is_complete": False if not new_task.completed_at else True}}, 201) |
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.
just like we did in the review session, think about how you could create a helper function to call here instead of writing this every time.
def handle_task(task_id): | ||
task = Task.query.get(task_id) | ||
if task is None: | ||
return make_response("Task not found", 404) | ||
|
||
if request.method == "GET": | ||
return {"task": { | ||
"id": task.task_id, | ||
"title": task.title, | ||
"description": task.description, | ||
"is_complete": False if not task.completed_at else True | ||
}} | ||
|
||
elif request.method == "PUT": | ||
form_data = request.get_json() | ||
# check for missing items | ||
if "title" not in form_data or "description" not in form_data or "completed_at" not in form_data: | ||
return make_response({"details": "Invalid data"}, 400) | ||
|
||
task.title = form_data["title"] | ||
task.description = form_data["description"] | ||
task.completed_at = form_data["completed_at"] | ||
db.session.commit() | ||
return make_response({ | ||
"task": { | ||
"id": task.task_id, | ||
"title": task.title, | ||
"description": task.description, | ||
"is_complete": False if not task.completed_at else True}}, 200) | ||
|
||
elif request.method == "DELETE": | ||
db.session.delete(task) | ||
db.session.commit() | ||
return make_response({"details": f'Task {task.task_id} "{task.title}" successfully 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.
💃🏽
def mark_task_incomplete(task_id): | ||
if request.method == "PATCH": | ||
task = Task.query.get(task_id) | ||
if task is None: | ||
return make_response("", 404) | ||
|
||
task.completed_at = None | ||
db.session.commit() | ||
return make_response({ | ||
"task": { | ||
"id": task.task_id, | ||
"title": task.title, | ||
"description": task.description, | ||
"is_complete": False if not task.completed_at else True}}, 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 job here
- you could do the 404 check with .get_or_404
- again you could do a helper function for the response so you don't have to retype it. You could also have the is_complete check in the helper function
@goals_bp.route("", methods=["GET", "POST"]) | ||
def handle_goals(): | ||
if request.method == "GET": | ||
goals = Goal.query.all() | ||
goals_response = [] | ||
for goal in goals: | ||
goals_response.append({ | ||
"id": goal.goal_id, | ||
"title": goal.title, | ||
}) | ||
return jsonify(goals_response) | ||
elif request.method == "POST": | ||
req_body = request.get_json() | ||
|
||
# check for missing data | ||
if "title" not in req_body: | ||
return make_response({"details": "Invalid data"}, 400) | ||
new_goal = Goal(title=req_body["title"]) | ||
db.session.add(new_goal) | ||
db.session.commit() | ||
|
||
return make_response({"goal": { | ||
"id": new_goal.goal_id, | ||
"title": new_goal.title | ||
}}, 201) |
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.
💃🏽
db.session.delete(goal) | ||
db.session.commit() | ||
|
||
return make_response({"details": f'Goal {goal.goal_id} "{goal.title}" successfully 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.
Go ahead and follow what you've been doing add the route here @goal_bp.route("/<goal_id>/tasks", methods=["GET", "POST"])
you want to get each Task to add it to the goal using the tasks field you created in the model.
No description provided.