From b4fa2c9bfd3cf5750dd1cf8392c513ade8bd316a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Kadir=20Ak=C4=B1n?= Date: Mon, 29 Dec 2025 16:58:18 +0300 Subject: [PATCH] refactor(api): Refactor prompt route to include vote count and user's vote status --- src/app/api/prompts/[id]/route.ts | 26 +++++++++++++++++++++++++- src/app/layout.tsx | 1 - 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/app/api/prompts/[id]/route.ts b/src/app/api/prompts/[id]/route.ts index cb08f7d0..77dc832e 100644 --- a/src/app/api/prompts/[id]/route.ts +++ b/src/app/api/prompts/[id]/route.ts @@ -58,6 +58,9 @@ export async function GET( orderBy: { version: "desc" }, take: 10, }, + _count: { + select: { votes: true }, + }, }, }); @@ -76,7 +79,28 @@ export async function GET( ); } - return NextResponse.json(prompt); + // Check if logged-in user has voted + let hasVoted = false; + if (session?.user?.id) { + const vote = await db.promptVote.findUnique({ + where: { + userId_promptId: { + userId: session.user.id, + promptId: id, + }, + }, + }); + hasVoted = !!vote; + } + + // Omit embedding from response (it's large binary data) + const { embedding: _embedding, ...promptWithoutEmbedding } = prompt; + + return NextResponse.json({ + ...promptWithoutEmbedding, + voteCount: prompt._count.votes, + hasVoted, + }); } catch (error) { console.error("Get prompt error:", error); return NextResponse.json( diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 04385efe..f36f444f 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -130,7 +130,6 @@ export default async function RootLayout({
-
{children}