33 lines
895 B
TypeScript
33 lines
895 B
TypeScript
import { NextResponse } from "next/server";
|
|
import prisma from "@/lib/prisma";
|
|
import { z } from "zod";
|
|
|
|
const createCommentSchema = z.object({
|
|
postId: z.string().min(1),
|
|
authorId: z.string().optional(),
|
|
content: z.string().min(1),
|
|
isAnonymous: z.boolean().optional(),
|
|
isSecret: z.boolean().optional(),
|
|
});
|
|
|
|
export async function POST(req: Request) {
|
|
const body = await req.json();
|
|
const parsed = createCommentSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
|
}
|
|
const { postId, authorId, content, isAnonymous, isSecret } = parsed.data;
|
|
const comment = await prisma.comment.create({
|
|
data: {
|
|
postId,
|
|
authorId: authorId ?? null,
|
|
content,
|
|
isAnonymous: !!isAnonymous,
|
|
isSecret: !!isSecret,
|
|
},
|
|
});
|
|
return NextResponse.json({ comment }, { status: 201 });
|
|
}
|
|
|
|
|