feat: pagination and query params for a deal end-point
This commit is contained in:
@ -2,20 +2,35 @@ from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from models import Deal, CardStatusHistory
|
||||
from models import Deal, CardStatusHistory, Board
|
||||
from repositories.base import BaseRepository
|
||||
from schemas.deal import UpdateDealSchema, CreateDealSchema
|
||||
|
||||
|
||||
class DealRepository(BaseRepository):
|
||||
async def get_all(self, board_id: int) -> list[Deal]:
|
||||
stmt = (
|
||||
select(Deal)
|
||||
.where(Deal.is_deleted.is_(False), Deal.board_id == board_id)
|
||||
.order_by(Deal.lexorank)
|
||||
)
|
||||
async def get_all(
|
||||
self,
|
||||
board_id: Optional[int],
|
||||
project_id: Optional[int],
|
||||
page: Optional[int],
|
||||
items_per_page: Optional[int],
|
||||
) -> tuple[list[Deal], int]:
|
||||
stmt = select(Deal).where(Deal.is_deleted.is_(False))
|
||||
|
||||
if board_id:
|
||||
stmt = stmt.where(Deal.board_id == board_id)
|
||||
if project_id:
|
||||
stmt = stmt.join(Board).where(Board.project_id == project_id)
|
||||
|
||||
total_items = len((await self.session.execute(stmt)).all())
|
||||
|
||||
stmt = stmt.order_by(Deal.lexorank)
|
||||
|
||||
if page and items_per_page:
|
||||
stmt = self._apply_pagination(stmt, page, items_per_page)
|
||||
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
return list(result.scalars().all()), total_items
|
||||
|
||||
async def get_by_id(self, deal_id: int) -> Optional[Deal]:
|
||||
stmt = select(Deal).where(Deal.id == deal_id, Deal.is_deleted.is_(False))
|
||||
|
||||
Reference in New Issue
Block a user