Skip to content

Conversation

@Ramora6627
Copy link

No description provided.

Copy link

@audreyandoy audreyandoy left a comment

Choose a reason for hiding this comment

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

Raha and Jamie! Great work in finishing solar system. I have provided comments on small ways y'all can improve the API.

🦈 ✨

migrate = Migrate()
load_dotenv()

def create_app(test_config=None):

Choose a reason for hiding this comment

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

👍

@@ -0,0 +1,15 @@
from app import db

class Planet(db.Model):

Choose a reason for hiding this comment

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

👍

db.init_app(app)
migrate.init_app(app, db)

from .routes.routes import planet_bp

Choose a reason for hiding this comment

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

We may want to change the import to planet_routes.py since it contains the nested route for moons.

And planet_routes is a much better file name to hold all the planet routes.

Suggested change
from .routes.routes import planet_bp
from .routes.planet_routes import planet_bp

Comment on lines +5 to +7
name = db.Column(db.String)
description = db.Column(db.String)
moons = db.Column(db.Integer)

Choose a reason for hiding this comment

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

Should we be able to make planets with no name? To prevent our API from creating a planet with a null name, we can set that column to nullable = False.

Additionally, shouldn't moons be a relationship instead of a column? I also noticed that y'all forgot to add back_populates to Planet. Remember that if we use back_populates, we need to add it to both the parent and child models.

    name = db.Column(db.String, nullable=False)
    description = db.Column(db.String)
    moons = db.Relationship('Moon', back_populates='planet')

Comment on lines +9 to +15
def to_json(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"moons": self.moons
}

Choose a reason for hiding this comment

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

👍

Comment on lines +85 to +94
@planets_bp.route("/<planet_id>/moons", methods=["POST"])
def write_moon_to_planet(planet_id):
planet = validate_planet(planet_id)
request_body = request.get_json()
new_moon = Moon(name=request_body["name"], planet=planet)

db.session.add(new_moon)
db.session.commit()

return make_response(jsonify(f"Moon {new_moon.name} of the planet {new_moon.planet.name} successfully created"), 201) No newline at end of file

Choose a reason for hiding this comment

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

👍

Choose a reason for hiding this comment

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

The previous comment about not needing make_response can also apply here.

@@ -0,0 +1,75 @@
from app import db

Choose a reason for hiding this comment

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

Comments from planet_routes.py can also apply to this file.

Comment on lines +29 to +34
def two_saved_planets(app):
Mercury = Planet(name = "Mercury", description = "This is the first planet", moons = 2)
Venus = Planet(name = "Venus", description = "named after the Roman goddess of love and beauty", moons = 0)

db.session.add_all([Mercury,Venus])
db.session.commit() No newline at end of file

Choose a reason for hiding this comment

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

👍

Nice work! My one critique is about Mercury and Venus variables looking like classes. Capitalizing the first letter in a name is reserved for class names like, Planet. To avoid confusion and follow pep8 guidelines, let's continue to practice lowercase for our variable names.

@@ -0,0 +1,37 @@

Choose a reason for hiding this comment

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

👍


# moons_bp = Blueprint("moons", __name__, url_prefix="/moons")

# do we even need this No newline at end of file

Choose a reason for hiding this comment

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

Nope, you don't need this file unless you're planning on adding future enhancements for moons 🌚 ✨

Comment on lines +1 to +14
# Helper function:
from flask import abort, make_response, jsonify
from app.models.planet import Planet

def validate_planet(planet_id):
try:
planet_id = int(planet_id)
except:
abort(make_response(jsonify(f"planet {planet_id} invalid"), 400))

planet = Planet.query.get(planet_id)
if not planet:
abort(make_response(jsonify(f"planet {planet_id} not found"), 404))
return planet No newline at end of file

Choose a reason for hiding this comment

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

Helpers look great! 👍 We may want to consider making the validation code as generic as possible to process any class and id or renaming this file to helper_planet_routes or planet_routes_helper. Either would be helpful if we expand this project to contain more routes for moons, suns, etc.

A more generic validation helper method would look like:

from flask import make_response, abort

def validate_object(cls, id):
    try:
        id = int(id)
    except:
        return abort(make_response({"message": f"{cls} {id} is invalid"}, 400))

    obj = cls.query.get(id)
    
    if not obj:
        abort(make_response({"message": f"{cls} {id} not found"},404))
    
    return obj

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