63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/lib/auth-options';
|
|
import { prisma } from '@/lib/prisma';
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ commentId: string }> }
|
|
) {
|
|
const { commentId } = await params;
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const { content } = await request.json();
|
|
|
|
if (!content || typeof content !== 'string' || content.trim().length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Non-empty content is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Verify comment exists
|
|
const comment = await prisma.comment.findUnique({
|
|
where: { id: commentId },
|
|
});
|
|
|
|
if (!comment) {
|
|
return NextResponse.json({ error: 'Comment not found' }, { status: 404 });
|
|
}
|
|
|
|
const reply = await prisma.commentReply.create({
|
|
data: {
|
|
commentId,
|
|
userId: (session.user as any).id,
|
|
content: content.trim(),
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
image: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(reply, { status: 201 });
|
|
} catch (error) {
|
|
console.error('Failed to create reply:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to create reply' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|