Conversation
…T functionality to driver code, and added one to many rel to goal and task model/Wave 4,5,(a bit of 6)
spitsfire
left a comment
There was a problem hiding this comment.
Great job Abby! One thing to consider is making all your return statements consistent. Everything could return jsonify() or return a dictionary or return make_response. Either options work, but it's good to pick just one. That way your endpoints will have consistency when making tests or being read by others!
very small thing, though!
| def to_json_goal(self): | ||
| # This method was created so that we do not have to write out the dictionary many times in the routes.py file. | ||
| return { | ||
| "id": self.goal_id, | ||
| "title": self.title, | ||
| } |
| __tablename__ = 'task' | ||
| task_id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
| title = db.Column(db.String) | ||
| description = db.Column(db.String) | ||
| completed_at = db.Column(db.DateTime, nullable=True) | ||
| goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id'), nullable = True) |
| goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id'), nullable = True) | ||
|
|
||
|
|
||
| def to_json(self): |
There was a problem hiding this comment.
👍 glad you put all your logic in the helper method instead of in the route!
| if self.goal_id == None: | ||
| return { | ||
| "id": self.task_id, | ||
| "title": self.title, | ||
| "description": self.description, | ||
| "is_complete": result | ||
| } | ||
| else: | ||
| return { | ||
| "id": self.task_id, | ||
| "title": self.title, | ||
| "description": self.description, | ||
| "is_complete": result, | ||
| "goal_id": self.goal_id | ||
|
|
||
| } No newline at end of file |
There was a problem hiding this comment.
one thing we could do to shorten this is create the data structure first, then change out the key-value pair you need to.
| if self.goal_id == None: | |
| return { | |
| "id": self.task_id, | |
| "title": self.title, | |
| "description": self.description, | |
| "is_complete": result | |
| } | |
| else: | |
| return { | |
| "id": self.task_id, | |
| "title": self.title, | |
| "description": self.description, | |
| "is_complete": result, | |
| "goal_id": self.goal_id | |
| } | |
| task = { | |
| "id": self.task_id, | |
| "title": self.title, | |
| "description": self.description, | |
| "is_complete": result | |
| } | |
| if self.goal_id: | |
| task["goal_id"] = self.goal_id | |
| return task |
| task = Task.query.get(task_id) | ||
| # With the GET, POST and DELETE request if there is nothing we output this | ||
| if request == None or task == None: | ||
| return jsonify(None), 404 |
There was a problem hiding this comment.
👍 this works great! One thing we could do to shorten this is:
| task = Task.query.get(task_id) | |
| # With the GET, POST and DELETE request if there is nothing we output this | |
| if request == None or task == None: | |
| return jsonify(None), 404 | |
| task = Task.query.get_or_404(task_id) |
this will do your 404 check for you!
| from app.models.goal import Goal | ||
| from flask import Blueprint, request, jsonify | ||
|
|
||
| goals_bp = Blueprint("goals", __name__, url_prefix="/goals") |
There was a problem hiding this comment.
we could add Goal import to the top. also, we've already imported Blueprint, request, and jsonify, so we don't need to do it again.
| try: | ||
| # This portion is the POST request | ||
| request_body = request.get_json() | ||
| new_goal = Goal(title=request_body["title"]) | ||
|
|
||
| db.session.add(new_goal) | ||
| db.session.commit() | ||
| return {"goal": new_goal.to_json_goal()}, 201 | ||
| except KeyError: | ||
| return { | ||
| "details": "Invalid data"}, 400 |
There was a problem hiding this comment.
cool! good job experimenting with a try-except statement
| goals = Goal.query.all() | ||
| if goals == None: | ||
| return [] |
There was a problem hiding this comment.
we can let all() return an empty list for us
| goal = Goal.query.get(goal_id) | ||
| # With the GET, POST and DELETE request if there is nothing we output this | ||
| if request == None or goal == None: | ||
| return jsonify(None), 404 |
There was a problem hiding this comment.
we could also use the get_or_404() method here, too!
| goal = Goal.query.get(goal_id) | ||
| if goal == None: | ||
| return jsonify(None), 404 |
There was a problem hiding this comment.
we could also use the get_or_404() method here, too!
No description provided.