Open
Conversation
… refactored code for updating, reading one planet, read all planets, add planet and validate model routes file
CheezItMan
reviewed
Nov 20, 2022
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Kat & Sunny, this is well done. I left some minor comments here. If you have questions ping me in slack.
Comment on lines
+10
to
+32
| def to_dict(self): | ||
| return { | ||
| "id": self.id, | ||
| "name": self.name, | ||
| "description": self.description, | ||
| "diameter": self.diameter | ||
| } | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, planet_data): | ||
| new_planet = Planet(name=planet_data["name"], | ||
| description=planet_data["description"], | ||
| diameter=planet_data["diameter"]) | ||
| return new_planet | ||
|
|
||
| def update(self, req_body): | ||
| try: | ||
| self.name = req_body["name"] | ||
| self.description = req_body["description"] | ||
| self.diameter = req_body["diameter"] | ||
| except KeyError as error: | ||
| abort(make_response({"message": f"Missing attribute: {error}"},400)) | ||
|
|
Comment on lines
+10
to
+12
| request_body = request.get_json() | ||
| new_planet = Planet.from_dict(request_body) | ||
|
|
There was a problem hiding this comment.
Some some validation on request body would be nice here to ensure required fields are present.
| planets_response = [planet.to_dict() for planet in planets] | ||
| return jsonify(planets_response) | ||
|
|
||
| def validate_model(cls, model_id): |
| }] | ||
| assert len(response_body) == 2 | ||
|
|
||
| def test_create_one_planet(client): |
There was a problem hiding this comment.
Having a test with an invalid request body would be nice.
Comment on lines
+54
to
+73
| def update_planet(planet_id): | ||
| planet = validate_model(Planet,planet_id) | ||
|
|
||
| request_body = request.get_json() | ||
|
|
||
| planet.update(request_body) | ||
| db.session.commit() | ||
|
|
||
| return make_response(jsonify(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(jsonify(f"Planet #{planet.id} successfully deleted")) | ||
|
|
||
|
|
There was a problem hiding this comment.
Just noting these functions are untested.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.