Conversation
mikellewade
left a comment
There was a problem hiding this comment.
Nice work, Leidy! Please feel free to reach out if you have any questions about the comments left.
| app = Flask(__name__) | ||
|
|
||
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
| #app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/task_list_api_development' |
There was a problem hiding this comment.
You would want to make sure to delete a comment like this so you don't expose your database connection string.
| from .task import Task | ||
|
|
||
| class Goal(db.Model): | ||
| __tablename__ = 'goal' |
There was a problem hiding this comment.
This would automatically be set to goal, is there a reason as to why use this syntax here?
|
|
||
| if tasks_ids: | ||
| tasks_ids_list = [task.id for task in self.tasks] | ||
| goal_dict["task_ids"] = tasks_ids_list |
There was a problem hiding this comment.
We have an endpoint that will provide us with the same data that goal_to_dict["tasks"] will give us so we don't need to put it on the dictionary representation of the object. If I client has the information to retrieve a Goal (i.e. goal.id) then they have the information to get the same information from our endpoint.
| if include_name: | ||
| return {Goal.__name__.lower(): goal_dict} |
There was a problem hiding this comment.
Nice! We could even make this more general and maybe add it to the Base class since we do this for both Task and Goal.
| class Goal(db.Model): | ||
| __tablename__ = 'goal' | ||
| id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) | ||
| title: Mapped[str] = mapped_column(nullable=False) |
There was a problem hiding this comment.
If we want a column to be nullable then we would want to use Optional like you did on line 11. Without Optional we don't need nullable=False.
| query = db.select(Task) #select records from db Model | ||
|
|
||
| # Check for sorting parameter and apply | ||
| sorting_param = request.args.get("sort", "asc") | ||
|
|
||
| if sorting_param == "desc": | ||
| query = query.order_by(Task.title.desc()) | ||
| else: | ||
| query = query.order_by(Task.title) | ||
|
|
||
| #query = query.order_by(Task.id)#select records | ||
| tasks = db.session.scalars(query) #retrieve the records | ||
|
|
||
| response =[] | ||
| for task in tasks: | ||
| response.append(task.to_dict(include_name=False)) |
There was a problem hiding this comment.
Could this sorting logic be placed inside of our get_models_with_filters function?
| @bp.patch("/<task_id>/<task_status>") | ||
| def task_status(task_id, task_status): | ||
|
|
||
| task = validate_model(Task,task_id) #record with id = task_id | ||
|
|
||
| # Update task status based on the task_status value | ||
| if task_status == "mark_complete": | ||
| task.completed_at = datetime.now() | ||
|
|
||
|
|
||
| # Send Slack notification | ||
| send_slack_message(f"Someone just completed the task '{task.title}'") | ||
|
|
||
| elif task_status == "mark_incomplete": | ||
| task.completed_at = None # Set to None to indicate incomplete | ||
| else: | ||
| # Return error response for invalid task_status | ||
| return {"error": "Invalid task status provided"}, 400 | ||
|
|
||
| db.session.commit() #save the changes to db | ||
| return task.to_dict() |
| assert len(result["task"]) == 4 | ||
| assert result["task"]["id"] == 1 | ||
| assert result["task"]["title"] == "Test Task" | ||
| assert result["task"]["description"] is None No newline at end of file |
There was a problem hiding this comment.
Nice work on unit testing the models, this way you are able to make sure that the models work appropriately before integrating them into other parts of our code base.
| def test_invalid_task_status(client,one_task): | ||
| # Act | ||
| response = client.patch('/tasks/1/invalid_status') | ||
| response_data = response.get_json() | ||
|
|
||
| # Assert that the status code is 400 (Bad Request) | ||
| assert response.status_code == 400 | ||
| assert len(response_data) == 1 | ||
| assert response_data == {"error": "Invalid task status provided"} |
| response = client.put("/goals/1", json={ | ||
| "title": "Updated Goal Title" | ||
| }) | ||
| response_body = response.get_json() | ||
|
|
||
| # Assert | ||
| # ---- Complete Assertions Here ---- | ||
| # assertion 1 goes here | ||
| # assertion 2 goes here | ||
| # assertion 3 goes here | ||
| assert response.status_code == 200 | ||
| assert "goal" in response_body | ||
| assert response_body == { | ||
| "goal": { | ||
| "id": 1, | ||
| "title": "Updated Goal Title" | ||
| } | ||
| } | ||
| goal = db.session.get(Goal, 1) # Use Session.get() for SQLAlchemy 2.0 compatibility | ||
| assert goal.title == "Updated Goal Title" |
No description provided.