-
Notifications
You must be signed in to change notification settings - Fork 61
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
Sharks - Morgan B. and Huma H. #21
base: main
Are you sure you want to change the base?
Changes from 2 commits
3cc5774
5f18e47
1ae4803
f962a33
a968b9e
2f43a2d
59e6abc
a9ca585
bcf5ceb
c66c7a5
83b5578
43cdbc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,50 @@ | ||
from flask import Blueprint | ||
from flask import Blueprint, jsonify, abort, make_response | ||
|
||
planets_bp = Blueprint("planets", __name__, url_prefix="/planets") | ||
|
||
class Planet: | ||
def __init__ (self, id, name, description, moon_count): | ||
self.id = id | ||
self.name = name | ||
self.description = description | ||
self.moon_count = moon_count | ||
|
||
def to_json(self): | ||
return { | ||
"id": self.id, | ||
"name": self.name, | ||
"description": self.description, | ||
"moon_count": self.moon_count | ||
} | ||
|
||
planets = [ | ||
Planet(1, "Mercury", "red planet", 1), | ||
Planet(2, "Venus", "blue planet", 3), | ||
Planet(1, "Earth", "water planet", 1) | ||
] | ||
|
||
def validate_planet(id): | ||
try: | ||
id = int(id) | ||
except: | ||
abort(make_response({"message": f"Planet {id} is not valid"}, 400)) | ||
|
||
for planet in planets: | ||
if planet.id == id: | ||
return planet | ||
|
||
return abort(make_response({"message": f"Planet {id} not found"}, 404)) | ||
|
||
@planets_bp.route("", methods=["GET"]) | ||
def get_planets(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job naming the method to describe what this route does. In the future, when you split your routes up into distinct route files (cat_routes.py and dog_routes.py for example in a routes directory) then you could have method names like get_all() and get_one() since we know that all the routes in cat_routes.py are related to the Cat class. |
||
planet_response_body = [] | ||
for planet in planets: | ||
planet_response_body.append(planet.to_json()) | ||
|
||
return jsonify(planet_response_body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding 200 status code adds clarity even though it happens by default return jsonify(planet_response_body), 200 |
||
|
||
@planets_bp.route("/<planet_id>", methods=["GET"]) | ||
def read_one_planet(planet_id): | ||
planet = validate_planet(planet_id) | ||
|
||
return jsonify(planet.to_json()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above, you can return status code 200 here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏽