Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from flask import Flask
from .routes.planet_routes import planets_bp


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

return app
# Register Blueprints here
app.register_blueprint(planets_bp)

return app
Empty file added app/models/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions app/models/planet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Planet:
def __init__(self, id, name, description):
self.id = id
self.name = name
self.description = description

Choose a reason for hiding this comment

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

How would you write the to_dict instance method so you can take an instance of Planet and turn it into a dictionary that will be returned as a response to a client's request?

planets = [
Planet(1, "Neptune", "Giant and icy planet"),
Planet(2, "Uranus", "Rotates on its side"),
Planet(3, "Saturn", "Has beautiful rings"),
Planet(4, "Jupiter", "Largest planet in the solar system"),
Planet(5, "Mars", "The red planet"),
Planet(6, "Earth", "Our home planet"),
Planet(7, "Venus", "Second planet from the sun"),
Planet(8, "Mercury", "Smallest and hottest planet"),
Planet(6, "Pluto", "The planet they declassified as a planet")
]
2 changes: 0 additions & 2 deletions app/routes.py

This file was deleted.

Empty file added app/routes/__init__.py
Empty file.
62 changes: 62 additions & 0 deletions app/routes/planet_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from flask import Blueprint, abort, make_response, request
from app.models.planet import planets
# from ..db import db

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

@planets_bp.get("")
def get_all_planets():

planets_response = []
for planet in planets:
planets_response.append(
{
"id": planet.id,
"name": planet.name,
"description": planet.description
}
Comment on lines +34 to +39

Choose a reason for hiding this comment

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

Prefer your Planet model has a to_dict method so that it can be called in your different routes instead repeating code. It would also help your routes be more concise.

)
return planets_response

@planets_bp.get("/<planet_id>")
def get_one_planet(planet_id):
planet = validate_planet(planet_id)

for planet in planets:
if planet.id == int(planet_id):
return {
"id": planet.id,
"name": planet.name,
"description": planet.description,
}

Choose a reason for hiding this comment

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

After your model has been validated, we don't need to loop through all the planets again to see if the id matches. Have a look at the validate_planet function and see that you already check to see if the id matches on line 40-42.

Suggested change
for planet in planets:
if planet.id == int(planet_id):
return {
"id": planet.id,
"name": planet.name,
"description": planet.description,
}
return {
"id": planet.id,
"name": planet.name,
"description": planet.description,
}


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

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

response = {"message": f"planet {planet_id} not found"}
abort(make_response(response, 404))

# @planets_bp.post("")
# def create_planet():
# request_body = request.get_json()
# title = request_body["title"]
# description = request_body["description"]

# new_book = Planet(title=title, description=description)
# db.session.add(new_planet)
# db.session.commit()

# response = {
# "id": new_planet.id,
# "title": new_planet.name,
# "description": new_planet.description,
# }
# return response, 201

Choose a reason for hiding this comment

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

We'll have a look at how the model needs to be updated in order for the POST route to work!