CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
The complement to /search for browsing without a query: paginated, filterable (genre, year, status) and sortable (popularity, release/air date, rating, title) listings. Offset-based pagination rather than the Aurora ID cursor scheme, since sorting by popularity/rating/date doesn't line up with creation-order cursors. Total count is computed via a window function in the same query as the page of results, avoiding a second round trip per request. An invalid filter (malformed genre id, unknown status) is a 422 via a per-service InvalidMovieFilterError/InvalidSeriesFilterError, caught before any DB query runs. build_movie_summary/build_series_summary refactored to share their row-to-summary logic with the new list path, avoiding a redundant get_by_id lookup when the row is already in hand. Co-Authored-By: Claude Sonnet 5 <[email protected]>
73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
"""Series endpoints (section 17)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Literal
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from redis.asyncio import Redis
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.api.deps import cached_response, not_found
|
|
from app.core.database import get_db
|
|
from app.core.redis import get_redis
|
|
from app.core.security import enforce_rate_limit
|
|
from app.schemas.tv import SeriesDetailOut, SeriesListOut, SeriesSeasonsOut
|
|
from app.services import tv_service
|
|
from app.services.cache import build_key
|
|
|
|
router = APIRouter(prefix="/series", tags=["series"], dependencies=[Depends(enforce_rate_limit)])
|
|
|
|
|
|
@router.get("", response_model=SeriesListOut)
|
|
async def list_series(
|
|
genre: str | None = Query(None, description="Aurora genre id, e.g. aur_genre_..."),
|
|
year: int | None = Query(None, description="First air date year"),
|
|
status_: str | None = Query(None, alias="status", description="Series status, e.g. ended"),
|
|
sort_by: Literal["popularity", "first_air_date", "vote_average", "title"] = Query("popularity"),
|
|
sort_dir: Literal["asc", "desc"] = Query("desc"),
|
|
page: int = Query(1, ge=1),
|
|
page_size: int = Query(20, ge=1, le=50),
|
|
session: AsyncSession = Depends(get_db),
|
|
) -> SeriesListOut:
|
|
try:
|
|
return await tv_service.list_series(
|
|
session,
|
|
genre_aurora_id=genre,
|
|
year=year,
|
|
status=status_,
|
|
sort_by=sort_by,
|
|
sort_dir=sort_dir,
|
|
page=page,
|
|
page_size=page_size,
|
|
)
|
|
except tv_service.InvalidSeriesFilterError as exc:
|
|
raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, str(exc)) from exc
|
|
|
|
|
|
@router.get("/{aurora_id}", response_model=SeriesDetailOut)
|
|
async def get_series(
|
|
aurora_id: str,
|
|
session: AsyncSession = Depends(get_db),
|
|
redis: Redis = Depends(get_redis),
|
|
) -> SeriesDetailOut:
|
|
try:
|
|
return await cached_response(
|
|
redis,
|
|
build_key("series", aurora_id),
|
|
SeriesDetailOut,
|
|
lambda: tv_service.get_series_detail(session, aurora_id),
|
|
)
|
|
except tv_service.SeriesNotFoundError as exc:
|
|
raise not_found("series", aurora_id) from exc
|
|
|
|
|
|
@router.get("/{aurora_id}/seasons", response_model=SeriesSeasonsOut)
|
|
async def get_series_seasons(
|
|
aurora_id: str, session: AsyncSession = Depends(get_db)
|
|
) -> SeriesSeasonsOut:
|
|
try:
|
|
return await tv_service.get_series_seasons(session, aurora_id)
|
|
except tv_service.SeriesNotFoundError as exc:
|
|
raise not_found("series", aurora_id) from exc
|