diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..994b32bb4 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,7 +1,10 @@ from flask import Flask +from app.routes.planet_routes import planets_bp def create_app(test_config=None): app = Flask(__name__) + app.register_blueprint(planets_bp) + return app diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/models/planet.py b/app/models/planet.py new file mode 100644 index 000000000..52264b9a2 --- /dev/null +++ b/app/models/planet.py @@ -0,0 +1,17 @@ +class Planet: + def __init__(self, id, name, description, distance_from_sun): + self.id = id + self.name = name + self.description = description + self.distance_from_sun = distance_from_sun + +planets =[ + Planet(1, "Mercury", "smallest and hottest planet", 57.9), + Planet(2, "Venus", "second planet from the sun", 108.2), + Planet(3, "Earth", "our home planet", 149.6), + Planet(4, "Mars", "the red planet", 227.9), + Planet(5, "Jupiter", "largest planet in the solar system", 778.3), + Planet(6, "Saturn", "has beautiful rings", 1437.0), + Planet(7, "Uranus", "rotates on its side", 2871.0), + Planet(8, "Neptune", "farthest planet from the sun", 4497.1) + ] \ No newline at end of file diff --git a/app/routes.py b/app/routes.py deleted file mode 100644 index 8e9dfe684..000000000 --- a/app/routes.py +++ /dev/null @@ -1,2 +0,0 @@ -from flask import Blueprint - diff --git a/app/routes/__init__.py b/app/routes/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py new file mode 100644 index 000000000..c4b751be5 --- /dev/null +++ b/app/routes/planet_routes.py @@ -0,0 +1,50 @@ +from flask import Blueprint, abort, make_response +from app.models.planet import planets + + +planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") + +@planets_bp.get("") +def get_all_planets(): + result_planets = [] + for planet in planets: + result_planets.append( + { + "id" : planet.id, + "name": planet.name, + "description" : planet.description, + "distance_from_sun" : planet.distance_from_sun + } + ) + return result_planets + +# Helper function for error handling +def validate_planet(id): + try: + input_id = int(id) + except: + abort(make_response(f"Planet id: '{id}' is invalid. Enter a valid planet number from 1 to 8.", 400)) + + + for planet in planets: + if planet.id == input_id: + return planet + abort(make_response(f"Planet id: '{id}' not found. Enter planet id from 1 to 8", 404)) + + + +@planets_bp.get("/") +def get_one_planet(id): + + planet = validate_planet(id) + return { + "id" : planet.id, + "name" : planet.name, + "description" : planet.description, + "distance_from_sun" : planet.distance_from_sun + } + + + + +