← back to Trademarks Copyright

src/app/api/items/route.ts

34 lines

import { NextRequest, NextResponse } from "next/server";
import { query } from "@/lib/db";

export async function GET(req: NextRequest) {
  const sp = req.nextUrl.searchParams;
  const q = sp.get("q")?.trim() || "";
  const kind = sp.get("kind") || "";
  const status = sp.get("status") || "";

  const where: string[] = [];
  const params: unknown[] = [];
  if (q) {
    params.push(`%${q}%`);
    where.push(`(name ILIKE $${params.length} OR goods_services ILIKE $${params.length} OR notes ILIKE $${params.length} OR original_owner ILIKE $${params.length})`);
  }
  if (kind) { params.push(kind); where.push(`kind = $${params.length}`); }
  if (status) { params.push(status); where.push(`status = $${params.length}`); }

  const sql = `
    SELECT id, kind, name, original_owner, nice_class, goods_services, status,
           expired_date, source, source_url, notes,
           distinctiveness_score, recognition_score, domain_available,
           competing_active_marks, monetization_breadth, composite_score,
           monetization_ideas,
           EXTRACT(YEAR FROM AGE(NOW(), expired_date))::int AS years_expired,
           (CURRENT_DATE + INTERVAL '6 months')::date AS follow_up_date
    FROM items
    ${where.length ? `WHERE ${where.join(" AND ")}` : ""}
    ORDER BY composite_score DESC NULLS LAST, name ASC
  `;
  const { rows } = await query(sql, params);
  return NextResponse.json({ items: rows });
}