Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

db = SQLAlchemy()
migrate = Migrate()

def create_app(test_config=None):
app = Flask(__name__)

app.comfig['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development'

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

from .routes import planet_bp
app.register_blueprint(planet_bp)
Comment on lines +17 to +18

Choose a reason for hiding this comment

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

👍


return app
5 changes: 5 additions & 0 deletions app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def create_app(test_config=None):

from app.models.book import Book

return app
15 changes: 15 additions & 0 deletions app/models/planet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from app import db

class Plant(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
description = db.Column(db.String)
moons = db.Column(db.Integer)

def to_json(self):
return {
"id": self.id,
"name": self.name,
"desciption": self.description,
"moons": self.moons
}
53 changes: 52 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
from flask import Blueprint
from flask import Blueprint, jsonify, abort, make_response


class Planet:
def __init__(self, id, name, description, moons):
self.id = id
self.name = name
self.description = description
self.moons = moons
def to_json(self):
return {
"id": self.id,
"name": self.name,
"desciption": self.description,
"moons": self.moons
}
Comment on lines +4 to +16

Choose a reason for hiding this comment

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

Nice work! This to_json() helper method will be especially helpful as we continue to make more routes.

Choose a reason for hiding this comment

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

Don't forget to add a space in between our functions to improve readability.


planets = [
Planet(1, "Mercury", "the nearest planet to the Sun", 0),
Planet(2, "Venus", "named after the Roman goddess of love and beauty", 0),
Planet(3, "Earth", "Home. the only astronomical object known to harbor life", 1),
Planet(4, "Mars", "is often called the Red Planet", 2),
Planet(5, "Jupiter", "more then 2.5 time the mass of all other planets", 79),
Planet(6, "Saturn", "second-largest planet in the Solar System", 82),
Planet(7, "Uranus", "named after the Greek god of the sky", 27),
Planet(8, "Neptune", "the densest giant planet", 14),
Planet(9, "Pluto", "may or may not be a planet, poor Pluto", 1)
]
Comment on lines +27 to +28

Choose a reason for hiding this comment

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

I disagree... Pluto is most definitely a planet! 😆

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

@planet_bp.route("", methods=["GET"])
def read_planets():
planets_response = []
for planet in planets:
planets_response.append(planet.to_json())
return jsonify(planets_response), 200
Comment on lines +32 to +36

Choose a reason for hiding this comment

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

Great work! We can also use a list comprehension to write the logic for this function:

def read_planets():
    planets_response = [planet.to_json() for planet in planets]
    return jsonify(planets_response), 200


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

for planet in planets:
if planet.id == planet_id:
return planet

abort(make_response({"message":f"planet {planet_id} not found"}, 404))
Comment on lines +38 to +48

Choose a reason for hiding this comment

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

👍 Nice work! The validation for detecting a bad id and for an id with no record looks good!


@planet_bp.route("/<planet_id>", methods=["GET"])
def read_one_planet(planet_id):
planet = validate_planet(planet_id)
return jsonify(planet.to_json(), 200)
Comment on lines +50 to +53

Choose a reason for hiding this comment

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

Nice use of the helper method! This looks great!