mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-29 16:14:43 +00:00
Zod for API Routes
This commit is contained in:
@@ -1,20 +1,27 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import prisma from "@/app/prisma";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
const schema = z.object({
|
||||
networkId: z.string(),
|
||||
});
|
||||
|
||||
export async function DELETE(request: NextRequest) {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const networkId = searchParams.get("networkId");
|
||||
const network = schema.parse({ networkId: searchParams.get("networkId") });
|
||||
|
||||
try {
|
||||
await prisma.network.delete({
|
||||
where: {
|
||||
id: Number(networkId),
|
||||
id: Number(network.networkId),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: "Network deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error deleting network:", error);
|
||||
return NextResponse.json({ error: "Failed to delete network" }, { status: 500 });
|
||||
} catch (error: any) {
|
||||
if(error instanceof z.ZodError) {
|
||||
return NextResponse.json({ error: error.issues[0].message }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user