api route 구성 1

This commit is contained in:
koreacomp5
2025-10-09 14:18:55 +09:00
parent 296df7b582
commit 0f7c62d731
7 changed files with 191 additions and 1 deletions

View File

@@ -17,10 +17,66 @@ export async function POST(req: Request) {
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
}
const { boardId, authorId, title, content, isAnonymous } = parsed.data;
const board = await prisma.board.findUnique({ where: { id: boardId } });
const requiresApproval = board?.requiresApproval ?? false;
const post = await prisma.post.create({
data: { boardId, authorId: authorId ?? null, title, content, isAnonymous: !!isAnonymous },
data: {
boardId,
authorId: authorId ?? null,
title,
content,
isAnonymous: !!isAnonymous,
status: requiresApproval ? "hidden" : "published",
},
});
return NextResponse.json({ post }, { status: 201 });
}
const listQuerySchema = z.object({
page: z.coerce.number().min(1).default(1),
pageSize: z.coerce.number().min(1).max(100).default(10),
boardId: z.string().optional(),
q: z.string().optional(),
});
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const parsed = listQuerySchema.safeParse(Object.fromEntries(searchParams));
if (!parsed.success) {
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
}
const { page, pageSize, boardId, q } = parsed.data;
const where = {
...(boardId ? { boardId } : {}),
...(q
? {
OR: [
{ title: { contains: q } },
{ content: { contains: q } },
],
}
: {}),
} as const;
const [total, items] = await Promise.all([
prisma.post.count({ where }),
prisma.post.findMany({
where,
orderBy: [{ isPinned: "desc" }, { createdAt: "desc" }],
skip: (page - 1) * pageSize,
take: pageSize,
select: {
id: true,
title: true,
createdAt: true,
boardId: true,
isPinned: true,
status: true,
},
}),
]);
return NextResponse.json({ total, page, pageSize, items });
}