스키마 작업완료
This commit is contained in:
32
src/app/api/comments/route.ts
Normal file
32
src/app/api/comments/route.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user