2024-07-20 21:04:38 +02:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
2024-07-20 21:37:40 +02:00
|
|
|
import { promises as fs } from "fs";
|
|
|
|
import path from "path";
|
2024-07-20 21:04:38 +02:00
|
|
|
import matter from "gray-matter";
|
|
|
|
|
2024-07-20 21:37:40 +02:00
|
|
|
// Define the path to the changelogs directory
|
|
|
|
const pagesDir = path.join(process.cwd(), "data", "pages");
|
|
|
|
|
|
|
|
export async function GET(
|
|
|
|
request: NextRequest,
|
|
|
|
{ params }: { params: { slug: string } }
|
|
|
|
) {
|
|
|
|
const { slug } = params;
|
|
|
|
const filePath = path.join(pagesDir, `${slug}.mdx`);
|
2024-07-20 21:04:38 +02:00
|
|
|
|
2024-07-20 21:37:40 +02:00
|
|
|
try {
|
|
|
|
const content = await fs.readFile(filePath, "utf8");
|
|
|
|
// Use gray-matter to parse the front matter and content
|
|
|
|
const { data, content: mdxContent } = matter(content);
|
|
|
|
return NextResponse.json({ title: data.title, content: mdxContent });
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to load page:", error);
|
|
|
|
return NextResponse.json({ error: "Page not found" }, { status: 404 });
|
|
|
|
}
|
2024-07-20 21:04:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 21:37:40 +02:00
|
|
|
export async function PUT(
|
|
|
|
request: NextRequest,
|
|
|
|
{ params }: { params: { slug: string } }
|
|
|
|
) {
|
|
|
|
const { slug } = params;
|
|
|
|
const filePath = path.join(pagesDir, `${slug}.mdx`);
|
|
|
|
const { title, content } = await request.json();
|
|
|
|
|
2024-07-20 21:04:38 +02:00
|
|
|
try {
|
2024-07-20 21:37:40 +02:00
|
|
|
await fs.writeFile(filePath, `---\ntitle: ${title}\n---\n${content}`);
|
|
|
|
return NextResponse.json({ title, slug, content });
|
2024-07-20 21:04:38 +02:00
|
|
|
} catch (error) {
|
2024-07-20 21:37:40 +02:00
|
|
|
console.error("Failed to update page:", error);
|
2024-07-20 21:04:38 +02:00
|
|
|
return NextResponse.json(
|
2024-07-20 21:37:40 +02:00
|
|
|
{ error: "Failed to update page" },
|
2024-07-20 21:04:38 +02:00
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-20 21:37:40 +02:00
|
|
|
export async function DELETE(
|
|
|
|
request: NextRequest,
|
|
|
|
{ params }: { params: { slug: string } }
|
|
|
|
) {
|
|
|
|
const { slug } = params;
|
|
|
|
const filePath = path.join(pagesDir, `${slug}.mdx`);
|
2024-07-20 21:04:38 +02:00
|
|
|
|
|
|
|
try {
|
2024-07-20 21:37:40 +02:00
|
|
|
await fs.unlink(filePath);
|
|
|
|
return NextResponse.json({}, { status: 204 });
|
2024-07-20 21:04:38 +02:00
|
|
|
} catch (error) {
|
2024-07-20 21:37:40 +02:00
|
|
|
console.error("Failed to delete page:", error);
|
2024-07-20 21:04:38 +02:00
|
|
|
return NextResponse.json(
|
2024-07-20 21:37:40 +02:00
|
|
|
{ error: "Failed to delete page" },
|
2024-07-20 21:04:38 +02:00
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|