2026-09-04 17:34:03 -05:00
|
|
|
"""Assembles Movie API responses from repository data."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.ids import aurora_id, parse_aurora_id
|
2026-09-05 09:01:11 -05:00
|
|
|
from app.models.enums import ArtworkType, MovieStatus
|
|
|
|
|
from app.models.movie import Movie
|
2026-09-04 17:34:03 -05:00
|
|
|
from app.repositories import movie_repository, polymorphic
|
|
|
|
|
from app.schemas.common import CollectionBriefOut, CompanyBriefOut, GenreOut, KeywordOut
|
|
|
|
|
from app.schemas.movie import (
|
|
|
|
|
MovieCreditsOut,
|
|
|
|
|
MovieDetailOut,
|
|
|
|
|
MovieImagesOut,
|
2026-09-05 09:01:11 -05:00
|
|
|
MovieListOut,
|
2026-09-05 08:16:18 -05:00
|
|
|
MovieRecommendationsOut,
|
2026-09-04 17:34:03 -05:00
|
|
|
MovieSummaryOut,
|
|
|
|
|
MovieVideosOut,
|
|
|
|
|
)
|
2026-09-06 04:07:18 -05:00
|
|
|
from app.services.common import (
|
|
|
|
|
pick_certification,
|
|
|
|
|
primary_artwork_path,
|
|
|
|
|
split_credits,
|
|
|
|
|
to_images_out,
|
|
|
|
|
to_release_date_out,
|
|
|
|
|
to_video_out,
|
|
|
|
|
)
|
2026-09-04 17:34:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MovieNotFoundError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2026-09-05 09:01:11 -05:00
|
|
|
class InvalidMovieFilterError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2026-09-04 17:34:03 -05:00
|
|
|
async def _resolve_movie_id(movie_aurora_id: str) -> uuid.UUID:
|
|
|
|
|
try:
|
|
|
|
|
return uuid.UUID(parse_aurora_id(movie_aurora_id, expected_type="movie"))
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise MovieNotFoundError(str(exc)) from exc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_movie_detail(session: AsyncSession, movie_aurora_id: str) -> MovieDetailOut:
|
|
|
|
|
movie_id = await _resolve_movie_id(movie_aurora_id)
|
|
|
|
|
movie = await movie_repository.get_movie_by_id(session, movie_id)
|
|
|
|
|
if movie is None:
|
|
|
|
|
raise MovieNotFoundError(movie_aurora_id)
|
|
|
|
|
|
|
|
|
|
genres = await movie_repository.get_movie_genres(session, movie_id)
|
|
|
|
|
keywords = await movie_repository.get_movie_keywords(session, movie_id)
|
|
|
|
|
companies = await movie_repository.get_movie_companies(session, movie_id)
|
|
|
|
|
credits_ = await polymorphic.get_credits(session, "movie", movie_id)
|
|
|
|
|
artwork = await polymorphic.get_artwork(session, "movie", movie_id)
|
|
|
|
|
videos = await polymorphic.get_videos(session, "movie", movie_id)
|
2026-09-06 04:07:18 -05:00
|
|
|
release_dates = await polymorphic.get_release_dates(session, "movie", movie_id)
|
2026-09-04 17:34:03 -05:00
|
|
|
external_ids = await polymorphic.get_external_ids(session, "movie", movie_id)
|
|
|
|
|
|
|
|
|
|
cast, crew = split_credits(credits_)
|
|
|
|
|
|
2026-09-05 20:08:26 -05:00
|
|
|
collection_out: CollectionBriefOut | None = None
|
|
|
|
|
if movie.collection is not None:
|
|
|
|
|
collection_posters = await polymorphic.get_artwork(
|
|
|
|
|
session, "collection", movie.collection.id, ArtworkType.POSTER
|
|
|
|
|
)
|
|
|
|
|
collection_out = CollectionBriefOut(
|
|
|
|
|
id=aurora_id("collection", str(movie.collection.id)),
|
|
|
|
|
name=movie.collection.name,
|
|
|
|
|
poster_path=primary_artwork_path(collection_posters, ArtworkType.POSTER),
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-05 08:01:56 -05:00
|
|
|
company_out: list[CompanyBriefOut] = []
|
|
|
|
|
for company, _role in companies:
|
|
|
|
|
logos = await polymorphic.get_artwork(session, "company", company.id, ArtworkType.LOGO)
|
|
|
|
|
company_out.append(
|
|
|
|
|
CompanyBriefOut(
|
|
|
|
|
id=aurora_id("company", str(company.id)),
|
|
|
|
|
name=company.name,
|
|
|
|
|
company_type=company.company_type.value,
|
|
|
|
|
logo_path=primary_artwork_path(logos, ArtworkType.LOGO),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-04 17:34:03 -05:00
|
|
|
return MovieDetailOut(
|
|
|
|
|
id=aurora_id("movie", str(movie.id)),
|
|
|
|
|
title=movie.title,
|
|
|
|
|
original_title=movie.original_title,
|
|
|
|
|
overview=movie.overview,
|
|
|
|
|
tagline=movie.tagline,
|
|
|
|
|
status=movie.status.value,
|
|
|
|
|
original_language=movie.original_language_code,
|
|
|
|
|
release_date=movie.primary_release_date,
|
|
|
|
|
runtime=movie.runtime_minutes,
|
|
|
|
|
budget=movie.budget,
|
|
|
|
|
revenue=movie.revenue,
|
|
|
|
|
popularity=movie.popularity,
|
|
|
|
|
vote_average=movie.vote_average,
|
|
|
|
|
vote_count=movie.vote_count,
|
|
|
|
|
adult=movie.adult,
|
2026-09-06 04:07:18 -05:00
|
|
|
certification=pick_certification(release_dates),
|
2026-09-04 17:34:03 -05:00
|
|
|
genres=[GenreOut(id=aurora_id("genre", str(g.id)), name=g.name) for g in genres],
|
|
|
|
|
keywords=[KeywordOut(id=aurora_id("keyword", str(k.id)), name=k.name) for k in keywords],
|
|
|
|
|
cast=cast,
|
|
|
|
|
crew=crew,
|
2026-09-05 08:01:56 -05:00
|
|
|
companies=company_out,
|
2026-09-05 20:08:26 -05:00
|
|
|
collection=collection_out,
|
2026-09-04 17:34:03 -05:00
|
|
|
images=to_images_out(artwork),
|
|
|
|
|
videos=[to_video_out(v) for v in videos],
|
2026-09-06 04:07:18 -05:00
|
|
|
release_dates=[to_release_date_out(rd) for rd in release_dates],
|
2026-09-04 17:34:03 -05:00
|
|
|
external_ids=external_ids,
|
|
|
|
|
created_at=movie.created_at,
|
|
|
|
|
updated_at=movie.updated_at,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_movie_credits(session: AsyncSession, movie_aurora_id: str) -> MovieCreditsOut:
|
|
|
|
|
movie_id = await _resolve_movie_id(movie_aurora_id)
|
|
|
|
|
movie = await movie_repository.get_movie_by_id(session, movie_id)
|
|
|
|
|
if movie is None:
|
|
|
|
|
raise MovieNotFoundError(movie_aurora_id)
|
|
|
|
|
credits_ = await polymorphic.get_credits(session, "movie", movie_id)
|
|
|
|
|
cast, crew = split_credits(credits_)
|
|
|
|
|
return MovieCreditsOut(id=aurora_id("movie", str(movie.id)), cast=cast, crew=crew)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_movie_images(session: AsyncSession, movie_aurora_id: str) -> MovieImagesOut:
|
|
|
|
|
movie_id = await _resolve_movie_id(movie_aurora_id)
|
|
|
|
|
movie = await movie_repository.get_movie_by_id(session, movie_id)
|
|
|
|
|
if movie is None:
|
|
|
|
|
raise MovieNotFoundError(movie_aurora_id)
|
|
|
|
|
artwork = await polymorphic.get_artwork(session, "movie", movie_id)
|
|
|
|
|
return MovieImagesOut(id=aurora_id("movie", str(movie.id)), images=to_images_out(artwork))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_movie_videos(session: AsyncSession, movie_aurora_id: str) -> MovieVideosOut:
|
|
|
|
|
movie_id = await _resolve_movie_id(movie_aurora_id)
|
|
|
|
|
movie = await movie_repository.get_movie_by_id(session, movie_id)
|
|
|
|
|
if movie is None:
|
|
|
|
|
raise MovieNotFoundError(movie_aurora_id)
|
|
|
|
|
videos = await polymorphic.get_videos(session, "movie", movie_id)
|
|
|
|
|
return MovieVideosOut(id=aurora_id("movie", str(movie.id)), videos=[to_video_out(v) for v in videos])
|
|
|
|
|
|
|
|
|
|
|
2026-09-05 08:16:18 -05:00
|
|
|
async def get_movie_recommendations(
|
|
|
|
|
session: AsyncSession, movie_aurora_id: str
|
|
|
|
|
) -> MovieRecommendationsOut:
|
|
|
|
|
movie_id = await _resolve_movie_id(movie_aurora_id)
|
|
|
|
|
movie = await movie_repository.get_movie_by_id(session, movie_id)
|
|
|
|
|
if movie is None:
|
|
|
|
|
raise MovieNotFoundError(movie_aurora_id)
|
|
|
|
|
recommended_ids = await movie_repository.get_movie_recommendation_ids(session, movie_id)
|
|
|
|
|
summaries = await build_movie_summaries(session, recommended_ids)
|
|
|
|
|
return MovieRecommendationsOut(id=aurora_id("movie", str(movie.id)), recommendations=summaries)
|
|
|
|
|
|
|
|
|
|
|
2026-09-05 09:01:11 -05:00
|
|
|
async def _movie_summary_from_row(session: AsyncSession, movie: Movie) -> MovieSummaryOut:
|
|
|
|
|
posters = await polymorphic.get_artwork(session, "movie", movie.id, ArtworkType.POSTER)
|
2026-09-04 17:34:03 -05:00
|
|
|
return MovieSummaryOut(
|
|
|
|
|
id=aurora_id("movie", str(movie.id)),
|
|
|
|
|
title=movie.title,
|
|
|
|
|
original_title=movie.original_title,
|
|
|
|
|
overview=movie.overview,
|
|
|
|
|
release_date=movie.primary_release_date,
|
|
|
|
|
poster_path=primary_artwork_path(posters, ArtworkType.POSTER),
|
|
|
|
|
popularity=movie.popularity,
|
|
|
|
|
vote_average=movie.vote_average,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-09-05 09:01:11 -05:00
|
|
|
async def build_movie_summary(session: AsyncSession, movie_id: uuid.UUID) -> MovieSummaryOut | None:
|
|
|
|
|
movie = await movie_repository.get_movie_by_id(session, movie_id)
|
|
|
|
|
if movie is None:
|
|
|
|
|
return None
|
|
|
|
|
return await _movie_summary_from_row(session, movie)
|
|
|
|
|
|
|
|
|
|
|
2026-09-04 17:34:03 -05:00
|
|
|
async def build_movie_summaries(
|
|
|
|
|
session: AsyncSession, movie_ids: list[uuid.UUID]
|
|
|
|
|
) -> list[MovieSummaryOut]:
|
|
|
|
|
out: list[MovieSummaryOut] = []
|
|
|
|
|
for movie_id in movie_ids:
|
|
|
|
|
summary = await build_movie_summary(session, movie_id)
|
|
|
|
|
if summary is not None:
|
|
|
|
|
out.append(summary)
|
|
|
|
|
return out
|
2026-09-05 09:01:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def list_movies(
|
|
|
|
|
session: AsyncSession,
|
|
|
|
|
*,
|
|
|
|
|
genre_aurora_id: str | None,
|
|
|
|
|
year: int | None,
|
|
|
|
|
status: str | None,
|
|
|
|
|
sort_by: str,
|
|
|
|
|
sort_dir: str,
|
|
|
|
|
page: int,
|
|
|
|
|
page_size: int,
|
|
|
|
|
) -> MovieListOut:
|
|
|
|
|
genre_id: uuid.UUID | None = None
|
|
|
|
|
if genre_aurora_id is not None:
|
|
|
|
|
try:
|
|
|
|
|
genre_id = uuid.UUID(parse_aurora_id(genre_aurora_id, expected_type="genre"))
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise InvalidMovieFilterError(f"invalid genre id: {genre_aurora_id!r}") from exc
|
|
|
|
|
|
|
|
|
|
status_enum: MovieStatus | None = None
|
|
|
|
|
if status is not None:
|
|
|
|
|
try:
|
|
|
|
|
status_enum = MovieStatus(status)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise InvalidMovieFilterError(f"unknown movie status: {status!r}") from exc
|
|
|
|
|
|
|
|
|
|
movies, total = await movie_repository.list_movies(
|
|
|
|
|
session,
|
|
|
|
|
genre_id=genre_id,
|
|
|
|
|
year=year,
|
|
|
|
|
status=status_enum,
|
|
|
|
|
sort_by=sort_by,
|
|
|
|
|
sort_dir=sort_dir,
|
|
|
|
|
limit=page_size,
|
|
|
|
|
offset=(page - 1) * page_size,
|
|
|
|
|
)
|
|
|
|
|
results = [await _movie_summary_from_row(session, m) for m in movies]
|
|
|
|
|
total_pages = (total + page_size - 1) // page_size if total else 0
|
|
|
|
|
|
|
|
|
|
return MovieListOut(
|
|
|
|
|
page=page, page_size=page_size, total_results=total, total_pages=total_pages, results=results
|
|
|
|
|
)
|