portfolio/app/api/comments/[slug]/route.ts
Ryan Freeman a6e8c373b8
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m9s
Render article nav on server
2025-03-27 15:47:52 +00:00

33 lines
1.2 KiB
TypeScript

import {createClient} from '@/lib/supabase/server'
import {NextResponse} from 'next/server'
export async function GET(request: Request, {params}: { params: Promise<{ slug: string }> }) {
const {slug} = await params
if (typeof slug !== 'undefined') {
try {
const supabase = await createClient()
const {data: comments, error} = await supabase
.from('comments')
.select(`
id,
content,
published,
created_at,
user:users!inner(id, name, image),
article:articles!inner(id, title, slug)
`)
.eq('article.slug', slug)
.eq('published', true)
.order('created_at', {ascending: false})
if (comments !== null && comments?.length > 0) {
return NextResponse.json({comments: comments})
}
return NextResponse.json([])
} catch (e) {
return new Response(JSON.stringify({status: 'Internal Server Error'}), {status: 500})
}
}
return new Response(JSON.stringify({status: 'Not Found'}), {status: 404})
}