Skip to content

Sphinx - Aleida Vieyra & Vlada Rapaport #17

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

Open
wants to merge 33 commits into
base: main
Choose a base branch
from

Conversation

vladarap88
Copy link

No description provided.

@vladarap88 vladarap88 changed the title Sphinx - Vlada Rapaport Sphinx - Aleida Vieyra & Vlada Rapaport Oct 23, 2024
aleidavi and others added 25 commits October 28, 2024 15:31
Copy link

@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.

Great work, everyone! Very clean code!

name=self.name,
description=self.description,
distance_from_sun=self.distance_from_sun,
)

Choose a reason for hiding this comment

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

We could also have a from_dict class method that we could use to construct our plant instances.

from app.models.planet import Planet


planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets")

Choose a reason for hiding this comment

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

Convention is to name this bp, inside of app/__init__.py we could then import this under an alias to prevent name conflicts like so:

from app.routes.planet_routes.py import bp as planets_bp

Comment on lines +12 to +25
request_body = request.get_json()
name = request_body["name"]
description = request_body["description"]
distance_from_sun = request_body["distance_from_sun"]

new_planet = Planet(
name=name, description=description, distance_from_sun=distance_from_sun
)
db.session.add(new_planet)
db.session.commit()

response = new_planet.to_dict()

return response, 201

Choose a reason for hiding this comment

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

We could move all of this logic into a function like create_model that we created in class.

Comment on lines +32 to +48
if name_param:
query = query.where(Planet.name == name_param)

description_param = request.args.get("description")
if description_param:
query = query.where(Planet.description.ilike(f"%{description_param}%"))

distance_from_sun_param = request.args.get("distance_from_sun")
if distance_from_sun_param:
query = query.where(Planet.distance_from_sun.ilike(f"%{distance_from_sun_param}%"))

query = query.order_by(Planet.id)

planets = db.session.scalars(query)

planets_response = [planet.to_dict() for planet in planets]
return planets_response

Choose a reason for hiding this comment

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

Like create_model we could also move this logic into a helper function like get_models_with_filters. How could the helper function look different to serve your needs here?

def get_one_planet(planet_id):
planet = validate_planet(planet_id)

response = planet.to_dict()

Choose a reason for hiding this comment

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

I think I would be okay with returning this on a single line since we aren't doing anything extra here.

Comment on lines +62 to +67
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()

Choose a reason for hiding this comment

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

Could we move this into a helper function? How would it look based off what we learned during refactoring?

Comment on lines +80 to +94
def validate_planet(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:
response = {"message": f"planet {planet_id} not found"}
abort(make_response(response, 404))

return planet

Choose a reason for hiding this comment

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

Where could this function live to better organize our code?


db.session.add_all([planet_1, planet_2])

db.session.commit()

Choose a reason for hiding this comment

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

Nice work on these fixtures here! 👍🏿

Comment on lines +28 to +29
assert response.status_code == 404
assert response_body == {"message": f"planet 1 not found"}

Choose a reason for hiding this comment

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

👍🏿

Comment on lines +43 to +49
assert response.status_code == 201
assert response_body == {
"id": 1,
"name": "Pluto",
"description": "Previously our 9th planet, is now classified as a dwarf planet.",
"distance_from_sun": 3700,
}

Choose a reason for hiding this comment

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

How could we test to see if the created planet is actually in the database?

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