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

39 lines
906 B
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";
export async function DELETE(
request: Request,
{ params }: { params: { id: string } }
) {
const { id } = params;
2024-07-31 20:51:28 +02:00
if (!id) {
return NextResponse.json({ message: "ID is required" }, { status: 400 });
}
2024-07-31 20:43:04 +02:00
2024-07-31 20:51:28 +02:00
try {
2024-08-09 13:36:28 +02:00
const client = await clientPromise;
const db = client.db("downloadsDatabase");
2024-07-31 20:51:28 +02:00
const result = await db
.collection("vulnerabilities")
.deleteOne({ _id: new ObjectId(id) });
2024-07-31 20:43:04 +02:00
if (result.deletedCount === 1) {
2024-07-31 20:51:28 +02:00
return NextResponse.json(
{ message: "Vulnerability deleted successfully" },
{ status: 200 }
);
2024-07-31 20:43:04 +02:00
} else {
2024-07-31 20:51:28 +02:00
return NextResponse.json(
{ message: "Vulnerability not found" },
{ status: 404 }
);
2024-07-31 20:43:04 +02:00
}
} catch (error) {
return NextResponse.json(
2024-07-31 20:51:28 +02:00
{ message: "Failed to delete vulnerability" },
2024-07-31 20:43:04 +02:00
{ status: 500 }
);
}
}