← back to Nineoh Guide

apps/web/app/api/news/route.ts

33 lines

import { NextResponse } from "next/server";
import { pool } from "@/lib/db";

export const dynamic = "force-dynamic";

/**
 * News index for the Expo app — headline + source + link only (no article
 * bodies are stored, for copyright). Newest first.
 */
export async function GET() {
  try {
    const { rows } = await pool.query(
      `select id, headline, publisher_name, canonical_url, published_at
         from news_items
        order by published_at desc nulls last, created_at desc
        limit 100`
    );
    const news = rows.map((r) => ({
      id: r.id,
      headline: r.headline,
      publisher: r.publisher_name ?? null,
      url: r.canonical_url,
      publishedAt: r.published_at
        ? new Date(r.published_at).toISOString()
        : null,
    }));
    return NextResponse.json({ news });
  } catch (err) {
    console.error("[api/news] DB error:", err);
    return NextResponse.json({ news: [] }, { status: 500 });
  }
}