2024-07-25 20:29:21 +02:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
2024-07-23 20:02:57 +02:00
|
|
|
import clientPromise from "@/lib/db";
|
2024-09-07 20:56:08 +02:00
|
|
|
import { revalidatePath } from "next/cache";
|
2024-07-23 20:02:57 +02:00
|
|
|
|
2024-07-25 22:09:06 +02:00
|
|
|
export const GET = async (
|
2024-09-07 09:12:48 +02:00
|
|
|
req: NextRequest,
|
|
|
|
{ params }: { params: { slug: string } }
|
2024-07-25 22:09:06 +02:00
|
|
|
) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
const client = await clientPromise;
|
2024-09-07 21:11:11 +02:00
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
2024-09-07 09:12:48 +02:00
|
|
|
const { slug } = params;
|
2024-07-23 20:02:57 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (!slug) {
|
|
|
|
return NextResponse.json({ message: "Slug is required" }, { status: 400 });
|
|
|
|
}
|
2024-07-23 20:02:57 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const page = await db.collection("pages").findOne({ slug });
|
2024-07-25 22:09:06 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (page) {
|
|
|
|
return NextResponse.json(page, { status: 200 });
|
|
|
|
} else {
|
|
|
|
return NextResponse.json({ message: "Page not found" }, { status: 404 });
|
|
|
|
}
|
2024-07-25 20:29:21 +02:00
|
|
|
};
|
2024-07-25 21:15:08 +02:00
|
|
|
|
2024-07-25 22:09:06 +02:00
|
|
|
export const PUT = async (
|
2024-09-07 09:12:48 +02:00
|
|
|
req: NextRequest,
|
|
|
|
{ params }: { params: { slug: string } }
|
2024-07-25 22:09:06 +02:00
|
|
|
) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
const client = await clientPromise;
|
2024-09-07 21:11:11 +02:00
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
2024-09-07 09:12:48 +02:00
|
|
|
const { slug } = params;
|
2024-07-25 21:15:08 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (!slug) {
|
|
|
|
return NextResponse.json({ message: "Slug is required" }, { status: 400 });
|
|
|
|
}
|
2024-07-25 21:15:08 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const { title, content, vulnerabilities } = await req.json();
|
2024-07-25 22:09:06 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (
|
|
|
|
typeof title !== "string" ||
|
|
|
|
typeof content !== "string" ||
|
|
|
|
typeof vulnerabilities !== "string"
|
|
|
|
) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Invalid title, content, or vulnerabilities" },
|
|
|
|
{ status: 400 }
|
|
|
|
);
|
|
|
|
}
|
2024-07-25 21:15:08 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
const result = await db.collection("pages").findOneAndUpdate(
|
|
|
|
{ slug },
|
|
|
|
{ $set: { title, content, vulnerabilities } },
|
|
|
|
{ returnDocument: "after" } // Updated option
|
|
|
|
);
|
2024-07-25 22:09:06 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (result?.value) {
|
|
|
|
const serializedResult = {
|
|
|
|
...result.value,
|
|
|
|
_id: result.value._id.toString() // Convert ObjectId to string
|
|
|
|
};
|
2024-09-07 20:56:08 +02:00
|
|
|
revalidatePath(`/changelog/${slug}`);
|
|
|
|
revalidatePath("/vulnerabilities");
|
2024-09-07 09:12:48 +02:00
|
|
|
return NextResponse.json(serializedResult, { status: 200 });
|
|
|
|
} else {
|
|
|
|
return NextResponse.json({ message: "Page not found" }, { status: 404 });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error updating page:", error);
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Failed to update page" },
|
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
2024-07-25 22:09:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const DELETE = async (
|
2024-09-07 09:12:48 +02:00
|
|
|
req: NextRequest,
|
|
|
|
{ params }: { params: { slug: string } }
|
2024-07-25 22:09:06 +02:00
|
|
|
) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
const client = await clientPromise;
|
2024-09-07 21:11:11 +02:00
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
2024-09-07 09:12:48 +02:00
|
|
|
const { slug } = params;
|
2024-07-25 22:09:06 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (!slug) {
|
|
|
|
return NextResponse.json({ message: "Slug is required" }, { status: 400 });
|
|
|
|
}
|
2024-07-25 22:09:06 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
const result = await db.collection("pages").deleteOne({ slug });
|
2024-07-25 22:09:06 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (result.deletedCount > 0) {
|
2024-09-07 20:56:08 +02:00
|
|
|
revalidatePath(`/changelog/${slug}`);
|
|
|
|
revalidatePath("/vulnerabilities");
|
2024-09-07 09:12:48 +02:00
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Page deleted successfully" },
|
|
|
|
{ status: 200 }
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return NextResponse.json({ message: "Page not found" }, { status: 404 });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error deleting page:", error);
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Failed to delete page" },
|
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
2024-07-25 21:15:08 +02:00
|
|
|
};
|