added priority by task and added filters and search tasks

parent 5b84b68d
...@@ -71,3 +71,6 @@ class TaskController(BaseController): ...@@ -71,3 +71,6 @@ class TaskController(BaseController):
except common_exc.NotFoundException as e: except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e)) raise http_exc.HTTPNotFoundException(detail=str(e))
async def search(self, keywords: str):
return await self.task_service.search_tasks(keywords)
...@@ -14,8 +14,12 @@ ctrl = TaskController() ...@@ -14,8 +14,12 @@ ctrl = TaskController()
@router.get('') @router.get('')
async def get_tasks(query: TaskBaseSchema = fastapi.Depends()): async def get_tasks(
return await ctrl.get_list(**query.model_dump(exclude_none=True)) title: str | None = None,
status: str | None = None,
priority: str | None = None
):
return await ctrl.get_list(title=title, status=status, priority=priority)
@router.get('/{id}') @router.get('/{id}')
...@@ -51,3 +55,8 @@ async def add_comment(id: UUID, body: CommentSchema): ...@@ -51,3 +55,8 @@ async def add_comment(id: UUID, body: CommentSchema):
@router.delete('/comments/{id}') @router.delete('/comments/{id}')
async def delete_comment(id: UUID): async def delete_comment(id: UUID):
return await ctrl.delete_comment(id) return await ctrl.delete_comment(id)
@router.get('/tasks/search')
async def search_tasks(keywords: str):
return await ctrl.search(keywords)
...@@ -109,3 +109,7 @@ class TaskService: ...@@ -109,3 +109,7 @@ class TaskService:
async def delete_comment(self, id: UUID): async def delete_comment(self, id: UUID):
return await self.comment_repository.delete(id) return await self.comment_repository.delete(id)
async def search_tasks(self, keywords: str):
tasks = await self.task_repository.search(keywords)
return [TaskListSchema.model_validate(task) for task in tasks]
from enum import Enum
from tortoise import fields from tortoise import fields
from .base import BaseModel from .base import BaseModel
...@@ -22,7 +24,14 @@ class Type(BaseModel): ...@@ -22,7 +24,14 @@ class Type(BaseModel):
table = 'types' table = 'types'
class PriorityEnum(str, Enum):
LOW = 'low'
MEDIUM = 'medium'
HIGH = 'high'
class Task(BaseModel): class Task(BaseModel):
title = fields.CharField(max_length=255) title = fields.CharField(max_length=255)
description = fields.TextField() description = fields.TextField()
status = fields.ForeignKeyField( status = fields.ForeignKeyField(
...@@ -33,6 +42,7 @@ class Task(BaseModel): ...@@ -33,6 +42,7 @@ class Task(BaseModel):
'models.Type', related_name='type_tasks', 'models.Type', related_name='type_tasks',
through='types_tasks' through='types_tasks'
) )
priority = fields.CharEnumField(enum_type=PriorityEnum, default=PriorityEnum.LOW)
def __str__(self): def __str__(self):
return self.title return self.title
...@@ -65,3 +75,13 @@ class Comment(BaseModel): ...@@ -65,3 +75,13 @@ class Comment(BaseModel):
class Meta: class Meta:
table = 'comments' table = 'comments'
class Category(BaseModel):
name = fields.CharField(max_length=255)
def __str__(self):
return self.name
class Meta(BaseModel.Meta):
table = 'categories'
import tortoise import tortoise
from tortoise.expressions import Q
from .models import ( from .models import (
Status, Type, Task, User, Comment Status, Type, Task, User, Comment
...@@ -56,6 +57,40 @@ class TypeRepository(BaseRepository): ...@@ -56,6 +57,40 @@ class TypeRepository(BaseRepository):
class TaskRepository(BaseRepository): class TaskRepository(BaseRepository):
model = Task model = Task
async def get_list(
self,
title: str | None = None,
status: str | None = None,
priority: str | None = None
):
query = self.model
if title is not None:
query = query.filter(title__icontains=title)
if status is not None:
query = query.filter(status__name__icontains=status)
if priority is not None:
query = query.filter(priority=priority)
return await query.all()
async def search(self, keywords: str):
query = self.model
search_terms = keywords.split()
conditions = [Q(title__icontains=term) | Q(description__icontains=term) for term in search_terms]
combined_condition = conditions.pop()
for condition in conditions:
combined_condition |= condition
query = query.filter(combined_condition)
return await query.all()
class UserRepository(BaseRepository): class UserRepository(BaseRepository):
model = User model = User
......
from tortoise import BaseDBAsyncClient
async def upgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "tasks" ADD "priority" VARCHAR(6) NOT NULL DEFAULT 'low';"""
async def downgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "tasks" DROP COLUMN "priority";"""
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