Skip to content

Final Task-List-Api- Diana S. #141

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

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8b7e331
added set up environment
sorindevops Nov 3, 2022
4eafdcf
Task Model completed
sorindevops Nov 6, 2022
55f719b
registered blueprint
sorindevops Nov 6, 2022
771a7d9
trying to understand tests and fixtures
sorindevops Nov 11, 2022
da0f53d
correct class
sorindevops Nov 11, 2022
03de79b
Able to retrive all & individual tasks
sorindevops Nov 12, 2022
70bf4c3
404 for missing ID finalized
sorindevops Nov 12, 2022
076860c
Successful: Missing title 400 bad request, need to update dict
sorindevops Nov 12, 2022
1450c18
created 404 requests from missing info
sorindevops Nov 12, 2022
90991f0
Passed 2/3 Wave 1(W/1) tests
sorindevops Nov 12, 2022
89b7815
sort queried started
sorindevops Nov 14, 2022
a4744ef
successful task added
sorindevops Nov 15, 2022
1513e96
successfully ran test wave 2 tests
sorindevops Nov 15, 2022
1d92498
successfully ran test more test 1 waves
sorindevops Nov 15, 2022
f89c2bd
successfully ran test more test 1 waves
sorindevops Nov 15, 2022
da37206
successfully ran test 1,2 waves
sorindevops Nov 15, 2022
1382671
added custom endpoints for both Patch scenarios
sorindevops Nov 16, 2022
7f5d681
added goal dict and routes
sorindevops Nov 17, 2022
ec02fee
added CRUD routes, tests are not working
sorindevops Nov 17, 2022
b862d43
review database setup
sorindevops Nov 21, 2022
f376660
updated read_one and read_all
sorindevops Nov 21, 2022
70a56d2
updated get task route
sorindevops Nov 29, 2022
0a78860
registerd init blueprint update
sorindevops Nov 29, 2022
e9252cb
updated tests
sorindevops Dec 17, 2022
1872bc6
updated models
sorindevops Jan 18, 2023
741ed60
updated models
sorindevops Jan 18, 2023
22aaa55
redo flask config
sorindevops Jan 20, 2023
5bdd38e
fixed database
sorindevops Jan 20, 2023
0353016
test wave 1
sorindevops Jan 21, 2023
8d2eae5
test wave 2
sorindevops Jan 21, 2023
c4f0bee
test wave 3
sorindevops Jan 21, 2023
dbed2de
test wave 5
sorindevops Jan 21, 2023
320dbe2
test wave 6
sorindevops Jan 21, 2023
b8b4842
final
sorindevops Jan 21, 2023
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
Empty file added .idea/.gitignore
Empty file.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/task-list-api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ def create_app(test_config=None):
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_TEST_DATABASE_URI")

# Import models here for Alembic setup
from app.models.task import Task
from app.models.goal import Goal

db.init_app(app)
migrate.init_app(app, db)

# Register Blueprints here
# Register Blueprints
from . import task_routes
from . import goal_routes

return app
app.register_blueprint(task_routes.tasks_bp)
app.register_blueprint(goal_routes.goals_bp)

return app
147 changes: 147 additions & 0 deletions app/goal_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import json
import datetime
from os import abort

from flask import Blueprint, abort, jsonify, make_response, request
from sqlalchemy import asc, desc

from app import db
from app.models.goal import Goal
from app.models.task import Task

goals_bp = Blueprint('goals', __name__, url_prefix="/goals")


def validate_goal_id(cls, goal_id):
try:
goal_id = int(goal_id)
except:
abort(make_response({"message":f"{cls.__name__} {goal_id} invalid"},400))

goal = cls.query.get(goal_id)

if not goal:
abort(make_response({"message":f"{cls.__name__} {goal_id} not found"}, 404))

return goal


# @goals_bp.route('/<goal_id>', methods=['GET'])
# def no_saved_goal(goal_id):
# goal_validate = validate_goal_id(goal_id)

# goal_response = []
# # goal.todict() for goal in Goal

# if goal_response is None:
# return None
# else:
# return jsonify({goal_validate.goal_dict()}),200

# @goals_bp.route('/<goal_id>', methods=['GET'])
# def one_saved_goal(goal_id):
# goal_validate = validate_goal_id(goal_id)

# goal_response = [goal.todict() for goal in Goal]

# if goal_id == None:
# return None
# else:
# return jsonify({goal_validate.goal_dict()}),

@goals_bp.route('/<goal_id>', methods=['GET'])
def get_goal(goal_id):
goal_validate = validate_goal_id(Goal,goal_id)


return jsonify({"goal": goal_validate.goal_dict()}), 200

@goals_bp.route("", methods=['POST'])
def create_goal():
response_body = request.get_json()

if "title" not in response_body:
return jsonify({"details": "Invalid data"}), 400

created_goal = Goal(title=response_body["title"])

# new_goal.goal = Goal

db.session.add(created_goal)
db.session.commit()

return jsonify({"goal": created_goal.goal_dict()}), 201


@goals_bp.route('', methods=['GET'])
def query_all():

sort_query = request.args.get("sort")

query_lists = []

if sort_query== "desc":
query_goals = Goal.query.order_by(Goal.title.desc())


elif sort_query == "asc":
query_goals = Goal.query.order_by(Goal.title.asc())

else:
query_goals = Goal.query.all()

for query in query_goals:
query_lists.append(query.goal_dict())

return jsonify(query_lists), 200



@goals_bp.route('/<goal_id>', methods=['PUT'])
def update_goals(goal_id):

goal_object = validate_goal_id(Goal,goal_id)

response_body = request.get_json()

if "title" in response_body:
goal_object.title = response_body["title"]

db.session.commit()

return jsonify(goal_object.goal_dict()), 200


@goals_bp.route('/<goal_id>', methods=['DELETE'])
def delete_goals(goal_id):
test_goal = validate_goal_id(Goal,goal_id)
result_notice = {"details": f'Goal {goal_id} "{test_goal.title}" successfully deleted'}

db.session.delete(test_goal)
db.session.commit()

return make_response(result_notice, 200)

@goals_bp.route('/<goal_id>/tasks', methods=['POST'])
def task_ids_to_goal(goal_id):
goal_object = validate_goal_id(Goal,goal_id)

request_body = request.get_json()

goal_object.tasks = [Task.query.get(task_id) for task_id in request_body["task_ids"]]
db.session.commit()

return {"id":goal_object.goal_id,
"task_ids":[task.task_id for task in goal_object.tasks]}, 200


@goals_bp.route('/<goal_id>/tasks', methods=['GET'])
def goal_with_no_task(goal_id):
goal_object = validate_goal_id(Goal,goal_id)

# request_body = request.get_json()

tasks = [task.build_task_dict() for task in goal_object.tasks]

return {"id": goal_object.goal_id, "title": goal_object.title,
"tasks": tasks}, 200
16 changes: 15 additions & 1 deletion app/models/goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@


class Goal(db.Model):
goal_id = db.Column(db.Integer, primary_key=True)
goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(80))
tasks = db.relationship("Task", back_populates="goal", lazy=True)

def goal_dict(self):
return {
"id": self.goal_id,
"title": self.title}


@classmethod
def from_dict(cls, goal_data):
goal_class = cls(title=goal_data["title"])
return goal_class

26 changes: 24 additions & 2 deletions app/models/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
from app import db

from datetime import datetime

class Task(db.Model):
task_id = db.Column(db.Integer, primary_key=True)
task_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(80))
description = db.Column(db.String(80))
completed_at = db.Column(db.DateTime, nullable=True)
goal = db.relationship("Goal", back_populates="tasks")
goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id'),nullable=True)

def build_task_dict(self):
return {
"id": self.task_id,
"title": self.title,
"description": self.description,
"is_complete": bool(self.completed_at),
"goal_id": self.goal_id
}

@classmethod
def from_dict(cls, task_data):
new_Task = cls(title=task_data["title"],
description=task_data["description"],
is_complete=task_data["completed_at"],
completed_at=task_data.get("is_complete", None),)
return new_Task
1 change: 0 additions & 1 deletion app/routes.py

This file was deleted.

Loading