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 (req: NextRequest) => {
|
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-07-25 22:09:06 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
const pages = await db.collection("pages").find().toArray();
|
|
|
|
return NextResponse.json(pages, { status: 200 });
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching pages:", error);
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Failed to fetch pages" },
|
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
2024-07-25 20:29:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const POST = async (req: NextRequest) => {
|
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 { title, slug, content } = await req.json();
|
2024-07-25 20:29:21 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (!slug) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Missing required fields" },
|
|
|
|
{ status: 400 }
|
|
|
|
);
|
|
|
|
}
|
2024-07-25 20:29:21 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
const newPage = { title, slug, content };
|
|
|
|
const result = await db.collection("pages").insertOne(newPage);
|
2024-09-07 20:56:08 +02:00
|
|
|
revalidatePath(`/changelog/${slug}`);
|
|
|
|
revalidatePath("/vulnerabilities");
|
2024-09-07 09:12:48 +02:00
|
|
|
return NextResponse.json(newPage, { status: 201 });
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error creating page:", error);
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Failed to create page" },
|
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
2024-07-25 20:29:21 +02:00
|
|
|
};
|