Refactor Study Desk application structure
This commit is contained in:
parent
faaccf8a7e
commit
089439ed90
145 changed files with 8087 additions and 3412 deletions
|
|
@ -1,20 +1,23 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import * as classService from "@/services/classService";
|
||||
import { classCreateSchema, isPrismaError } from "@/lib/validation/contentSchemas";
|
||||
|
||||
export async function GET() {
|
||||
const classes = await classService.listClasses();
|
||||
return NextResponse.json(classes);
|
||||
return NextResponse.json(await classService.listClasses());
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const body = await request.json().catch(() => null);
|
||||
if (!body?.name || typeof body.name !== "string" || !body.name.trim()) {
|
||||
return NextResponse.json(
|
||||
{ error: "Class name is required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
const parsed = classCreateSchema.safeParse(await request.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: "Class name is required and must be 160 characters or fewer" }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
return NextResponse.json(await classService.createClass(parsed.data.name), { status: 201 });
|
||||
} catch (error) {
|
||||
if (isPrismaError(error, "P2002")) {
|
||||
return NextResponse.json({ error: "A class with that slug already exists" }, { status: 409 });
|
||||
}
|
||||
console.error("Failed to create class", error);
|
||||
return NextResponse.json({ error: "Failed to create class" }, { status: 500 });
|
||||
}
|
||||
|
||||
const newClass = await classService.createClass(body.name.trim());
|
||||
return NextResponse.json(newClass, { status: 201 });
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue