Skip to content

Conversation

@naomiquinones
Copy link

No description provided.

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)
Copy link

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)

Copy link

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,
Copy link

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

Comment on lines +52 to +56
"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)
Copy link

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.

Comment on lines +62 to +95
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'})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃🏽

Comment on lines +99 to +112
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)
Copy link

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

Comment on lines +145 to +169
@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)
Copy link

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'})
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants