feat(api): Update anime API endpoints to use database storage
Task 6: Update Anime API endpoints to use database - Modified add_series endpoint to save series to database when available - Added get_optional_database_session dependency for graceful fallback - Falls back to file-based storage when database unavailable - All 55 API tests and 809 unit tests pass
This commit is contained in:
@@ -134,6 +134,38 @@ async def get_database_session() -> AsyncGenerator:
|
||||
)
|
||||
|
||||
|
||||
async def get_optional_database_session() -> AsyncGenerator:
|
||||
"""
|
||||
Dependency to get optional database session.
|
||||
|
||||
Unlike get_database_session(), this returns None if the database
|
||||
is not available, allowing endpoints to fall back to other storage.
|
||||
|
||||
Yields:
|
||||
AsyncSession or None: Database session if available, None otherwise
|
||||
|
||||
Example:
|
||||
@app.post("/anime/add")
|
||||
async def add_anime(
|
||||
db: Optional[AsyncSession] = Depends(get_optional_database_session)
|
||||
):
|
||||
if db:
|
||||
# Use database
|
||||
await AnimeSeriesService.create(db, ...)
|
||||
else:
|
||||
# Fall back to file-based storage
|
||||
series_app.list.add(serie)
|
||||
"""
|
||||
try:
|
||||
from src.server.database import get_db_session
|
||||
|
||||
async with get_db_session() as session:
|
||||
yield session
|
||||
except (ImportError, RuntimeError):
|
||||
# Database not available - yield None
|
||||
yield None
|
||||
|
||||
|
||||
def get_current_user(
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(
|
||||
http_bearer_security
|
||||
|
||||
Reference in New Issue
Block a user