fixed create and update tasks

parent e5691170
......@@ -21,12 +21,14 @@ class TaskListSchema(TaskBaseSchema, TaskIdSchema):
class TaskPostSchema(TaskBaseSchema):
description: str
status: UUID | None = None
category: UUID | None = None
types: List[UUID] | None = None
class TaskPatchSchema(TaskBaseSchema):
description: str | None = None
status: UUID | None
status: UUID | None = None
category: UUID | None = None
types: List[UUID] | None = None
......
......@@ -2,7 +2,7 @@ from uuid import UUID
from db.repositories import (
TaskRepository, StatusRepository, CommentRepository,
UserRepository
UserRepository, CategoryRepository
)
from exceptions import common as common_exc, http as http_exc
from db.models import Type, Status
......@@ -14,6 +14,7 @@ class TaskService:
status_repository = StatusRepository()
comment_repository = CommentRepository()
user_repository = UserRepository()
category_repository = CategoryRepository()
async def get_task_list(self, **kwargs):
tasks = await self.task_repository.get_list(**kwargs)
......@@ -21,9 +22,10 @@ class TaskService:
async def get_task(self, id: UUID):
task = await self.task_repository.get(id=id)
await task.fetch_related('status', 'types')
await task.fetch_related('status', 'category', 'types')
status_id = task.status.id if task.status else None
category_id = task.category.id if task.category else None
types_data = [type.id for type in task.types]
task_data = {
......@@ -31,6 +33,7 @@ class TaskService:
"title": task.title,
"description": task.description,
"status": status_id,
"category": category_id,
"types": types_data
}
......@@ -38,20 +41,24 @@ class TaskService:
async def create_task(self, **kwargs):
status_uuid = kwargs.get('status')
category_uuid = kwargs.get('category')
type_uuids = kwargs.get('types', [])
if status_uuid is None:
raise http_exc.HTTPBadRequestException(detail="Validation error: status: Value must not be None")
if status_uuid is None or category_uuid is None:
raise http_exc.HTTPBadRequestException(detail="Validation error: Value must not be None")
try:
status = await self.status_repository.get(status_uuid)
category = await self.category_repository.get(category_uuid)
except common_exc.NotFoundException:
raise http_exc.HTTPBadRequestException(detail=f"status does not exist")
except common_exc.NotFoundException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
task = await self.task_repository.create(
title=kwargs.get('title'),
description=kwargs.get('description'),
status=status
status=status,
category=category
)
types = await Type.filter(id__in=type_uuids)
......@@ -61,10 +68,19 @@ class TaskService:
async def update_task(self, id: UUID, **kwargs):
status_uuid = kwargs.get('status')
category_uuid = kwargs.get('category')
type_uuids = kwargs.get('types', [])
if status_uuid:
try:
kwargs['status'] = await self.status_repository.get(status_uuid)
except common_exc.NotFoundException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
if category_uuid:
try:
kwargs['category'] = await self.category_repository.get(category_uuid)
except common_exc.NotFoundException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
types = kwargs.pop('types', [])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment