feat(api): 관리자 카테고리 CRUD 추가 및 보드에 category 연동\nfeat(api): 공개 보드 목록 category 포함/필터 지원\ndocs(todo): 체크리스트 2.1~2.3 완료 표시

This commit is contained in:
mota
2025-10-13 06:50:05 +09:00
parent 17a071f125
commit 0ecf29bdfe
6 changed files with 67 additions and 5 deletions

View File

@@ -5,7 +5,7 @@ export async function PATCH(req: Request, context: { params: Promise<{ id: strin
const { id } = await context.params;
const body = await req.json().catch(() => ({}));
const data: any = {};
for (const k of ["name", "slug", "description", "sortOrder", "readLevel", "writeLevel", "allowAnonymousPost", "allowSecretComment", "requiresApproval", "status", "type", "isAdultOnly"]) {
for (const k of ["name", "slug", "description", "sortOrder", "readLevel", "writeLevel", "allowAnonymousPost", "allowSecretComment", "requiresApproval", "status", "type", "isAdultOnly", "categoryId"]) {
if (k in body) data[k] = body[k];
}
if ("requiredTags" in body) {

View File

@@ -17,6 +17,8 @@ export async function GET() {
requiresApproval: true,
type: true,
status: true,
categoryId: true,
category: { select: { id: true, name: true, slug: true } },
},
});
return NextResponse.json({ boards });

View File

@@ -0,0 +1,21 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
export async function PATCH(req: Request, context: { params: Promise<{ id: string }> }) {
const { id } = await context.params;
const body = await req.json().catch(() => ({}));
const data: any = {};
for (const k of ["name", "slug", "sortOrder", "status"]) {
if (k in body) data[k] = body[k];
}
const category = await prisma.boardCategory.update({ where: { id }, data });
return NextResponse.json({ category });
}
export async function DELETE(_: Request, context: { params: Promise<{ id: string }> }) {
const { id } = await context.params;
await prisma.boardCategory.delete({ where: { id } });
return NextResponse.json({ ok: true });
}

View File

@@ -0,0 +1,27 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { z } from "zod";
export async function GET() {
const categories = await prisma.boardCategory.findMany({
orderBy: [{ sortOrder: "asc" }, { createdAt: "asc" }],
});
return NextResponse.json({ categories });
}
const createSchema = z.object({
name: z.string().min(1),
slug: z.string().min(1),
sortOrder: z.coerce.number().int().optional(),
status: z.enum(["active", "hidden"]).optional(),
});
export async function POST(req: Request) {
const body = await req.json().catch(() => ({}));
const parsed = createSchema.safeParse(body);
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
const category = await prisma.boardCategory.create({ data: parsed.data });
return NextResponse.json({ category }, { status: 201 });
}

View File

@@ -1,8 +1,19 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
export async function GET() {
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const category = searchParams.get("category"); // slug or id
const where: any = {};
if (category) {
if (category.length === 25 || category.length === 24) {
where.categoryId = category;
} else {
where.category = { slug: category };
}
}
const boards = await prisma.board.findMany({
where,
orderBy: [{ sortOrder: "asc" }, { createdAt: "asc" }],
select: {
id: true,
@@ -13,6 +24,7 @@ export async function GET() {
requiresApproval: true,
allowAnonymousPost: true,
isAdultOnly: true,
category: { select: { id: true, name: true, slug: true } },
},
});
return NextResponse.json({ boards });