Files
msgapp/src/app/api/posts/route.ts

83 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-10-09 11:23:27 +09:00
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { z } from "zod";
const createPostSchema = z.object({
boardId: z.string().min(1),
authorId: z.string().optional(),
title: z.string().min(1),
content: z.string().min(1),
isAnonymous: z.boolean().optional(),
});
export async function POST(req: Request) {
const body = await req.json();
const parsed = createPostSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
}
const { boardId, authorId, title, content, isAnonymous } = parsed.data;
2025-10-09 14:18:55 +09:00
const board = await prisma.board.findUnique({ where: { id: boardId } });
const requiresApproval = board?.requiresApproval ?? false;
2025-10-09 11:23:27 +09:00
const post = await prisma.post.create({
2025-10-09 14:18:55 +09:00
data: {
boardId,
authorId: authorId ?? null,
title,
content,
isAnonymous: !!isAnonymous,
status: requiresApproval ? "hidden" : "published",
},
2025-10-09 11:23:27 +09:00
});
return NextResponse.json({ post }, { status: 201 });
}
2025-10-09 14:18:55 +09:00
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 });
}
2025-10-09 11:23:27 +09:00