79 lines
1.7 KiB
TypeScript
79 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import connectDB from '@/lib/mongodb'
|
|
import TopicModel, { transformToTopic } from '@/models/topic'
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ slug: string }> }
|
|
) {
|
|
try {
|
|
await connectDB()
|
|
|
|
const { slug } = await params
|
|
|
|
if (!slug) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Topic slug is required',
|
|
code: 'SLUG_REQUIRED',
|
|
},
|
|
},
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Find the topic post by slug (only published posts)
|
|
const post = await TopicModel.findOne({
|
|
slug,
|
|
isDraft: false
|
|
}).lean()
|
|
|
|
if (!post) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Topic post not found',
|
|
code: 'POST_NOT_FOUND',
|
|
},
|
|
},
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
// Transform to proper format
|
|
const transformedPost = transformToTopic(post)
|
|
|
|
if (!transformedPost) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Failed to process topic post',
|
|
code: 'PROCESSING_ERROR',
|
|
},
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: transformedPost,
|
|
})
|
|
} catch (error) {
|
|
console.error('Error fetching topic post:', error)
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Failed to fetch topic post',
|
|
code: 'SERVER_ERROR',
|
|
},
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |