svrjs-nextjs-website/app/api/delete/vulnerability/[id]/route.ts
Dorian Niemiec a4cadaac3a
All checks were successful
Deploy Next.js application / deploy (push) Successful in 8m5s
fix: migrate the codebase from Next.js 14 to Next.js 15
2024-11-01 09:56:38 +01:00

41 lines
1.1 KiB
TypeScript

import clientPromise from "@/lib/db";
import { ObjectId } from "mongodb";
import { NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
export async function DELETE(
request: Request,
props: { params: Promise<{ id: string }> }
) {
const params = await props.params;
const { id } = params;
if (!id) {
return NextResponse.json({ message: "ID is required" }, { status: 400 });
}
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 }
);
}
}