Skip to content
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

C17 Sea turtles, Celina Barron, Shelby Faulconer, and Tiffini Hyatt #13

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0d6e418
Created Planet class
tiffinihyatt Apr 22, 2022
d3635a2
Started to create planets array
tiffinihyatt Apr 22, 2022
0d8bbec
Wave 1 Complete
ArmchairGraduate Apr 22, 2022
c59cbbd
added blueprint to __init__.py, added planet GET method to return pl…
pickled-bot Apr 22, 2022
b9e3617
created validate_planet helper function to check for valid planet id.
ArmchairGraduate Apr 25, 2022
174a417
added function to_dict to Planet class
pickled-bot Apr 25, 2022
4c96a0d
created route for planet_id with function get_planet_by_id
pickled-bot Apr 25, 2022
aef2191
Set up SQLAlchemy
tiffinihyatt Apr 29, 2022
e4ae7ad
added planet model
ArmchairGraduate Apr 29, 2022
dfe4065
added create planet endpoint
pickled-bot Apr 29, 2022
7f2ed54
Added read all planets GET endpoint
tiffinihyatt Apr 29, 2022
0b3a9c7
updated validate_planet function
pickled-bot May 3, 2022
c58002c
added get_planet_by_id function with GET method
pickled-bot May 3, 2022
43e4d2d
Created update_planet endpoint
tiffinihyatt May 3, 2022
8d6d02d
created DELETE endpoint
ArmchairGraduate May 3, 2022
9c95797
Removed old code and created scratch.py file
tiffinihyatt May 4, 2022
f3c2a74
Created to_dictionary Class method and refactored endpoints to use to…
tiffinihyatt May 4, 2022
bd3ba79
refactored create_app method
ArmchairGraduate May 5, 2022
7473c59
created tests folder and made pytest fixtures
pickled-bot May 5, 2022
72fd43a
Updated scratch file
tiffinihyatt May 5, 2022
0509329
Created test_get_all_planets_with_no_records
tiffinihyatt May 5, 2022
2df8138
Created two_saved_planets for test configurations
tiffinihyatt May 5, 2022
de695d4
Created and passed test_get_one_planet
tiffinihyatt May 5, 2022
71ae69d
Made test for planet id not found, 404
pickled-bot May 6, 2022
6ac2887
created get all planets test
ArmchairGraduate May 6, 2022
48837ce
Create test_post_planet_returns_201 and refactored routes to include …
tiffinihyatt May 6, 2022
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
3 changes: 2 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

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

from .routes import planets_bp
app.register_blueprint(planets_bp)
return app
65 changes: 64 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,65 @@
from flask import Blueprint
import json
from unicodedata import name
from flask import Blueprint, jsonify, abort, make_response


class Planet:
def __init__(self, id, name, description, dist_from_sun):
self.id = id
self.name = name
self.description = description
self.dist_from_sun = dist_from_sun

def to_dict(self):
return {
"id" : self.id,
"name" : self.name,
"description" : self.description,
"distance from sun" : self.dist_from_sun
}

planets = [
Planet(1, "Mercury", "rocky", 1),
Planet(2, "Venus", "rocky", 2),
Planet(3, "Earth", "water", 3),
Planet(4, "Mars", "red", 4),
Planet(5, "Jupiter", "big", 5),
Planet(6, "Saturn", "rings", 6),
Planet(7, "Uranus", "butt", 7),
Planet(8, "Neptune", "ice", 8),
Planet(9, "Pluto", "dwarf", 9)
]

Choose a reason for hiding this comment

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

I see what you did there 😆


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

@planets_bp.route("", methods = ["GET"])
def planet_data():
planet_list = []
for planet in planets:
planet_list.append({
"id" : planet.id,
"name" : planet.name,
"description" : planet.description,
"distance from sun" : planet.dist_from_sun
}
)

Choose a reason for hiding this comment

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

If the opening brackets are stacked together, I'd recommend doing the same with the closing brackets to be consistent.

Choose a reason for hiding this comment

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

To reduce repetition, we could reuse Planet's to_dict function here:

planet_list.append(planet.to_dict())

return jsonify(planet_list)

@planets_bp.route("/<planet_id>", methods = ["GET"])
def get_planet_by_id(planet_id):
planet = validate_planet(planet_id)
return planet.to_dict()


def validate_planet(planet_id):
Copy link

@kelsey-steven-ada kelsey-steven-ada Apr 27, 2022

Choose a reason for hiding this comment

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

I'd consider renaming this function. To me, validate_planet sounds like it should return a true/false based on if the planet passed as a parameter is valid. What name might better describe what the function is doing?

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

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

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

Choose a reason for hiding this comment

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

Great error handling in the helper function!