Skip to content

Conversation

@lauracastrotech
Copy link

No description provided.

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.

Good work on task-list-api!

Comment on lines +4 to +5
from .routes.task_routes import bp as task_bp
from .routes.goal_routes import bp as goal_bp

Choose a reason for hiding this comment

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

👍

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 (ilke how you did it for goal_id below on line 14).

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

Comment on lines +28 to +29
def is_complete(self):
return self.completed_at is not None

Choose a reason for hiding this comment

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

This logic is simple enough that you could get away with not using a helper function and using a ternary instead on line 22

"id":self.id,
"title":self.title,
"description":self.description,
"is_complete": self.is_complete()

Choose a reason for hiding this comment

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

Suggested change
"is_complete": self.is_complete()
"is_complete": True if self.completed_at else False

Instead of using a helper function, we could just use a ternary operator here.


@bp.get("")
def get_goals():
query=db.select(Goal)

Choose a reason for hiding this comment

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

Nitpick: spacing

Suggested change
query=db.select(Goal)
query = db.select(Goal)

@bp.get("/<id>")
def get_one_task(id):
task = validate_model(Task, id)
return {"task": task.to_dict()},200

Choose a reason for hiding this comment

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

Suggested change
return {"task": task.to_dict()},200
return {"task": task.to_dict()}

Comment on lines +54 to +55
task.title=request_body["title"]
task.description=request_body["description"]

Choose a reason for hiding this comment

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

These two lines will throw unhandled exceptions if the request body does not have keys exactly as "title" or "description". How could you update the logic in this route so that, instead of throwing a server error, you could return a nice error response with an appropriate status code?

Comment on lines +64 to +72
data = {
"token": f"{os.environ.get('SLACK_API_TOKEN')}",
"channel":"test-slack-api",
"text":"Someone just completed the task My Beautiful Task"
}
response = requests.post("https://slack.com/api/chat.postMessage", data=data,
headers={
"Authorization": f"Bearer {os.environ.get('SLACK_API_TOKEN')}"
})

Choose a reason for hiding this comment

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

Prefer all this logic to live in a helper function, maybe like call_slack_api to make this route more single-responsibility and concise.

Comment on lines +66 to +69
"channel":"test-slack-api",
"text":"Someone just completed the task My Beautiful Task"
}
response = requests.post("https://slack.com/api/chat.postMessage", data=data,

Choose a reason for hiding this comment

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

Prefer not to have string literal hanging out in a function. We should use a constant variable to reference the URL and channel name.

We call a string literal that appears in code magic strings: "Magic Strings are literal string values that are used directly in code without a clear explanation...This makes it difficult to maintain and extend the code in the future."

Read more about why we should avoid magic strings here

SLACK_CHANNEL = "test-slack-api"
SLACK_URL = "https://slack.com/api/chat.postMessage"

task = validate_model(Task, id)

data = {
"token": f"{os.environ.get('SLACK_API_TOKEN')}",

Choose a reason for hiding this comment

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

We need to pass in the authorization token as a header, not as a key value pair in the json we send with the request. You do this on line 71 so we should remove the unnecessary code here.

Suggested change
"token": f"{os.environ.get('SLACK_API_TOKEN')}",

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