Skip to content
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

[ITG-44] Implementacja endpointu GET do pobrania zadań będących w relacji z danym zadaniem #22

Merged
merged 12 commits into from
Apr 27, 2022
2 changes: 1 addition & 1 deletion bode/bode/models/task_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ def delete(relation_id):

def __repr__(self):
return f"""<TaskRelation
{self.first_task_id} <{self.relationship}> {self.second_task_id}
{self.first_task_id} <{self.type}> {self.second_task_id}
>"""
16 changes: 16 additions & 0 deletions bode/bode/resources/task_relations/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from sqlite3 import DataError

from flask.views import MethodView
from flask_smorest import Blueprint, abort
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound

from bode.models.task_relation import TaskRelation
from bode.resources.task_relations.schemas import (
SimpleTaskRelationSchema,
TaskRelationInputSchema,
)
from bode.resources.tasks.schemas import TasksRelationSchema

blueprint = Blueprint("task-relations", "task-relations", url_prefix="/task-relations")

Expand All @@ -27,3 +31,15 @@ class TasksRelationsById(MethodView):
@blueprint.response(200, SimpleTaskRelationSchema)
def delete(self, relation_id):
return TaskRelation.delete(relation_id)


@blueprint.route("/<task_id>")
class TasksInRelationWith(MethodView):
@blueprint.response(200, TasksRelationSchema(many=True))
def get(self, task_id):
try:
return TaskRelation.query.filter(TaskRelation.first_task_id == task_id).all() or abort(404)
except DataError:
abort(404)
except NoResultFound:
abort(404, message="Item not found.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting only the relations record is not really useful. When using this API route you'd want to have some data about the related tasks in the response, like title, due_date, etc. The response scheme should more or less look like this:

class RelatedTaskSchema(BaseSchema):
    id = fields.UUID(dump_only=True) # relation id
    type = fields.String() # relation type
    related_task = fields.nested(TaskSchema) # the task object that is related that is in relation with the given task

Also, you don't need to catch these errors when returning lists. A response with an empty list of related tasks is not exceptional and should return 200 response code.

7 changes: 7 additions & 0 deletions bode/bode/resources/tasks/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ class TaskSchema(BaseSchema):
description = fields.String()
due_date = fields.DateTime()
is_done = fields.Boolean()


class TasksRelationSchema(BaseSchema):
id = fields.UUID(dump_only=True)
first_task_id = fields.String()
second_task_id = fields.String()
type = fields.String()
koszar91 marked this conversation as resolved.
Show resolved Hide resolved
koszar91 marked this conversation as resolved.
Show resolved Hide resolved