-
Notifications
You must be signed in to change notification settings - Fork 127
Angela Amethyst A19 #130
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
base: main
Are you sure you want to change the base?
Angela Amethyst A19 #130
Conversation
@@ -3,3 +3,13 @@ | |||
|
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.
Great job completing Task List Angela! Your code looks clean and easily readable. I want to point out the good variable naming and your code is DRY. Great use of helper methods in your models! Excellent work using blue prints & creating RESTful CRUD routes for each model.
def to_dict(self): | ||
goal_as_dict = { | ||
"id": self.goal_id, | ||
"title": self.title | ||
} | ||
|
||
return goal_as_dict |
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.
Great job creating this reusable
helper function`
goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id'), nullable=True) | ||
goal = db.relationship("Goal", back_populates="tasks") |
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 approach creating this relationship to your Goal model. Why not use lazy here?
|
||
return task_as_dict | ||
|
||
|
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.
Looks like there's some extra whitespace here
from flask import Flask, Blueprint, jsonify, abort, make_response, request |
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 imports help a great deal when it comes to our request & response cycle
tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks") | ||
|
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.
👍🏾
try: | ||
model_id = int(model_id) | ||
except: | ||
abort(make_response({"message": f"{model_id} is not a valid type ({type(model_id)}). Must be an integer)"}, 400)) | ||
|
||
model = cls.query.get(model_id) | ||
|
||
if not model: | ||
abort(make_response({"message": f"{cls.__name__} {model_id} does not exist"}, 404)) | ||
|
||
return model |
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 creating this reusable function & keeping your code DRY. Nice approach to handling an invalid model 😁
### | ||
|
||
@tasks_bp.route("", methods=['POST']) |
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.
🎉
new_task.completed_at=request_body["completed_at"] | ||
|
||
db.session.add(new_task) |
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.
Great job using db.session.<method-name>
. Session gives us access to the follow:
db
is our app’s instance of SQLAlchemy.session
represents our active database connection.- By referencing
db.session
we can use SQLAlchemy’s methods to perform tasks like:- committing a change to a model
- storing a new record of a model
- deleting a record.
- By referencing
db.session.add()
you are able to use the SQLAlchemy method to store a new record of the task model
task = validate_model(Task, task_id) | ||
if task not in goal.tasks: | ||
goal.tasks.append(task) | ||
|
||
|
||
task_ids=[] | ||
for task in goal.tasks: | ||
task_ids.append(task.task_id) | ||
|
||
db.session.commit() |
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.
✅
@@ -4,19 +4,20 @@ | |||
import os | |||
from dotenv import load_dotenv |
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.
Great work! We need to import load_dotenv to set up environment variables in our .env file
db = SQLAlchemy() | ||
migrate = Migrate() | ||
load_dotenv() |
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.
🎉
@@ -4,19 +4,20 @@ | |||
import os | |||
from dotenv import load_dotenv | |||
|
|||
|
|||
db = SQLAlchemy() |
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.
Do you remember what this does?
app = Flask(__name__) | ||
|
||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | ||
|
||
if test_config is None: | ||
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( |
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.
Why do we need this line?
@@ -30,5 +31,9 @@ def create_app(test_config=None): | |||
migrate.init_app(app, db) |
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.
Great job! We need to pass our instance of our app & the instance of our SQLALchemy db to connect the db and migrate to our Flask app
# Register Blueprints here | ||
from .routes import goals_bp | ||
app.register_blueprint(goals_bp) | ||
from .routes import tasks_bp | ||
app.register_blueprint(tasks_bp) |
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.
Great work registering these blueprints ✅
assert response.status_code == 404 | ||
assert "message" in response_body | ||
assert response_body["message"] == "Task 1 does not exist" |
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 with this test completion! This assertion is a great way to validate functionality is working as expected
"goal": new_goal.to_dict() | ||
}, 201 |
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.
Usually a resource creation relates to a 201 response code 👍🏾
No description provided.