feat(api): 관리자 카테고리 CRUD 추가 및 보드에 category 연동\nfeat(api): 공개 보드 목록 category 포함/필터 지원\ndocs(todo): 체크리스트 2.1~2.3 완료 표시
This commit is contained in:
21
src/app/api/admin/categories/[id]/route.ts
Normal file
21
src/app/api/admin/categories/[id]/route.ts
Normal 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 });
|
||||
}
|
||||
|
||||
|
||||
27
src/app/api/admin/categories/route.ts
Normal file
27
src/app/api/admin/categories/route.ts
Normal 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 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user