refactor: Improve code readability by formatting query parameters in user endpoints and enhancing error handling in sound playback

This commit is contained in:
JSC
2025-08-19 22:09:50 +02:00
parent a660cc1861
commit 560ccd3f7e
10 changed files with 177 additions and 84 deletions

View File

@@ -2,7 +2,7 @@
from typing import Annotated, Any
from fastapi import APIRouter, Depends, Query
from fastapi import APIRouter, Depends, HTTPException, Query, status
from app.core.dependencies import get_current_user, get_dashboard_service
from app.models.user import User
@@ -17,7 +17,13 @@ async def get_soundboard_statistics(
dashboard_service: Annotated[DashboardService, Depends(get_dashboard_service)],
) -> dict[str, Any]:
"""Get soundboard statistics."""
return await dashboard_service.get_soundboard_statistics()
try:
return await dashboard_service.get_soundboard_statistics()
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to fetch soundboard statistics: {e!s}",
) from e
@router.get("/track-statistics")
@@ -26,7 +32,13 @@ async def get_track_statistics(
dashboard_service: Annotated[DashboardService, Depends(get_dashboard_service)],
) -> dict[str, Any]:
"""Get track statistics."""
return await dashboard_service.get_track_statistics()
try:
return await dashboard_service.get_track_statistics()
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to fetch track statistics: {e!s}",
) from e
@router.get("/top-sounds")
@@ -49,8 +61,14 @@ async def get_top_sounds(
] = 10,
) -> list[dict[str, Any]]:
"""Get top sounds by play count for a specific period."""
return await dashboard_service.get_top_sounds(
sound_type=sound_type,
period=period,
limit=limit,
)
try:
return await dashboard_service.get_top_sounds(
sound_type=sound_type,
period=period,
limit=limit,
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to fetch top sounds: {e!s}",
) from e