Skip to content

Commit 3aca89f

Browse files
committed
have gotten all tests up until the final two tests in wave 6 complete. Currently working on getting correct response body for the final two tests in wave 6
1 parent ce8edd8 commit 3aca89f

File tree

6 files changed

+96
-17
lines changed

6 files changed

+96
-17
lines changed

app/models/goal.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1-
from sqlalchemy.orm import Mapped, mapped_column
2-
from ..db import db
1+
from sqlalchemy.orm import Mapped, mapped_column, relationship
32
from datetime import datetime
43
from app.routes.utilities_routes import create_model, validate_model, check_for_completion
54
from typing import Optional
5+
from sqlalchemy import ForeignKey
6+
# from app.models.task import Task
7+
from ..db import db
68

79
class Goal(db.Model):
810
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
911
title: Mapped[str]
12+
tasks: Mapped[list["Task"]] = relationship("Task",back_populates="goal", lazy=True)
1013
# description=Mapped[str]
1114
# completed_at: Mapped[Optional[datetime]]=mapped_column(nullable = True)
1215

1316

1417
def to_dict(self):
15-
task_as_dict = {}
16-
task_as_dict["id"] = self.id
17-
task_as_dict["title"] = self.title
18+
goal_as_dict = {}
19+
goal_as_dict["id"] = self.id
20+
goal_as_dict["title"] = self.title
21+
if self.tasks:
22+
task_ids=[]
23+
task_dictionaries = [task.to_dict() for task in self.tasks]
24+
for task in task_dictionaries:
25+
task_id = task.get("id")
26+
task_ids.append(task_id)
27+
goal_as_dict["task_ids"] = task_ids
28+
else:
29+
goal_as_dict["task_ids"] = []
1830
# task_as_dict["description"] = self.description
1931
# task_as_dict["is_complete"] = check_for_completion(Goal,self)
2032

21-
return task_as_dict
33+
return goal_as_dict
2234

2335

2436

