Conversation
yangashley
left a comment
There was a problem hiding this comment.
Nice work! The project is correctly organized and your model and routes look good. Let me know if you have questions about my comments.
app/models/planet.py
Outdated
| self.id = id | ||
| self.name = name | ||
| self.description = description | ||
|
|
There was a problem hiding this comment.
How would you write the to_dict instance method so you can take an instance of Planet and turn it into a dictionary that will be returned as a response to a client's request?
app/routes/planet_routes.py
Outdated
| for planet in planets: | ||
| if planet.id == int(planet_id): | ||
| return { | ||
| "id": planet.id, | ||
| "name": planet.name, | ||
| "description": planet.description, | ||
| } |
There was a problem hiding this comment.
After your model has been validated, we don't need to loop through all the planets again to see if the id matches. Have a look at the validate_planet function and see that you already check to see if the id matches on line 40-42.
| for planet in planets: | |
| if planet.id == int(planet_id): | |
| return { | |
| "id": planet.id, | |
| "name": planet.name, | |
| "description": planet.description, | |
| } | |
| return { | |
| "id": planet.id, | |
| "name": planet.name, | |
| "description": planet.description, | |
| } |
app/routes/planet_routes.py
Outdated
| # @planets_bp.post("") | ||
| # def create_planet(): | ||
| # request_body = request.get_json() | ||
| # title = request_body["title"] | ||
| # description = request_body["description"] | ||
|
|
||
| # new_book = Planet(title=title, description=description) | ||
| # db.session.add(new_planet) | ||
| # db.session.commit() | ||
|
|
||
| # response = { | ||
| # "id": new_planet.id, | ||
| # "title": new_planet.name, | ||
| # "description": new_planet.description, | ||
| # } | ||
| # return response, 201 |
There was a problem hiding this comment.
We'll have a look at how the model needs to be updated in order for the POST route to work!
yangashley
left a comment
There was a problem hiding this comment.
Nice work on part 2 of solar-system!
| { | ||
| "id": planet.id, | ||
| "name": planet.name, | ||
| "description": planet.description, | ||
| "diameter": planet.diameter | ||
| } |
There was a problem hiding this comment.
Prefer your Planet model has a to_dict method so that it can be called in your different routes instead repeating code. It would also help your routes be more concise.
| response = { | ||
|
|
||
| "id": new_planet.id, | ||
| "name": new_planet.name, | ||
| "description": new_planet.description, | ||
| "diameter": new_planet.diameter | ||
| } |
| return { | ||
|
|
||
| "id": planet.id, | ||
| "name": planet.name, | ||
| "description": planet.description, | ||
| "diameter": planet.diameter | ||
| } |
There was a problem hiding this comment.
Could call to_dict on planet here instead so creating this literal dictionary isn't happening three different times in your route file.
There was a problem hiding this comment.
Is this file just for practice? I'm not clear on what it's testing and it's not inside your test directory.
No description provided.