-
Notifications
You must be signed in to change notification settings - Fork 79
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
Tigers - Mica + Reyna Solar System API Parts 1 + 2 #31
base: main
Are you sure you want to change the base?
Conversation
endpoint for returning single planet
create a planet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nic ework Reyna & Mica, you did really well here. I particularly like your validation work on the routes. I left a few minor comments let me know if you have questions via Slack.
@classmethod | ||
def from_dict(cls, planet_data): | ||
new_planet = Planet(name=planet_data["name"], | ||
description=planet_data["description"], | ||
moons=planet_data["moons"]) | ||
return new_planet | ||
|
||
def to_dict(self): | ||
return { | ||
"id": self.id, | ||
"name": self.name, | ||
"description": self.description, | ||
"moons": self.moons | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great helper methods!
if ("name" not in request_body or "description" not in request_body | ||
or "moons" not in request_body): | ||
return make_response(f"Invalid Request", 400) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great validation, could this be abstracted in to a helper method and used in the update action as well?
planets_response.append(planet.to_dict()) | ||
return jsonify(planets_response) | ||
|
||
def validate_model(cls, model_id): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Great helper function
planet.name = request_body["name"] | ||
planet.description = request_body["description"] | ||
planet.moons = request_body["moons"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some validation here would be good
assert response.status_code == 404 | ||
assert response_body == {"message": "Planet 6 not found"} | ||
|
||
def test_create_one_planet(client): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also test an invalid create action.
def update_planet(planet_id): | ||
planet = validate_model(Planet, planet_id) | ||
|
||
request_body = request.get_json() | ||
|
||
planet.name = request_body["name"] | ||
planet.description = request_body["description"] | ||
planet.moons = request_body["moons"] | ||
|
||
db.session.commit() | ||
|
||
return make_response(f"Planet #{planet.id} successfully updated") | ||
|
||
@planets_bp.route("/<planet_id>", methods=["DELETE"]) | ||
def delete_planet(planet_id): | ||
planet = validate_model(Planet, planet_id) | ||
|
||
db.session.delete(planet) | ||
db.session.commit() | ||
|
||
return make_response(f"Planet #{planet.id} successfully deleted") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting the delete and update actions are untested.
Part 1 - Waves 01 + 02
Part 2 - Waves 03 - 06