added pagination by task list

parent da8cb704
This diff is collapsed.
......@@ -12,6 +12,7 @@ uvicorn = "^0.24.0.post1"
tortoise-orm = {extras = ["asyncodbc"], version = "^0.20.0"}
aerich = "^0.7.2"
asyncpg = "^0.29.0"
fastapi-pagination = "^0.12.12"
......
from uuid import UUID
import fastapi
from fastapi_pagination import Page, paginate
from .schemas import (
TaskBaseSchema, TaskPostSchema, TaskPatchSchema,
......@@ -18,14 +19,21 @@ async def get_tasks(
title: str | None = None,
status: str | None = None,
category: str | None = None,
priority: str | None = None
priority: str | None = None,
sort_by: str | None = None,
page: int = 1,
size: int = 10,
):
return await ctrl.get_list(
title=title, status=status,
category=category, priority=priority
title=title,
status=status,
category=category,
priority=priority,
sort_by=sort_by,
page=page,
size=size,
)
@router.get('/{id}')
async def get_task(id: UUID):
return await ctrl.get(id)
......
......@@ -10,7 +10,6 @@ class TaskBaseSchema(BaseModel):
class TaskIdSchema(BaseModel):
id: UUID
model_config = ConfigDict(from_attributes=True)
......
......@@ -6,7 +6,7 @@ from db.repositories import (
)
from exceptions import common as common_exc, http as http_exc
from db.models import Type, Status
from .schemas import TaskListSchema, CommentSchema
from .schemas import TaskListSchema
class TaskService:
......@@ -18,7 +18,12 @@ class TaskService:
async def get_task_list(self, **kwargs):
tasks = await self.task_repository.get_list(**kwargs)
return [TaskListSchema.model_validate(task) for task in tasks]
return {
"page": tasks['page'],
"size": tasks['size'],
"total": tasks['total'],
"result": [TaskListSchema.model_validate(task) for task in tasks['result']]
}
async def get_task(self, id: UUID):
task = await self.task_repository.get(id=id)
......
import math
import tortoise
from tortoise.expressions import Q
......@@ -62,9 +64,15 @@ class TaskRepository(BaseRepository):
title: str | None = None,
status: str | None = None,
category: str | None = None,
priority: str | None = None
priority: str | None = None,
sort_by: str | None = None,
page: int | None = None,
size: int | None = None
):
query = self.model
query = self.model.all()
offset_min = (page - 1) * size
offset_max = page * size
if title is not None:
query = query.filter(title__icontains=title)
......@@ -78,9 +86,20 @@ class TaskRepository(BaseRepository):
if priority is not None:
query = query.filter(priority=priority)
return await query.all()
if sort_by is not None:
query = query.order_by(sort_by)
data = await query
result = {
"page": page,
"size": size,
"total": math.ceil(len(data) / size) - 1,
"result": data[offset_min:offset_max]
}
return result
async def search(self, keywords: str):
async def search(self, keywords: str):
query = self.model
search_terms = keywords.split()
......
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