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

@@ -3,9 +3,7 @@
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlmodel.ext.asyncio.session import AsyncSession
from app.core.database import get_db
from app.core.dependencies import get_current_active_user
from app.models.user import User
from app.schemas.common import MessageResponse
@@ -35,7 +33,7 @@ async def get_user_favorites(
) -> FavoritesListResponse:
"""Get all favorites for the current user."""
favorites = await favorite_service.get_user_favorites(
current_user.id, limit, offset
current_user.id, limit, offset,
)
return FavoritesListResponse(favorites=favorites)
@@ -49,7 +47,7 @@ async def get_user_sound_favorites(
) -> FavoritesListResponse:
"""Get sound favorites for the current user."""
favorites = await favorite_service.get_user_sound_favorites(
current_user.id, limit, offset
current_user.id, limit, offset,
)
return FavoritesListResponse(favorites=favorites)
@@ -63,7 +61,7 @@ async def get_user_playlist_favorites(
) -> FavoritesListResponse:
"""Get playlist favorites for the current user."""
favorites = await favorite_service.get_user_playlist_favorites(
current_user.id, limit, offset
current_user.id, limit, offset,
)
return FavoritesListResponse(favorites=favorites)
@@ -94,16 +92,15 @@ async def add_sound_favorite(
status_code=status.HTTP_404_NOT_FOUND,
detail=str(e),
) from e
elif "already favorited" in str(e):
if "already favorited" in str(e):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=str(e),
) from e
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
) from e
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
) from e
@router.post("/playlists/{playlist_id}", response_model=FavoriteResponse)
@@ -115,7 +112,7 @@ async def add_playlist_favorite(
"""Add a playlist to favorites."""
try:
favorite = await favorite_service.add_playlist_favorite(
current_user.id, playlist_id
current_user.id, playlist_id,
)
return FavoriteResponse.model_validate(favorite)
except ValueError as e:
@@ -124,16 +121,15 @@ async def add_playlist_favorite(
status_code=status.HTTP_404_NOT_FOUND,
detail=str(e),
) from e
elif "already favorited" in str(e):
if "already favorited" in str(e):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=str(e),
) from e
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
) from e
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
) from e
@router.delete("/sounds/{sound_id}", response_model=MessageResponse)
@@ -189,6 +185,6 @@ async def check_playlist_favorited(
) -> dict[str, bool]:
"""Check if a playlist is favorited by the current user."""
is_favorited = await favorite_service.is_playlist_favorited(
current_user.id, playlist_id
current_user.id, playlist_id,
)
return {"is_favorited": is_favorited}