feat: Enhance favorites functionality; add favorites filtering to playlists and sounds, and improve favorite indicators in responses

This commit is contained in:
JSC
2025-08-16 21:41:50 +02:00
parent 78508c84eb
commit f906b6d643
10 changed files with 97 additions and 52 deletions

View File

@@ -6,9 +6,6 @@ from sqlmodel.ext.asyncio.session import AsyncSession
from app.core.logging import get_logger
from app.models.favorite import Favorite
from app.models.playlist import Playlist
from app.models.sound import Sound
from app.models.user import User
from app.repositories.favorite import FavoriteRepository
from app.repositories.playlist import PlaylistRepository
from app.repositories.sound import SoundRepository
@@ -62,7 +59,7 @@ class FavoriteService:
existing = await favorite_repo.get_by_user_and_sound(user_id, sound_id)
if existing:
raise ValueError(
f"Sound {sound_id} is already favorited by user {user_id}"
f"Sound {sound_id} is already favorited by user {user_id}",
)
# Create favorite
@@ -106,11 +103,11 @@ class FavoriteService:
# Check if already favorited
existing = await favorite_repo.get_by_user_and_playlist(
user_id, playlist_id
user_id, playlist_id,
)
if existing:
raise ValueError(
f"Playlist {playlist_id} is already favorited by user {user_id}"
f"Playlist {playlist_id} is already favorited by user {user_id}",
)
# Create favorite
@@ -159,16 +156,16 @@ class FavoriteService:
favorite_repo = FavoriteRepository(session)
favorite = await favorite_repo.get_by_user_and_playlist(
user_id, playlist_id
user_id, playlist_id,
)
if not favorite:
raise ValueError(
f"Playlist {playlist_id} is not favorited by user {user_id}"
f"Playlist {playlist_id} is not favorited by user {user_id}",
)
await favorite_repo.delete(favorite)
logger.info(
"User %s removed playlist %s from favorites", user_id, playlist_id
"User %s removed playlist %s from favorites", user_id, playlist_id,
)
async def get_user_favorites(
@@ -233,7 +230,7 @@ class FavoriteService:
async with self.db_session_factory() as session:
favorite_repo = FavoriteRepository(session)
return await favorite_repo.get_user_playlist_favorites(
user_id, limit, offset
user_id, limit, offset,
)
async def is_sound_favorited(self, user_id: int, sound_id: int) -> bool:

View File

@@ -246,6 +246,8 @@ class PlaylistService:
include_stats: bool = False,
limit: int | None = None,
offset: int = 0,
favorites_only: bool = False,
current_user_id: int | None = None,
) -> list[dict]:
"""Search and sort playlists with optional statistics."""
return await self.playlist_repo.search_and_sort(
@@ -256,6 +258,8 @@ class PlaylistService:
include_stats=include_stats,
limit=limit,
offset=offset,
favorites_only=favorites_only,
current_user_id=current_user_id,
)
async def get_playlist_sounds(self, playlist_id: int) -> list[Sound]: