From 78508c84ebb1adaf25093708b185456cc6712456 Mon Sep 17 00:00:00 2001 From: JSC Date: Sat, 16 Aug 2025 21:27:40 +0200 Subject: [PATCH] feat: Add favorites filtering to sound retrieval; include user-specific favorite sounds in the API response --- app/api/v1/sounds.py | 6 ++++++ app/repositories/sound.py | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/api/v1/sounds.py b/app/api/v1/sounds.py index 7068cb3..8fb5564 100644 --- a/app/api/v1/sounds.py +++ b/app/api/v1/sounds.py @@ -70,6 +70,10 @@ async def get_sounds( # noqa: PLR0913 int, Query(description="Number of results to skip", ge=0), ] = 0, + favorites_only: Annotated[ + bool, + Query(description="Show only favorited sounds"), + ] = False, ) -> SoundsListResponse: """Get sounds with optional search, filtering, and sorting.""" try: @@ -80,6 +84,8 @@ async def get_sounds( # noqa: PLR0913 sort_order=sort_order, limit=limit, offset=offset, + favorites_only=favorites_only, + user_id=current_user.id, ) # Add favorite indicators for each sound diff --git a/app/repositories/sound.py b/app/repositories/sound.py index c2581f4..8c8f76e 100644 --- a/app/repositories/sound.py +++ b/app/repositories/sound.py @@ -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