Skip to content

Conversation

@linakl19
Copy link

@linakl19 linakl19 commented May 9, 2025

No description provided.

linakl19 added 30 commits May 1, 2025 14:37
Copy link

@yangashley yangashley left a comment

Choose a reason for hiding this comment

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

Great job on task-list-api and nice job making frequent commits!

Let me know if you have questions about my comments.

Comment on lines +4 to +5
from .routes.task_routes import bp as tasks_bp
from .routes.goal_routes import bp as goals_bp

Choose a reason for hiding this comment

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

👍


@classmethod
def from_dict(cls, goal_data):
new_goal= cls(title=goal_data["title"])

Choose a reason for hiding this comment

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

Nitpick: spacing

Suggested change
new_goal= cls(title=goal_data["title"])
new_goal = cls(title=goal_data["title"])

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str]
description: Mapped[str]
completed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True)

Choose a reason for hiding this comment

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

Using Optional[] is all that we need to indicate that a field is nullable so we don't need to also include mapped_column with nullable=True.

Suggested change
completed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True)
completed_at: Mapped[Optional[datetime]]

Here's the SQLAlchemy documentation about nullability

image

Comment on lines +46 to +50
return dict(
id = goal.id,
title = goal.title,
tasks = [task.to_dict() for task in goal.tasks]
)

Choose a reason for hiding this comment

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

Could we use the to_dict method you wrote in Goal?

Suggested change
return dict(
id = goal.id,
title = goal.title,
tasks = [task.to_dict() for task in goal.tasks]
)
return goal.to_dict()

Comment on lines +72 to +82
for task_in_goal in goal.tasks:
if task_in_goal.id not in task_ids:
task_in_goal.goal_id = None

valid_tasks_ids = []

# Assign valid tasks to this goal
for task in valid_tasks:
valid_tasks_ids.append(task.id)
if task.goal_id != goal.id:
task.goal_id = goal.id

Choose a reason for hiding this comment

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

What if we don't need to unlink the tasks because we can just replace them?

Suggested change
for task_in_goal in goal.tasks:
if task_in_goal.id not in task_ids:
task_in_goal.goal_id = None
valid_tasks_ids = []
# Assign valid tasks to this goal
for task in valid_tasks:
valid_tasks_ids.append(task.id)
if task.goal_id != goal.id:
task.goal_id = goal.id
tasks = [validate_model(task_id) for task_id in task_ids]
goal.tasks = tasks

try:
new_model = cls.from_dict(model_data)
except KeyError as error:
response = {"details": "Invalid data"}

Choose a reason for hiding this comment

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

It might be nice to provide even more details in the response you send back so that the client knows how to fix up the request body they will re-send.

Suggested change
response = {"details": "Invalid data"}
response = {"details": f"Invalid data for field: {error.args[0]}"}

For example, this would evaluate to "title" for example if I sent a bad request body to create a Task.

Comment on lines +57 to +67
path = "https://slack.com/api/chat.postMessage"
API_KEY = os.environ.get("API_KEY")
headers = {"Authorization": f"Bearer {API_KEY}"}
body ={
"channel": "task-notifications",
"text": f"Someone just completed the task {task.title}"
}

if not task.completed_at:
task.completed_at = datetime.now()
slack_post = requests.post(path, headers=headers, json=body )

Choose a reason for hiding this comment

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

Prefer all the logic related to calling Slack to be in a helper function (maybe like call_slack_api) to make this route more concise and single-responsibility.

"""
task = validate_model(Task, task_id)

path = "https://slack.com/api/chat.postMessage"

Choose a reason for hiding this comment

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

This string is a constant value so we should name the variable with all capital letters.

Suggested change
path = "https://slack.com/api/chat.postMessage"
PATH = "https://slack.com/api/chat.postMessage"

Comment on lines +60 to +61
body ={
"channel": "task-notifications",

Choose a reason for hiding this comment

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

Nitpick: spacing

Also prefer the channel to be referenced by a constant variable too.

Suggested change
body ={
"channel": "task-notifications",
SLACK_CHANNEL = "task-notifications"
body = {
"channel": SLACK_CHANNEL,


if not task.completed_at:
task.completed_at = datetime.now()
slack_post = requests.post(path, headers=headers, json=body )

Choose a reason for hiding this comment

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

Nitpick: spacing

Suggested change
slack_post = requests.post(path, headers=headers, json=body )
slack_post = requests.post(path, headers=headers, json=body)

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