refactor(api): Refactor prompt route to include vote count and user's vote status

This commit is contained in:
Fatih Kadir Akın
2025-12-29 16:58:18 +03:00
parent 911178c995
commit b4fa2c9bfd
2 changed files with 25 additions and 2 deletions
+25 -1
View File
@@ -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(
-1
View File
@@ -130,7 +130,6 @@ export default async function RootLayout({
<Providers locale={locale} messages={messages} theme={config.theme} branding={{ ...config.branding, useCloneBranding: config.homepage?.useCloneBranding }}>
<LocaleDetector />
<div className="relative min-h-screen flex flex-col">
<AppBanner />
<Header authProvider={config.auth.provider} allowRegistration={config.auth.allowRegistration} />
<main className="flex-1">{children}</main>
<Footer />