fixed create and update tasks

parent e5691170
...@@ -21,12 +21,14 @@ class TaskListSchema(TaskBaseSchema, TaskIdSchema): ...@@ -21,12 +21,14 @@ class TaskListSchema(TaskBaseSchema, TaskIdSchema):
class TaskPostSchema(TaskBaseSchema): class TaskPostSchema(TaskBaseSchema):
description: str description: str
status: UUID | None = None status: UUID | None = None
category: UUID | None = None
types: List[UUID] | None = None types: List[UUID] | None = None
class TaskPatchSchema(TaskBaseSchema): class TaskPatchSchema(TaskBaseSchema):
description: str | None = None description: str | None = None
status: UUID | None status: UUID | None = None
category: UUID | None = None
types: List[UUID] | None = None types: List[UUID] | None = None
......
...@@ -2,7 +2,7 @@ from uuid import UUID ...@@ -2,7 +2,7 @@ from uuid import UUID
from db.repositories import ( from db.repositories import (
TaskRepository, StatusRepository, CommentRepository, TaskRepository, StatusRepository, CommentRepository,
UserRepository UserRepository, CategoryRepository
) )
from exceptions import common as common_exc, http as http_exc from exceptions import common as common_exc, http as http_exc
from db.models import Type, Status from db.models import Type, Status
...@@ -14,6 +14,7 @@ class TaskService: ...@@ -14,6 +14,7 @@ class TaskService:
status_repository = StatusRepository() status_repository = StatusRepository()
comment_repository = CommentRepository() comment_repository = CommentRepository()
user_repository = UserRepository() user_repository = UserRepository()
category_repository = CategoryRepository()
async def get_task_list(self, **kwargs): async def get_task_list(self, **kwargs):
tasks = await self.task_repository.get_list(**kwargs) tasks = await self.task_repository.get_list(**kwargs)
...@@ -21,9 +22,10 @@ class TaskService: ...@@ -21,9 +22,10 @@ class TaskService:
async def get_task(self, id: UUID): async def get_task(self, id: UUID):
task = await self.task_repository.get(id=id) 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 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] types_data = [type.id for type in task.types]
task_data = { task_data = {
...@@ -31,6 +33,7 @@ class TaskService: ...@@ -31,6 +33,7 @@ class TaskService:
"title": task.title, "title": task.title,
"description": task.description, "description": task.description,
"status": status_id, "status": status_id,
"category": category_id,
"types": types_data "types": types_data
} }
...@@ -38,20 +41,24 @@ class TaskService: ...@@ -38,20 +41,24 @@ class TaskService:
async def create_task(self, **kwargs): async def create_task(self, **kwargs):
status_uuid = kwargs.get('status') status_uuid = kwargs.get('status')
category_uuid = kwargs.get('category')
type_uuids = kwargs.get('types', []) type_uuids = kwargs.get('types', [])
if status_uuid is None: if status_uuid is None or category_uuid is None:
raise http_exc.HTTPBadRequestException(detail="Validation error: status: Value must not be None") raise http_exc.HTTPBadRequestException(detail="Validation error: Value must not be None")
try: try:
status = await self.status_repository.get(status_uuid) status = await self.status_repository.get(status_uuid)
category = await self.category_repository.get(category_uuid)
except common_exc.NotFoundException: except common_exc.NotFoundException as e:
raise http_exc.HTTPBadRequestException(detail=f"status does not exist") raise http_exc.HTTPBadRequestException(detail=str(e))
task = await self.task_repository.create( task = await self.task_repository.create(
title=kwargs.get('title'), title=kwargs.get('title'),
description=kwargs.get('description'), description=kwargs.get('description'),
status=status status=status,
category=category
) )
types = await Type.filter(id__in=type_uuids) types = await Type.filter(id__in=type_uuids)
...@@ -61,10 +68,19 @@ class TaskService: ...@@ -61,10 +68,19 @@ class TaskService:
async def update_task(self, id: UUID, **kwargs): async def update_task(self, id: UUID, **kwargs):
status_uuid = kwargs.get('status') status_uuid = kwargs.get('status')
category_uuid = kwargs.get('category')
type_uuids = kwargs.get('types', []) type_uuids = kwargs.get('types', [])
if status_uuid: if status_uuid:
try:
kwargs['status'] = await self.status_repository.get(status_uuid) 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', []) 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