2537
@classmethod
2638
def from_dict(cls, goal_data):
27-
new_task = cls(
39+
new_goal = cls(
2840
title=goal_data["title"],
2941
# description=goal_data["description"],
3042
# completed_at=goal_data["completed_at"]
3143
)
32-
return new_task
44+
return new_goal

app/models/task.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1-
from sqlalchemy.orm import Mapped, mapped_column
1+
from sqlalchemy.orm import Mapped, mapped_column, relationship
22
from ..db import db
33
from datetime import datetime
44
from app.routes.utilities_routes import create_model, validate_model, check_for_completion
55
from typing import Optional
6+
from sqlalchemy import ForeignKey
67

78
class Task(db.Model):
89
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
910
title: Mapped[str]
1011
description: Mapped[str]
1112
completed_at: Mapped[Optional[datetime]]= mapped_column(nullable = True)
13+
goal_id: Mapped[Optional[int]] = mapped_column(ForeignKey("goal.id"), nullable=True)
14+
goal: Mapped["Goal"] = relationship("Goal", back_populates="tasks")
1215

1316
def to_dict(self):
1417
task_as_dict = {}
1518
task_as_dict["id"] = self.id
1619
task_as_dict["title"] = self.title
1720
task_as_dict["description"] = self.description
1821
task_as_dict["is_complete"] = check_for_completion(Task,self)
22+
if self.goal_id:
23+
task_as_dict["goal"] = self.goal_id
1924

2025
return task_as_dict
2126

2227

2328
@classmethod
2429
def from_dict(cls, task_data):
30+
goal_id = task_data.get("goal_id")
31+
2532
new_task = cls(
2633
title=task_data["title"],
2734
description=task_data["description"],
35+
goal_id = goal_id
2836
)
2937
return new_task

app/routes/goal_routes.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,48 @@
11
from flask import Blueprint, abort, make_response, request, Response
2+
3+
from app.models.goal import Goal
24
from app.models.task import Task
35
from ..db import db
46
from datetime import datetime
57
from app.routes.utilities_routes import create_model, validate_model, get_models_with_filters, check_for_completion, delete_model
6-
from app.models.goal import Goal
8+
79
import requests
810

911
goals_bp = Blueprint("goals_bp",__name__, url_prefix= "/goals")
1012

1113

14+
####################################################################
15+
######################### Create FUNCTIONS #########################
16+
####################################################################
1217
@goals_bp.post("")
1318
def create_goal():
1419
request_body = request.get_json()
1520
return create_model(Goal,request_body)
1621

22+
@goals_bp.post("/<goal_id>/tasks")
23+
def post_task_ids_to_goal(goal_id):
24+
request_body = request.get_json()
25+
goal = validate_model(Goal, goal_id)
26+
27+
task_ids = request_body.get("task_ids", [])
28+
for task_id in task_ids:
29+
task = validate_model(Task, task_id)
30+
if task:
31+
goal.tasks.append(task)
32+
33+
db.session.commit()
34+
goal = goal.to_dict()
35+
response_body = {
36+
"id": goal.get("id"),
37+
"task_ids": goal.get("task_ids")
38+
}
39+
40+
return response_body, 200
41+
42+
43+
####################################################################
44+
######################### READ FUNCTIONS #########################
45+
####################################################################
1746
@goals_bp.get("")
1847
def get_goals():
1948
request_arguements = request.args
@@ -25,6 +54,24 @@ def get_one_goal(goal_id):
2554
response = {"goal": goal.to_dict()}
2655
return make_response(response, 200)
2756

57+
@goals_bp.get("/<goal_id>/tasks")
58+
def get_tasks_for_specific_goal(goal_id):
59+
goal = validate_model(Goal, goal_id)
60+
goal_as_dict = goal.to_dict()
61+
print(goal_as_dict)
62+
request_arguement = {"Goal_Id": goal_id}
63+
filtered_result_body= get_models_with_filters(Task, request_arguement)
64+
65+
response_body = {
66+
"id": goal_as_dict.get("id"),
67+
"title": goal_as_dict.get("title"),
68+
"tasks": (filtered_result_body)}
69+
70+
return response_body, 200
71+
72+
####################################################################
73+
######################### UPDATE FUNCTIONS #########################
74+
####################################################################
2875
@goals_bp.put("/<goal_id>")
2976
def update_goal(goal_id):
3077
goal = validate_model(Goal, goal_id)
@@ -36,7 +83,11 @@ def update_goal(goal_id):
3683
response_body = {"message": f"Goal #{goal_id} succesfully updated"}
3784
return make_response(response_body, 200)
3885

86+
87+
####################################################################
88+
######################### DELETE FUNCTIONS #########################
89+
####################################################################
3990
@goals_bp.delete("/<goal_id>")
4091
def delete_goal(goal_id):
4192
goal = validate_model(Goal, goal_id)
42-
return delete_model(Goal, goal)
93+
return delete_model(Goal, goal)

tests/test_wave_03.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_mark_complete_missing_task(client):
127127

128128
# Assert
129129
assert response.status_code == 404
130-
assert response_body == {"details": f"task 1 not found"}
130+
assert response_body == {"error": f"Task 1 not found"}
131131

132132
## @pytest.mark.skip(reason="No way to test this feature yet")
133133
def test_mark_incomplete_missing_task(client):
@@ -137,4 +137,4 @@ def test_mark_incomplete_missing_task(client):
137137

138138
# Assert
139139
assert response.status_code == 404
140-
assert response_body == {"details": f"task 1 not found"}
140+
assert response_body == {"error": f"Task 1 not found"}

tests/test_wave_05.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def test_get_goals_one_saved_goal(client, one_goal):
2424
assert response_body == [
2525
{
2626
"id": 1,
27-
"title": "Build a habit of going outside daily"
27+
"title": "Build a habit of going outside daily",
28+
"task_ids": []
2829
}
2930
]
3031

@@ -41,7 +42,8 @@ def test_get_goal(client, one_goal):
4142
assert response_body == {
4243
"goal": {
4344
"id": 1,
44-
"title": "Build a habit of going outside daily"
45+
"title": "Build a habit of going outside daily",
46+
"task_ids": []
4547
}
4648
}
4749

@@ -54,7 +56,7 @@ def test_get_goal_not_found(client):
5456
response_body = response.get_json()
5557

5658
assert response.status_code == 404
57-
assert response_body == {"message": "Goal 1 not found"}
59+
assert response_body == {"error": "Goal 1 not found"}
5860

5961

6062
# @pytest.mark.skip(reason="No way to test this feature yet")
@@ -71,7 +73,8 @@ def test_create_goal(client):
7173
assert response_body == {
7274
"goal": {
7375
"id": 1,
74-
"title": "My New Goal"
76+
"title": "My New Goal",
77+
"task_ids": []
7578
}
7679
}
7780

tests/test_wave_06.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ def test_get_task_includes_goal_id(client, one_task_belongs_to_one_goal):
121121
print({
122122
"task": {
123123
"id": 1,
124+
<<<<<<< HEAD
124125
"goal_id": 1,m
125126
"title": "Go on y daily walk 🏞",
127+
=======
128+
"goal_id": 1,
129+
"title": "Go on my daily walk 🏞",
130+
>>>>>>> a3849fc (have gotten all tests up until the final two tests in wave 6 complete. Currently working on getting correct response body for the final two tests in wave 6)
126131
"description": "Notice something new every day",
127132
"is_complete": False
128133
}

0 commit comments

Comments
 (0)