-
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 - Sana Pournaghshband and Camilla #20
base: main
Are you sure you want to change the base?
Conversation
app/routes/planet_routes.py
Outdated
try: | ||
id = int(id) | ||
except: | ||
return abort(make_response({"message": f"planet {id} is invalid"}, 400)) | ||
|
||
for planet in planets: | ||
if planet.id == id: | ||
return planet | ||
|
||
return abort(make_response({"message": f"planet {id} is not found"}, 404)) |
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 👍🏽
|
||
#Get all planets | ||
@planet_bp.route("", methods=["GET"]) | ||
def read_all_planets(): |
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.
Nice job naming the method to describe what this route does.
Since you put your routes in planet_routs.py in routes directory, you can even call this read_all() since we know that all the routes in planet_routes.py are related to the Planet class.
for planet in planets: | ||
planets_response.append(planet.to_json()) | ||
|
||
return jsonify(planets_response) |
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.
Adding 200 status code adds clarity even though it happens by default
return jsonify(planets_response), 200
def update(self, request_body): | ||
self.title = request_body["title"] | ||
self.description = request_body["description"] | ||
self.moons = request_body["moons"] | ||
|
||
@classmethod | ||
def create(cls,request_body): | ||
new_planet = cls( | ||
title=request_body["title"], | ||
description=request_body["description"], | ||
moons = request_body["moons"] | ||
) | ||
return new_planet |
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.
Nice work pulling these two methods into the model Planet to keep your routes short and sweet.
This comment applies to your create() and update() methods:
How would you add in validation to make sure that all the required fields are sent in the request to your server?
For example, if someone sent a POST request but left off moons, then line 33 would throw KeyError that it couldn't find moons.
it would be nice to handle the error and return a message so that the client knows their request was invalid and they need to include moons. Something to think about for Task List.
app/routes/helpers.py
Outdated
from flask import Blueprint, jsonify, make_response, abort, request | ||
from app.models.planet import Planet | ||
|
||
def validate_planet(id): |
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.
In the future, you might need to make 'helper.py' more specific. Think about Task List, you have Goal and Task, you may need helpers for each. If you refactor helper methods into a helper file then you could have goal_helper.py and task_helper.py
@@ -0,0 +1,89 @@ | |||
from flask import jsonify, make_response |
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.
These tests look good to me 👍
No description provided.