From ff83c6ebb85537e3317f502fa4e1cd7350570519 Mon Sep 17 00:00:00 2001 From: Jamie H Date: Fri, 22 Apr 2022 14:17:55 -0400 Subject: [PATCH 1/4] Created Planet class and instantiated planets --- app/routes.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..92f697e09 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,27 @@ from flask import Blueprint + +class Planet: + def __init__(self, id, name, description, moons): + self.id = id + self.name = name + self.description = description + self.order = order + + planets = [ + Planet(0, "Mercury", "the nearest planet to the Sun", 0) + Planet(1, "Venus", "named after the Roman goddess of love and beauty", 0) + Planet(2, "Earth", "Home. the only astronomical object known to harbor life", 1) + Planet(3, "Mars", "is often called the Red Planet", 2) + Planet(4, "Jupiter", "more then 2.5 time the mass of all other planets", 79) + Planet(5, "Saturn", "second-largest planet in the Solar System", 82) + Planet(6, "Uranus", "named after the Greek god of the sky", 27) + Planet(7, "Neptune", "the densest giant planet", 14) + Planet(8, "Pluto", "may or may not be a planet, poor Pluto", 1) + ] + planet_bp = Blueprint("planets", __name__, url_prefix="/planets") + + # @planet_bp.route("", methods=["GET"]) + +# Define a Planet class with the attributes id, name, and description, and one additional attribute +# Create a list of Planet instances \ No newline at end of file From ae6c657089d2832ae7e8a99c81eb75da1d89d189 Mon Sep 17 00:00:00 2001 From: Jamie H Date: Fri, 22 Apr 2022 14:40:39 -0400 Subject: [PATCH 2/4] Created a GET response and tested successfully --- app/__init__.py | 2 ++ app/routes.py | 40 +++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..6368d4638 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,4 +4,6 @@ def create_app(test_config=None): app = Flask(__name__) + from .routes import planet_bp + app.register_blueprint(planet_bp) return app diff --git a/app/routes.py b/app/routes.py index 92f697e09..f01d8ef47 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint +from flask import Blueprint, jsonify class Planet: @@ -6,22 +6,32 @@ def __init__(self, id, name, description, moons): self.id = id self.name = name self.description = description - self.order = order + self.moons = moons - planets = [ - Planet(0, "Mercury", "the nearest planet to the Sun", 0) - Planet(1, "Venus", "named after the Roman goddess of love and beauty", 0) - Planet(2, "Earth", "Home. the only astronomical object known to harbor life", 1) - Planet(3, "Mars", "is often called the Red Planet", 2) - Planet(4, "Jupiter", "more then 2.5 time the mass of all other planets", 79) - Planet(5, "Saturn", "second-largest planet in the Solar System", 82) - Planet(6, "Uranus", "named after the Greek god of the sky", 27) - Planet(7, "Neptune", "the densest giant planet", 14) - Planet(8, "Pluto", "may or may not be a planet, poor Pluto", 1) - ] - planet_bp = Blueprint("planets", __name__, url_prefix="/planets") +planets = [ + Planet(0, "Mercury", "the nearest planet to the Sun", 0), + Planet(1, "Venus", "named after the Roman goddess of love and beauty", 0), + Planet(2, "Earth", "Home. the only astronomical object known to harbor life", 1), + Planet(3, "Mars", "is often called the Red Planet", 2), + Planet(4, "Jupiter", "more then 2.5 time the mass of all other planets", 79), + Planet(5, "Saturn", "second-largest planet in the Solar System", 82), + Planet(6, "Uranus", "named after the Greek god of the sky", 27), + Planet(7, "Neptune", "the densest giant planet", 14), + Planet(8, "Pluto", "may or may not be a planet, poor Pluto", 1) + ] +planet_bp = Blueprint("planets", __name__, url_prefix="/planets") - # @planet_bp.route("", methods=["GET"]) +@planet_bp.route("", methods=["GET"]) +def read_planets(): + planets_response = [] + for planet in planets: + planets_response.append({ + "id": planet.id, + "name": planet.name, + "desciption": planet.description, + "moons": planet.moons + }) + return jsonify(planets_response), 200 # Define a Planet class with the attributes id, name, and description, and one additional attribute # Create a list of Planet instances \ No newline at end of file From f8eaf24c3025b462d90dbec625d9f52746603401 Mon Sep 17 00:00:00 2001 From: Raha Date: Mon, 25 Apr 2022 18:54:04 -0400 Subject: [PATCH 3/4] part 1 is done --- app/routes.py | 52 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/app/routes.py b/app/routes.py index f01d8ef47..547f484a4 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint, jsonify +from flask import Blueprint, jsonify, abort, make_response class Planet: @@ -7,17 +7,24 @@ def __init__(self, id, name, description, moons): 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 + } planets = [ - Planet(0, "Mercury", "the nearest planet to the Sun", 0), - Planet(1, "Venus", "named after the Roman goddess of love and beauty", 0), - Planet(2, "Earth", "Home. the only astronomical object known to harbor life", 1), - Planet(3, "Mars", "is often called the Red Planet", 2), - Planet(4, "Jupiter", "more then 2.5 time the mass of all other planets", 79), - Planet(5, "Saturn", "second-largest planet in the Solar System", 82), - Planet(6, "Uranus", "named after the Greek god of the sky", 27), - Planet(7, "Neptune", "the densest giant planet", 14), - Planet(8, "Pluto", "may or may not be a planet, poor Pluto", 1) + 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) ] planet_bp = Blueprint("planets", __name__, url_prefix="/planets") @@ -25,13 +32,22 @@ def __init__(self, id, name, description, moons): def read_planets(): planets_response = [] for planet in planets: - planets_response.append({ - "id": planet.id, - "name": planet.name, - "desciption": planet.description, - "moons": planet.moons - }) + planets_response.append(planet.to_json()) return jsonify(planets_response), 200 -# Define a Planet class with the attributes id, name, and description, and one additional attribute -# Create a list of Planet instances \ No newline at end of file +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)) + +@planet_bp.route("/", methods=["GET"]) +def read_one_planet(planet_id): + planet = validate_planet(planet_id) + return jsonify(planet.to_json(), 200) \ No newline at end of file From 74e5e4199a913dab32758e444267ea130a687200 Mon Sep 17 00:00:00 2001 From: Jamie H Date: Fri, 29 Apr 2022 14:15:40 -0400 Subject: [PATCH 4/4] Set up both constructors and planet model class --- app/__init__.py | 11 +++++++++++ app/models/__init__.py | 5 +++++ app/models/planet.py | 15 +++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 app/models/__init__.py create mode 100644 app/models/planet.py diff --git a/app/__init__.py b/app/__init__.py index 6368d4638..60b2c07c2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,9 +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) + return app diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 000000000..ec9c5eaac --- /dev/null +++ b/app/models/__init__.py @@ -0,0 +1,5 @@ +def create_app(test_config=None): + + from app.models.book import Book + + return app \ No newline at end of file diff --git a/app/models/planet.py b/app/models/planet.py new file mode 100644 index 000000000..c06af9a2c --- /dev/null +++ b/app/models/planet.py @@ -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 + }