All checks were successful
Automated Container Build / build-and-push (push) Successful in 1m10s
35 lines
1 KiB
TypeScript
35 lines
1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import * as deckService from "@/services/deckService";
|
|
import { flashcardImportSchema } from "@/lib/validation/importSchemas";
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const body = await request.json().catch(() => null);
|
|
|
|
if (!body) {
|
|
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
|
|
}
|
|
|
|
const { classId, data, name, groupId } = body;
|
|
|
|
if (!classId || typeof classId !== "string") {
|
|
return NextResponse.json({ error: "classId is required" }, { status: 400 });
|
|
}
|
|
|
|
// Server-side validation — never trust client
|
|
const parsed = flashcardImportSchema.safeParse(data);
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: "Validation failed", details: parsed.error.issues },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const deck = await deckService.createDeckFromImport(
|
|
classId,
|
|
parsed.data,
|
|
name?.trim() || undefined,
|
|
groupId || null
|
|
);
|
|
|
|
return NextResponse.json(deck, { status: 201 });
|
|
}
|