From 4dcfc9b65bb6636d584d20724a695c610fc9ba73 Mon Sep 17 00:00:00 2001 From: Lisa Utsett Date: Wed, 14 Dec 2022 14:06:42 -0500 Subject: [PATCH 1/5] Created Planet class in planets.py --- app/planets.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 app/planets.py diff --git a/app/planets.py b/app/planets.py new file mode 100644 index 000000000..d9d3ce941 --- /dev/null +++ b/app/planets.py @@ -0,0 +1,11 @@ +class Planet(): + def __init__(self, id, name, description): + self.id = id + self.name = name + self.description = description + +planets = [ + Planet(id = 3, name = "Earth", description = "habitable"), + Planet(id = 1, name = "Mercury", description = "inhabitable"), + Planet(id = 4, name = "Mars", description = "inhabitable") +] \ No newline at end of file From f7c2f4ab27c1158334553d58cb52922722ad6b98 Mon Sep 17 00:00:00 2001 From: Xuan Hien Pham Date: Wed, 14 Dec 2022 14:44:32 -0500 Subject: [PATCH 2/5] created planets blueprint --- app/__init__.py | 3 +++ app/planets.py | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..aa9cc052c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,5 +3,8 @@ def create_app(test_config=None): app = Flask(__name__) + + from .planets import planets_bp + app.register_blueprint(planets_bp) return app diff --git a/app/planets.py b/app/planets.py index d9d3ce941..f49635539 100644 --- a/app/planets.py +++ b/app/planets.py @@ -1,3 +1,5 @@ +from flask import Blueprint, jsonify + class Planet(): def __init__(self, id, name, description): self.id = id @@ -8,4 +10,19 @@ def __init__(self, id, name, description): Planet(id = 3, name = "Earth", description = "habitable"), Planet(id = 1, name = "Mercury", description = "inhabitable"), Planet(id = 4, name = "Mars", description = "inhabitable") -] \ No newline at end of file +] + +planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") + +@planets_bp.route("", methods=["GET"]) + +def get_all_planets(): + planet_response = [] + for planet in planets: + planet_response.append({ + "id": planet.id, + "name": planet.name, + "description": planet.description + }) + + return jsonify(planet_response) \ No newline at end of file From 7e71408ecbdd04c087dc8af53a51ca808032c8c6 Mon Sep 17 00:00:00 2001 From: Lisa Utsett Date: Thu, 15 Dec 2022 13:21:46 -0500 Subject: [PATCH 3/5] created to_dict method in Planet class --- app/planets.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/planets.py b/app/planets.py index f49635539..44e774bb8 100644 --- a/app/planets.py +++ b/app/planets.py @@ -6,12 +6,21 @@ def __init__(self, id, name, description): self.name = name self.description = description + def to_dict(self): + return { + "id": self.id, + "name": self.name, + "description": self.description + } + + planets = [ Planet(id = 3, name = "Earth", description = "habitable"), Planet(id = 1, name = "Mercury", description = "inhabitable"), Planet(id = 4, name = "Mars", description = "inhabitable") ] + planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") @planets_bp.route("", methods=["GET"]) @@ -19,10 +28,6 @@ def __init__(self, id, name, description): def get_all_planets(): planet_response = [] for planet in planets: - planet_response.append({ - "id": planet.id, - "name": planet.name, - "description": planet.description - }) + planet_response.append(planet.to_dict()) return jsonify(planet_response) \ No newline at end of file From c4ff0402f479692555802d3c9be81928e7e0f76e Mon Sep 17 00:00:00 2001 From: Lisa Utsett Date: Thu, 15 Dec 2022 14:13:36 -0500 Subject: [PATCH 4/5] Added get_planet_by_id function and 400/404 error handling to planets.py --- app/planets.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/planets.py b/app/planets.py index 44e774bb8..3b1b89bd7 100644 --- a/app/planets.py +++ b/app/planets.py @@ -1,4 +1,4 @@ -from flask import Blueprint, jsonify +from flask import Blueprint, jsonify, abort, make_response class Planet(): def __init__(self, id, name, description): @@ -30,4 +30,17 @@ def get_all_planets(): for planet in planets: planet_response.append(planet.to_dict()) - return jsonify(planet_response) \ No newline at end of file + return jsonify(planet_response) + +@planets_bp.route("/", methods=["GET"]) +def get_planet_by_id(planet_id): + try: + planet_id = int(planet_id) + except: + abort(make_response({"message": f"Planet {planet_id} is not an int."}, 400)) + + for planet in planets: + if planet.id == planet_id: + return jsonify(planet.to_dict()) + + abort(make_response({"message":f"planet {planet_id} not found"}, 404)) \ No newline at end of file From 0d44f67dee3621f7dc5b11bed69b658c1272c38f Mon Sep 17 00:00:00 2001 From: Lisa Utsett Date: Thu, 15 Dec 2022 14:26:31 -0500 Subject: [PATCH 5/5] updated get_planet_by_id function, created new validate_planet function that handles 400/404 errors --- app/planets.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/planets.py b/app/planets.py index 3b1b89bd7..4b9033ca3 100644 --- a/app/planets.py +++ b/app/planets.py @@ -20,11 +20,22 @@ def to_dict(self): Planet(id = 4, name = "Mars", description = "inhabitable") ] +def validate_planet(planet_id): + try: + planet_id = int(planet_id) + except: + abort(make_response({"message": f"Planet {planet_id} is not an int."}, 400)) + + for planet in planets: + if planet.id == planet_id: + return planet + + abort(make_response({"message":f"planet {planet_id} not found"}, 404)) + planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") @planets_bp.route("", methods=["GET"]) - def get_all_planets(): planet_response = [] for planet in planets: @@ -34,13 +45,5 @@ def get_all_planets(): @planets_bp.route("/", methods=["GET"]) def get_planet_by_id(planet_id): - try: - planet_id = int(planet_id) - except: - abort(make_response({"message": f"Planet {planet_id} is not an int."}, 400)) - - for planet in planets: - if planet.id == planet_id: - return jsonify(planet.to_dict()) - - abort(make_response({"message":f"planet {planet_id} not found"}, 404)) \ No newline at end of file + planet = validate_planet(planet_id) + return jsonify(planet.to_dict())