feat: Add favorites filtering to sound retrieval; include user-specific favorite sounds in the API response
This commit is contained in:
@@ -8,6 +8,7 @@ from sqlmodel import col, select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.core.logging import get_logger
|
||||
from app.models.favorite import Favorite
|
||||
from app.models.sound import Sound
|
||||
from app.models.sound_played import SoundPlayed
|
||||
from app.repositories.base import BaseRepository
|
||||
@@ -140,11 +141,20 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
sort_order: SortOrder = SortOrder.ASC,
|
||||
limit: int | None = None,
|
||||
offset: int = 0,
|
||||
favorites_only: bool = False,
|
||||
user_id: int | None = None,
|
||||
) -> list[Sound]:
|
||||
"""Search and sort sounds with optional filtering."""
|
||||
try:
|
||||
statement = select(Sound)
|
||||
|
||||
# Apply favorites filter
|
||||
if favorites_only and user_id is not None:
|
||||
statement = statement.join(Favorite).where(
|
||||
Favorite.user_id == user_id,
|
||||
Favorite.sound_id == Sound.id,
|
||||
)
|
||||
|
||||
# Apply type filter
|
||||
if sound_types:
|
||||
statement = statement.where(col(Sound.type).in_(sound_types))
|
||||
@@ -179,12 +189,14 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
logger.exception(
|
||||
(
|
||||
"Failed to search and sort sounds: "
|
||||
"query=%s, types=%s, sort_by=%s, sort_order=%s"
|
||||
"query=%s, types=%s, sort_by=%s, sort_order=%s, favorites_only=%s, user_id=%s"
|
||||
),
|
||||
search_query,
|
||||
sound_types,
|
||||
sort_by,
|
||||
sort_order,
|
||||
favorites_only,
|
||||
user_id,
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
Reference in New Issue
Block a user