svrjs-nextjs-website/app/api/delete/vulnerability/[id]/route.ts

41 lines
1 KiB
TypeScript
Raw Normal View History

2024-07-31 20:43:04 +02:00
import clientPromise from "@/lib/db";
import { ObjectId } from "mongodb";
import { NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
2024-07-31 20:43:04 +02:00
export async function DELETE(
request: Request,
{ params }: { params: { id: string } }
2024-07-31 20:43:04 +02:00
) {
const { id } = params;
2024-07-31 20:43:04 +02:00
if (!id) {
return NextResponse.json({ message: "ID is required" }, { status: 400 });
}
2024-07-31 20:43:04 +02:00
try {
const client = await clientPromise;
const db = client.db(process.env.MONGODB_DB);
const result = await db
.collection("vulnerabilities")
.deleteOne({ _id: new ObjectId(id) });
if (result.deletedCount === 1) {
revalidatePath("/vulnerabilities");
return NextResponse.json(
{ message: "Vulnerability deleted successfully" },
{ status: 200 }
);
} else {
return NextResponse.json(
{ message: "Vulnerability not found" },
{ status: 404 }
);
}
} catch (error) {
return NextResponse.json(
{ message: "Failed to delete vulnerability" },
{ status: 500 }
);
}
2024-07-31 20:43:04 +02:00
}