diff --git a/src/app/api/__tests__/api/source.test.ts b/src/app/api/__tests__/api/source.test.ts index 053e1334dfed533125fa0c67faa2513afbe886aa..f0c341d467361a722d5284a343f7e9a6c464142b 100644 --- a/src/app/api/__tests__/api/source.test.ts +++ b/src/app/api/__tests__/api/source.test.ts @@ -83,9 +83,9 @@ describe("unit tests for /categories API Endpoint", () => { const response = await GET(req); const jsonResult = await response.json(); - expect(response.status).toBe(500); + expect(response.status).toBe(404); expect(jsonResult).toMatchObject({ - error: { message: "Error while fetching source for given ID" }, + error: { message: "No source found for given ID" }, }); }); }); diff --git a/src/app/api/source/route.ts b/src/app/api/source/route.ts index 1cf128759c4582062a64a830af5301d032552b29..f3d63c1f790dd4272a986b7c8b01571c0bea264f 100644 --- a/src/app/api/source/route.ts +++ b/src/app/api/source/route.ts @@ -1,23 +1,27 @@ import { type NextRequest } from "next/server"; +import SolrQuery from "@/app/api/utils/solrQuery"; import { type SolrResponseType } from "@/types/types"; -const SOLR_URL = new URL(process.env.NEXT_PRIVATE_SOLR_SERVICE_URL || ""); - export const GET = async (req: NextRequest) => { const id = req.nextUrl.searchParams.get("id"); if (!id) { return Response.json({ error: { message: "Request missing ID parameter" } }, { status: 400 }); } try { - const params = new URLSearchParams({ - q: "*:*", - fq: `+id:"${id}"`, - rows: "1", - }); + const solrResponse: SolrResponseType = await new SolrQuery( + 0, + 1, + [], + [], + undefined, + undefined, + `id:${id}` + ).json(); - const response = await fetch(`${SOLR_URL}/select?${params}`); - const solrResponse: SolrResponseType = await response.json(); + if (solrResponse.response.numFound === 0) { + return Response.json({ error: { message: "No source found for given ID" } }, { status: 404 }); + } return Response.json(JSON.parse(solrResponse.response.docs[0].json_source)); } catch (error) { console.log("Error while handling source", error);