Open
Conversation
mikellewade
reviewed
Nov 12, 2024
Comment on lines
+9
to
+41
| def validate_planet_identifier(planet_identifier): | ||
| # Checks if id is int or name. If id return id function if str return name function | ||
| if planet_identifier.isdigit(): | ||
| return validate_planet_id(planet_identifier) | ||
| else: | ||
| return validate_planet_name(planet_identifier) | ||
|
|
||
| # checks if user requested valid planet id and returns 404 if user enters unknown planet id | ||
| def validate_planet_id(planet_id): | ||
| try: | ||
| planet_id = int(planet_id) | ||
| except: | ||
| response = {"message": f"planet {planet_id} invalid"} | ||
| abort(make_response(response, 400)) | ||
|
|
||
| query = db.select(Planet).where(Planet.id == planet_id) | ||
| planet = db.session.scalar(query) | ||
|
|
||
| if not planet: | ||
| abort(make_response({"message": f"planet {planet_id} not found"}, 404)) | ||
|
|
||
| return planet | ||
|
|
||
| # checks if user requested valid planet name and returns 404 if user enter unknown planet name | ||
| def validate_planet_name(planet_name): | ||
| planet_name = planet_name.lower() | ||
| query = db.select(Planet).where(func.lower(Planet.name) == planet_name) | ||
| planet = db.session.scalar(query) | ||
|
|
||
| if not planet: | ||
| abort(make_response({"message": f"planet {planet_name} not found"}, 404)) | ||
|
|
||
| return planet |
There was a problem hiding this comment.
Now that we done a little more refactoring, we could move these functions into their own file to clean up our code base a little.
| planets = db.session.scalars(query) | ||
|
|
||
| planets_response = [planet.get_dict() for planet in planets] | ||
| return planets_response |
Comment on lines
+92
to
+98
| planet = validate_planet_identifier(planet_identifier) | ||
| request_body = request.get_json() | ||
|
|
||
| planet.name = request_body["name"] | ||
| planet.description = request_body["description"] | ||
| planet.distance_from_sun = request_body["distance_from_sun"] | ||
| db.session.commit() |
There was a problem hiding this comment.
Could this be its own helper function?
Comment on lines
+36
to
+43
| @pytest.fixture | ||
| def two_saved_planets(app): | ||
| planet_1 = Planet(name="Mercury", description="Smallest, closest to the Sun, extreme temperatures.", distance_from_sun=57.9) | ||
| planet_2 = Planet(name="Venus", description="Hot, toxic atmosphere, Earth's size, rotates backward.", distance_from_sun=108.2) | ||
|
|
||
| db.session.add_all([planet_1,planet_2]) | ||
|
|
||
| db.session.commit() |
| "description" : "Supports life, water in all forms, protective atmosphere.", | ||
| "distance_from_sun" : 149.6 | ||
| } | ||
|
No newline at end of file |
There was a problem hiding this comment.
Nice testing here! How could we modify our tests to make sure that the records being created and deleted are persisting to the database?
Comment on lines
+85
to
+87
| def get_one_planet(planet_identifier): | ||
| planet = validate_planet_identifier(planet_identifier) | ||
| return planet.get_dict() |
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.
Part 1 Solar API submission (Wave 1 & 2)