Skip to content

Leidy Sphinx Task List API #43

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Leidy-Martinez
Copy link

No description provided.

Copy link

@mikellewade mikellewade left a comment

Choose a reason for hiding this comment

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

Nice work, Leidy! Please feel free to reach out if you have any questions about the comments left.

import os

def create_app(config=None):
app = Flask(__name__)

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/task_list_api_development'

Choose a reason for hiding this comment

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

You would want to make sure to delete a comment like this so you don't expose your database connection string.


class Goal(db.Model):
__tablename__ = 'goal'

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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.

Comment on lines +34 to +35
if include_name:
return {Goal.__name__.lower(): goal_dict}

Choose a reason for hiding this comment

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

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.

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str] = mapped_column(nullable=False)

Choose a reason for hiding this comment

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

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.

Comment on lines +28 to +43

# 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))

Choose a reason for hiding this comment

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

Could this sorting logic be placed inside of our get_models_with_filters function?

Comment on lines +70 to +90
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()

Choose a reason for hiding this comment

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

Nice work here! ⭐️

assert len(result["task"]) == 4
assert result["task"]["id"] == 1
assert result["task"]["title"] == "Test Task"
assert result["task"]["description"] is None

Choose a reason for hiding this comment

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

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.

Comment on lines +155 to +163
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"}

Choose a reason for hiding this comment

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

⭐️

Comment on lines +92 to +108
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"

Choose a reason for hiding this comment

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

Great work!

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