48 lines
946 B
TypeScript
48 lines
946 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
type RouteParams = {
|
|
params: {
|
|
id: string;
|
|
};
|
|
};
|
|
|
|
export async function GET(_: Request, { params }: RouteParams) {
|
|
const id = Number(params.id);
|
|
|
|
if (!Number.isInteger(id)) {
|
|
return NextResponse.json(
|
|
{ error: "ID inválido." },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const prompt = await prisma.prompt.findUnique({
|
|
where: { id },
|
|
include: { categories: true }
|
|
});
|
|
|
|
if (!prompt) {
|
|
return NextResponse.json(
|
|
{ error: "Prompt não encontrado." },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const answers = prompt.categories.reduce<Record<string, string>>(
|
|
(acc, category) => {
|
|
acc[category.categoryKey] = category.content;
|
|
return acc;
|
|
},
|
|
{}
|
|
);
|
|
|
|
return NextResponse.json({
|
|
id: prompt.id,
|
|
title: prompt.title,
|
|
createdAt: prompt.createdAt,
|
|
updatedAt: prompt.updatedAt,
|
|
answers
|
|
});
|
|
}
|