route.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { NextResponse } from "next/server";
  2. import { REEL_POSTER_COUNT } from "@/lib/constants";
  3. import { fetchTMDBMovies } from "@/lib/tmdb-fetch";
  4. interface ReelPoster {
  5. tmdb_id: number;
  6. poster_path: string;
  7. title: string;
  8. }
  9. // Always-pinned entries that lead the carousel.
  10. const PINNED_REEL_POSTERS: ReelPoster[] = [
  11. {
  12. tmdb_id: 615,
  13. title: "The Passion of the Christ",
  14. poster_path: "/rBM5o2HpmCfDejuIPybI09tkY3V.jpg",
  15. },
  16. ];
  17. // Fallback: fetch popular movies for reel posters.
  18. // In production, the cron job populates the landing_reel_posters table.
  19. export async function GET() {
  20. try {
  21. const params = new URLSearchParams({
  22. language: "en-US",
  23. page: "1",
  24. include_adult: "false",
  25. });
  26. const { movies, error, status } = await fetchTMDBMovies("/movie/popular", params);
  27. if (error) {
  28. return NextResponse.json({ error }, { status: status ?? 502 });
  29. }
  30. const popular: ReelPoster[] = movies.map((m) => ({
  31. tmdb_id: m.id,
  32. poster_path: m.poster_path!,
  33. title: m.title,
  34. }));
  35. const seen = new Set<number>();
  36. const posters: ReelPoster[] = [];
  37. for (const p of [...PINNED_REEL_POSTERS, ...popular]) {
  38. if (seen.has(p.tmdb_id)) continue;
  39. seen.add(p.tmdb_id);
  40. posters.push(p);
  41. if (posters.length >= REEL_POSTER_COUNT) break;
  42. }
  43. return NextResponse.json({ posters });
  44. } catch {
  45. return NextResponse.json({ error: "Internal server error" }, { status: 500 });
  46. }
  47. }