-
Notifications
You must be signed in to change notification settings - Fork 111
first commit #115
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: master
Are you sure you want to change the base?
first commit #115
Changes from all commits
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 |
---|---|---|
|
@@ -126,6 +126,7 @@ venv.bak/ | |
# mkdocs documentation | ||
/site | ||
|
||
play.py | ||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: gunicorn 'app:create_app()' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
from app.models.goal import Goal | ||
from app.models.task import Task | ||
from flask import Blueprint, jsonify, abort, make_response,request | ||
from app import db | ||
from datetime import datetime, date, time, timezone | ||
import requests | ||
|
||
goals_bp = Blueprint("goals_bp", __name__, url_prefix="/goals") | ||
|
||
# Helper Function | ||
def validate_goal(id): | ||
try: | ||
goal_id = int(id) | ||
except ValueError: | ||
abort(make_response(jsonify(dict(details=f"Invalid id {goal_id}")), 400)) | ||
|
||
|
||
goal = Goal.query.get(id) | ||
if goal: | ||
return goal | ||
|
||
abort(make_response(jsonify(dict(details=f"No goal with id {goal_id} found")), 404)) | ||
|
||
# CREATE NEW TASK | ||
@goals_bp.route("", methods=[ "POST"]) | ||
def create_goal(): | ||
request_body = request.get_json() | ||
|
||
if request_body == {}: | ||
|
||
response_body = { | ||
"details": "Invalid data" | ||
}, 400 | ||
|
||
return response_body | ||
|
||
|
||
new_goal = Goal( | ||
title=request_body["title"]) | ||
|
||
|
||
db.session.add(new_goal) | ||
db.session.commit() | ||
|
||
response_body = { "goal": | ||
{ | ||
"id": new_goal.goal_id, | ||
"title": new_goal.title | ||
} | ||
}, 201 | ||
Comment on lines
+45
to
+50
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. How can you create an instance method or helper function so you don't have to repeat this code ? 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. I can create an instance method but am unclear as to how to incorporate it in this function |
||
|
||
return response_body | ||
|
||
|
||
@goals_bp.route("/<goal_id>", methods=["GET"]) | ||
# return dictionary with "goal" as key and the value is a nested dictionary | ||
|
||
def get_task_by_id(goal_id): | ||
goal = validate_goal(goal_id) | ||
|
||
if goal == None: | ||
return [] | ||
response_body = { "goal": | ||
{ | ||
"id": goal.goal_id, | ||
"title": goal.title, | ||
} | ||
} | ||
|
||
return response_body | ||
|
||
# GET ALL ROUTE | ||
@goals_bp.route("", methods=["GET"]) | ||
def get_all_goals(): | ||
|
||
goals = Goal.query.all() | ||
|
||
if goals == None: | ||
return [] | ||
else: | ||
response_body = [] | ||
for goal in goals: | ||
response_body.append({ | ||
"id": goal.goal_id, | ||
"title": goal.title, | ||
}) | ||
|
||
return jsonify(response_body) | ||
|
||
# # Update ONE Goal | ||
@goals_bp.route("/<goal_id>", methods=["PUT"]) | ||
|
||
def update_task_by_id(goal_id): | ||
|
||
goal = validate_goal(goal_id) | ||
request_body = request.get_json() | ||
|
||
goal.title = request_body["title"] | ||
|
||
|
||
response_body = { "goal": | ||
{ | ||
"id": goal.goal_id, | ||
"title": goal.title, | ||
} | ||
} | ||
|
||
db.session.commit() | ||
|
||
return response_body | ||
|
||
|
||
@goals_bp.route("/<goal_id>", methods=["DELETE"]) | ||
|
||
def delete_goal_by_id(goal_id): | ||
goal = validate_goal(goal_id) | ||
|
||
db.session.delete(goal) | ||
db.session.commit() | ||
|
||
response_body = { | ||
"details": | ||
f"Goal {goal.goal_id} \ {goal.title} \" successfully deleted" | ||
} | ||
|
||
return response_body | ||
|
||
|
||
# NESTED ROUTE CREATE BY GOAL AND TASK | ||
# CREATE A GOAL THATS LINKED TO A TASK | ||
# USE THE GOALS bp | ||
|
||
@goals_bp.route("/<goal_id>/tasks", methods=["POST"]) | ||
|
||
def post_tasks_to_goal(goal_id): | ||
goal = validate_goal(goal_id) | ||
request_body = request.get_json() | ||
|
||
for task_id in request_body["task_ids"]: | ||
linked_task = Task.query.get(task_id) | ||
linked_task.goal_id = goal_id | ||
|
||
db.session.commit() | ||
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. this should be moved to line 149 |
||
|
||
|
||
linked_task_ids = [] | ||
for task in goal.tasks: | ||
linked_task_ids.append(task.task_id) | ||
|
||
return {"id":goal.goal_id, "task_ids":linked_task_ids}, 200 | ||
|
||
|
||
@goals_bp.route("/<goal_id>/tasks", methods=["GET"]) | ||
def get_tasks_one_goal(goal_id): | ||
|
||
goal = validate_goal(goal_id) | ||
response = { | ||
"id": goal.goal_id, | ||
"title": goal.title, | ||
"tasks": [] | ||
} | ||
# tasks_response = [] | ||
for task in goal.tasks: | ||
response["tasks"].append( | ||
|
||
|
||
{ | ||
"id": task.task_id, | ||
"goal_id": goal.goal_id, | ||
"title": task.title, | ||
"description": task.description, | ||
"is_complete": True if task.completed_at else False | ||
} | ||
|
||
) | ||
return jsonify(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.
like that you have specific error messages so the user knows exactly wants going on!