feat: Add favorites filtering to sound retrieval; include user-specific favorite sounds in the API response

This commit is contained in:
JSC
2025-08-16 21:27:40 +02:00
parent a947fd830b
commit 78508c84eb
2 changed files with 19 additions and 1 deletions

View File

@@ -70,6 +70,10 @@ async def get_sounds( # noqa: PLR0913
int, int,
Query(description="Number of results to skip", ge=0), Query(description="Number of results to skip", ge=0),
] = 0, ] = 0,
favorites_only: Annotated[
bool,
Query(description="Show only favorited sounds"),
] = False,
) -> SoundsListResponse: ) -> SoundsListResponse:
"""Get sounds with optional search, filtering, and sorting.""" """Get sounds with optional search, filtering, and sorting."""
try: try:
@@ -80,6 +84,8 @@ async def get_sounds( # noqa: PLR0913
sort_order=sort_order, sort_order=sort_order,
limit=limit, limit=limit,
offset=offset, offset=offset,
favorites_only=favorites_only,
user_id=current_user.id,
) )
# Add favorite indicators for each sound # Add favorite indicators for each sound

View File

@@ -8,6 +8,7 @@ from sqlmodel import col, select
from sqlmodel.ext.asyncio.session import AsyncSession from sqlmodel.ext.asyncio.session import AsyncSession
from app.core.logging import get_logger from app.core.logging import get_logger
from app.models.favorite import Favorite
from app.models.sound import Sound from app.models.sound import Sound
from app.models.sound_played import SoundPlayed from app.models.sound_played import SoundPlayed
from app.repositories.base import BaseRepository from app.repositories.base import BaseRepository
@@ -140,11 +141,20 @@ class SoundRepository(BaseRepository[Sound]):
sort_order: SortOrder = SortOrder.ASC, sort_order: SortOrder = SortOrder.ASC,
limit: int | None = None, limit: int | None = None,
offset: int = 0, offset: int = 0,
favorites_only: bool = False,
user_id: int | None = None,
) -> list[Sound]: ) -> list[Sound]:
"""Search and sort sounds with optional filtering.""" """Search and sort sounds with optional filtering."""
try: try:
statement = select(Sound) 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 # Apply type filter
if sound_types: if sound_types:
statement = statement.where(col(Sound.type).in_(sound_types)) statement = statement.where(col(Sound.type).in_(sound_types))
@@ -179,12 +189,14 @@ class SoundRepository(BaseRepository[Sound]):
logger.exception( logger.exception(
( (
"Failed to search and sort sounds: " "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, search_query,
sound_types, sound_types,
sort_by, sort_by,
sort_order, sort_order,
favorites_only,
user_id,
) )
raise raise