Skip to content

C22 Tatyana - Leidy -Solar API#9

Open
taty1202 wants to merge 12 commits intoAda-C22:mainfrom
taty1202:main
Open

C22 Tatyana - Leidy -Solar API#9
taty1202 wants to merge 12 commits intoAda-C22:mainfrom
taty1202:main

Conversation

@taty1202
Copy link
Copy Markdown

Part 1 Solar API submission (Wave 1 & 2)

Copy link
Copy Markdown

@mikellewade mikellewade left a comment

Choose a reason for hiding this comment

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

Nice work! ✨

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍🏿

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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this be its own helper function?

Comment thread tests/conftest.py
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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work on your fixtures!

"description" : "Supports life, water in all forms, protective atmosphere.",
"distance_from_sun" : 149.6
}

No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⭐️

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.

3 participants