feat: Add endpoint to retrieve sounds with optional type filtering and implement corresponding repository method
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Sound repository for database operations."""
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlmodel import select
|
||||
from sqlmodel import col, select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.core.logging import get_logger
|
||||
@@ -95,3 +95,15 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
sound_type,
|
||||
)
|
||||
raise
|
||||
|
||||
async def get_by_types(self, sound_types: list[str] | None = None) -> list[Sound]:
|
||||
"""Get sounds by types. If types is None or empty, return all sounds."""
|
||||
try:
|
||||
statement = select(Sound)
|
||||
if sound_types:
|
||||
statement = statement.where(col(Sound.type).in_(sound_types))
|
||||
result = await self.session.exec(statement)
|
||||
return list(result.all())
|
||||
except Exception:
|
||||
logger.exception("Failed to get sounds by types: %s", sound_types)
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user