feat: Implement favorites management API; add endpoints for adding, removing, and retrieving favorites for sounds and playlists
feat: Create Favorite model and repository for managing user favorites in the database feat: Add FavoriteService to handle business logic for favorites management feat: Enhance Playlist and Sound response schemas to include favorite indicators and counts refactor: Update API routes to include favorites functionality in playlists and sounds
This commit is contained in:
@@ -19,6 +19,7 @@ from app.schemas.playlist import (
|
||||
PlaylistStatsResponse,
|
||||
PlaylistUpdateRequest,
|
||||
)
|
||||
from app.services.favorite import FavoriteService
|
||||
from app.services.playlist import PlaylistService
|
||||
|
||||
router = APIRouter(prefix="/playlists", tags=["playlists"])
|
||||
@@ -31,6 +32,12 @@ async def get_playlist_service(
|
||||
return PlaylistService(session)
|
||||
|
||||
|
||||
def get_favorite_service() -> FavoriteService:
|
||||
"""Get the favorite service."""
|
||||
from app.core.database import get_session_factory
|
||||
return FavoriteService(get_session_factory())
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def get_all_playlists( # noqa: PLR0913
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
@@ -72,30 +79,46 @@ async def get_all_playlists( # noqa: PLR0913
|
||||
async def get_user_playlists(
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
) -> list[PlaylistResponse]:
|
||||
"""Get playlists for the current user only."""
|
||||
playlists = await playlist_service.get_user_playlists(current_user.id)
|
||||
return [PlaylistResponse.from_playlist(playlist) for playlist in playlists]
|
||||
|
||||
# Add favorite indicators for each playlist
|
||||
playlist_responses = []
|
||||
for playlist in playlists:
|
||||
is_favorited = await favorite_service.is_playlist_favorited(current_user.id, playlist.id)
|
||||
favorite_count = await favorite_service.get_playlist_favorite_count(playlist.id)
|
||||
playlist_response = PlaylistResponse.from_playlist(playlist, is_favorited, favorite_count)
|
||||
playlist_responses.append(playlist_response)
|
||||
|
||||
return playlist_responses
|
||||
|
||||
|
||||
@router.get("/main")
|
||||
async def get_main_playlist(
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
) -> PlaylistResponse:
|
||||
"""Get the global main playlist."""
|
||||
playlist = await playlist_service.get_main_playlist()
|
||||
return PlaylistResponse.from_playlist(playlist)
|
||||
is_favorited = await favorite_service.is_playlist_favorited(current_user.id, playlist.id)
|
||||
favorite_count = await favorite_service.get_playlist_favorite_count(playlist.id)
|
||||
return PlaylistResponse.from_playlist(playlist, is_favorited, favorite_count)
|
||||
|
||||
|
||||
@router.get("/current")
|
||||
async def get_current_playlist(
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
) -> PlaylistResponse:
|
||||
"""Get the global current playlist (falls back to main playlist)."""
|
||||
playlist = await playlist_service.get_current_playlist()
|
||||
return PlaylistResponse.from_playlist(playlist)
|
||||
is_favorited = await favorite_service.is_playlist_favorited(current_user.id, playlist.id)
|
||||
favorite_count = await favorite_service.get_playlist_favorite_count(playlist.id)
|
||||
return PlaylistResponse.from_playlist(playlist, is_favorited, favorite_count)
|
||||
|
||||
|
||||
@router.post("/")
|
||||
@@ -117,12 +140,15 @@ async def create_playlist(
|
||||
@router.get("/{playlist_id}")
|
||||
async def get_playlist(
|
||||
playlist_id: int,
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
) -> PlaylistResponse:
|
||||
"""Get a specific playlist."""
|
||||
playlist = await playlist_service.get_playlist_by_id(playlist_id)
|
||||
return PlaylistResponse.from_playlist(playlist)
|
||||
is_favorited = await favorite_service.is_playlist_favorited(current_user.id, playlist.id)
|
||||
favorite_count = await favorite_service.get_playlist_favorite_count(playlist.id)
|
||||
return PlaylistResponse.from_playlist(playlist, is_favorited, favorite_count)
|
||||
|
||||
|
||||
@router.put("/{playlist_id}")
|
||||
|
||||
Reference in New Issue
Block a user