Skip to content
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

Cedar - Rebeca & Lizet #13

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from flask import Flask


def create_app(test_config=None):
def create_app():
app = Flask(__name__)
from .routes import planets_bp
app.register_blueprint(planets_bp)

Choose a reason for hiding this comment

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

Consider adding a line space to enhance readability

Suggested change
app = Flask(__name__)
from .routes import planets_bp
app.register_blueprint(planets_bp)
app = Flask(__name__)
from .routes import planets_bp
app.register_blueprint(planets_bp)


return app
42 changes: 42 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
from flask import Blueprint
from flask import Blueprint, jsonify

class Planet:
def __init__(self, id, name, description, moon):
self.id = id
self.name = name
self.description = description
self.moon = moon

planets = [
Planet(1, "Saturn", "A gassy, heavy, giant who's sixth from the sun. Most likely compposed of iron, nickel, and rock.", 82),
Planet(2, "Mercury", "When it retrogrades everything in the world sucks.", 0),
Planet(3, "Venus", "It is named after the goddess of love and beauty. Second brightest object in the sky.", 0)
]

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

@planets_bp.route("", methods=["GET"])

def read_planets():
planet_response = []
for planet in planets:
planet_response.append({
"id": planet.id,
"name": planet.name,
"description": planet.description,
"moon": planet.moon
})
return jsonify(planet_response)

@planets_bp.route("/<planet_id>", methods=["GET"])
def read_single_planet(planet_id):
planet_id = int(planet_id)
for planet in planets:
if planet.id == planet.id:
return {
"id": planet.id,
"name": planet.name,
"description": planet.description,
"moon": planet.moon
}

Choose a reason for hiding this comment

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

Writing an instance method for the Planet class that returns this dictionary with planet data can DRY up your code. You will find that you need to do this work for other route functions too.