38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import {
|
|
materialGroupCreateSchema,
|
|
materialGroupQuerySchema,
|
|
} from "@/lib/validation/materialGroupSchemas";
|
|
import {
|
|
createMaterialGroup,
|
|
listMaterialGroups,
|
|
} from "@/services/materialGroupService";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const parsed = materialGroupQuerySchema.safeParse(
|
|
Object.fromEntries(request.nextUrl.searchParams)
|
|
);
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "Invalid group query" }, { status: 400 });
|
|
}
|
|
return NextResponse.json(
|
|
await listMaterialGroups(parsed.data.classId, parsed.data.type)
|
|
);
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const parsed = materialGroupCreateSchema.safeParse(
|
|
await request.json().catch(() => null)
|
|
);
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: parsed.error.issues[0]?.message ?? "Invalid group" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
const group = await createMaterialGroup(parsed.data);
|
|
if (!group) {
|
|
return NextResponse.json({ error: "Class not found" }, { status: 404 });
|
|
}
|
|
return NextResponse.json(group, { status: 201 });
|
|
}
|