feat: Add search and sorting functionality to sound repository and API
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Sound repository for database operations."""
|
||||
|
||||
from enum import Enum
|
||||
from sqlalchemy import func
|
||||
from sqlmodel import col, select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
@@ -11,6 +12,26 @@ from app.repositories.base import BaseRepository
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class SoundSortField(str, Enum):
|
||||
"""Sound sort field enumeration."""
|
||||
|
||||
NAME = "name"
|
||||
FILENAME = "filename"
|
||||
DURATION = "duration"
|
||||
SIZE = "size"
|
||||
TYPE = "type"
|
||||
PLAY_COUNT = "play_count"
|
||||
CREATED_AT = "created_at"
|
||||
UPDATED_AT = "updated_at"
|
||||
|
||||
|
||||
class SortOrder(str, Enum):
|
||||
"""Sort order enumeration."""
|
||||
|
||||
ASC = "asc"
|
||||
DESC = "desc"
|
||||
|
||||
|
||||
class SoundRepository(BaseRepository[Sound]):
|
||||
"""Repository for sound operations."""
|
||||
|
||||
@@ -107,3 +128,53 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
except Exception:
|
||||
logger.exception("Failed to get sounds by types: %s", sound_types)
|
||||
raise
|
||||
|
||||
async def search_and_sort(
|
||||
self,
|
||||
search_query: str | None = None,
|
||||
sound_types: list[str] | None = None,
|
||||
sort_by: SoundSortField | None = None,
|
||||
sort_order: SortOrder = SortOrder.ASC,
|
||||
limit: int | None = None,
|
||||
offset: int = 0,
|
||||
) -> list[Sound]:
|
||||
"""Search and sort sounds with optional filtering."""
|
||||
try:
|
||||
statement = select(Sound)
|
||||
|
||||
# Apply type filter
|
||||
if sound_types:
|
||||
statement = statement.where(col(Sound.type).in_(sound_types))
|
||||
|
||||
# Apply search filter
|
||||
if search_query and search_query.strip():
|
||||
search_pattern = f"%{search_query.strip().lower()}%"
|
||||
statement = statement.where(
|
||||
func.lower(Sound.name).like(search_pattern)
|
||||
)
|
||||
|
||||
# Apply sorting
|
||||
if sort_by:
|
||||
sort_column = getattr(Sound, sort_by.value)
|
||||
if sort_order == SortOrder.DESC:
|
||||
statement = statement.order_by(sort_column.desc())
|
||||
else:
|
||||
statement = statement.order_by(sort_column.asc())
|
||||
else:
|
||||
# Default sorting by name ascending
|
||||
statement = statement.order_by(Sound.name.asc())
|
||||
|
||||
# Apply pagination
|
||||
if offset > 0:
|
||||
statement = statement.offset(offset)
|
||||
if limit is not None:
|
||||
statement = statement.limit(limit)
|
||||
|
||||
result = await self.session.exec(statement)
|
||||
return list(result.all())
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to search and sort sounds: query=%s, types=%s, sort_by=%s, sort_order=%s",
|
||||
search_query, sound_types, sort_by, sort_order
|
||||
)
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user