feat: Add filtering, searching, and sorting to extraction retrieval endpoints
This commit is contained in:
@@ -5,6 +5,7 @@ from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.models.extraction import Extraction
|
||||
from app.models.user import User
|
||||
from app.repositories.base import BaseRepository
|
||||
|
||||
|
||||
@@ -38,10 +39,11 @@ class ExtractionRepository(BaseRepository[Extraction]):
|
||||
)
|
||||
return list(result.all())
|
||||
|
||||
async def get_pending_extractions(self) -> list[Extraction]:
|
||||
async def get_pending_extractions(self) -> list[tuple[Extraction, User]]:
|
||||
"""Get all pending extractions."""
|
||||
result = await self.session.exec(
|
||||
select(Extraction)
|
||||
select(Extraction, User)
|
||||
.join(User, Extraction.user_id == User.id)
|
||||
.where(Extraction.status == "pending")
|
||||
.order_by(Extraction.created_at),
|
||||
)
|
||||
@@ -63,9 +65,13 @@ class ExtractionRepository(BaseRepository[Extraction]):
|
||||
sort_by: str = "created_at",
|
||||
sort_order: str = "desc",
|
||||
status_filter: str | None = None,
|
||||
) -> list[Extraction]:
|
||||
) -> list[tuple[Extraction, User]]:
|
||||
"""Get extractions for a user with filtering, search, and sorting."""
|
||||
query = select(Extraction).where(Extraction.user_id == user_id)
|
||||
query = (
|
||||
select(Extraction, User)
|
||||
.join(User, Extraction.user_id == User.id)
|
||||
.where(Extraction.user_id == user_id)
|
||||
)
|
||||
|
||||
# Apply search filter
|
||||
if search:
|
||||
@@ -75,7 +81,42 @@ class ExtractionRepository(BaseRepository[Extraction]):
|
||||
Extraction.title.ilike(search_pattern),
|
||||
Extraction.url.ilike(search_pattern),
|
||||
Extraction.service.ilike(search_pattern),
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
# Apply status filter
|
||||
if status_filter:
|
||||
query = query.where(Extraction.status == status_filter)
|
||||
|
||||
# Apply sorting
|
||||
sort_column = getattr(Extraction, sort_by, Extraction.created_at)
|
||||
if sort_order.lower() == "asc":
|
||||
query = query.order_by(asc(sort_column))
|
||||
else:
|
||||
query = query.order_by(desc(sort_column))
|
||||
|
||||
result = await self.session.exec(query)
|
||||
return list(result.all())
|
||||
|
||||
async def get_all_extractions_filtered(
|
||||
self,
|
||||
search: str | None = None,
|
||||
sort_by: str = "created_at",
|
||||
sort_order: str = "desc",
|
||||
status_filter: str | None = None,
|
||||
) -> list[tuple[Extraction, User]]:
|
||||
"""Get all extractions with filtering, search, and sorting."""
|
||||
query = select(Extraction, User).join(User, Extraction.user_id == User.id)
|
||||
|
||||
# Apply search filter
|
||||
if search:
|
||||
search_pattern = f"%{search}%"
|
||||
query = query.where(
|
||||
or_(
|
||||
Extraction.title.ilike(search_pattern),
|
||||
Extraction.url.ilike(search_pattern),
|
||||
Extraction.service.ilike(search_pattern),
|
||||
),
|
||||
)
|
||||
|
||||
# Apply status filter
|
||||
|
||||
Reference in New Issue
Block a user