Heather M Task List API#92
Conversation
tgoslee
left a comment
There was a problem hiding this comment.
Great job Heather!! I left some comments on some possible ways you could refactor! Let me know if you have questions!
| migrate.init_app(app, db) | ||
|
|
||
| # Register Blueprints here | ||
| from .routes import tasks_bp, goals_bp |
There was a problem hiding this comment.
you could refactor your routes files to be goal_routes.py and task_routes.py for a little more organization
| goal_id = db.Column(db.Integer, primary_key=True) | ||
| goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
| title = db.Column(db.String, nullable = False) | ||
| tasks = db.relationship("Task", back_populates="goal") |
There was a problem hiding this comment.
Because the default value for lazy is True you could technically leave it off. More info here https://docs.sqlalchemy.org/en/14/orm/loading_relationships.html
| task_id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
| title = db.Column(db.String, nullable = False) | ||
| description = db.Column(db.String, nullable = False) | ||
| completed_at = db.Column(db.DateTime) |
There was a problem hiding this comment.
you could add nullable=True here because completed_at doesn't have to exist
| if self.goal_id != None: | ||
| return { | ||
| "id": self.task_id, | ||
| "goal_id": self.goal_id, | ||
| "title": self.title, | ||
| "description": self.description, | ||
| "is_complete": bool(self.completed_at) | ||
| } | ||
| else: | ||
| return { | ||
| "id": self.task_id, | ||
| "title": self.title, | ||
| "description": self.description, | ||
| "is_complete": bool(self.completed_at) | ||
| } |
There was a problem hiding this comment.
Because this is repetitive you could create a response dictionary to check for goal_id something like
response_dict = dict(
id=self.task_id,
title=self.title,
description=self.description,
is_complete= bool(self.completed_at)
)
if self.goal_id:
response_dict["goal_id"] = self.goal_id
return response_dict| tasks_bp = Blueprint("tasks_bp", __name__, url_prefix="/tasks") | ||
| goals_bp = Blueprint("goals_bp", __name__, url_prefix="/goals") | ||
|
|
||
| def send_slack_message(task, message): |
There was a problem hiding this comment.
I like that you pulled this out into a helper function
| return make_response(jsonify(new_task.single_dict()), 201) | ||
|
|
||
| except KeyError: | ||
| return make_response(jsonify({"details":"Invalid data"}), 400) |
There was a problem hiding this comment.
you could make your message more specific to let the user know what's wrong. Invalid data: title or description is missing
| return make_response(jsonify(task.single_dict()), 200) | ||
|
|
||
| @tasks_bp.route("/<task_id>/<mark_completion>", methods=["PATCH"]) | ||
| def task_completion(task_id, mark_completion): |
There was a problem hiding this comment.
this seems similar to lines 119-124. Are they performing the same task?
| # sort_query = request.args.get("sort") | ||
| # title_query = request.args.get("title") | ||
|
|
||
| # if title_query: | ||
| # tasks = Task.query.filter(Task.title.ilike("%" + title_query + "%")) | ||
| # elif description_query: | ||
| # tasks = Task.query.filter(Task.description.ilike("%" + description_query + "%")) | ||
| # elif completed_query == "false": | ||
| # tasks = Task.query.filter(Task.completed_at == None) | ||
| # elif completed_query == "true": | ||
| # tasks = Task.query.filter(Task.completed_at != None) | ||
| # elif sort_query in ("asc","desc"): | ||
| # if sort_query == "asc": | ||
| # tasks = Task.query.order_by(Task.title).all() | ||
| # else: | ||
| # tasks = Task.query.order_by(Task.title.desc()).all() | ||
| # else: |
There was a problem hiding this comment.
you can remove this if you aren't using it
| if "task_ids" in request_body_keys: | ||
| for task_id in request_body["task_ids"]: | ||
| task_ids.append(validate_tasks(task_id)) | ||
| print(task_ids) |
| # tasks = goal.tasks | ||
| # print(tasks) |
No description provided.