Enhance Admin page with post editing functionality; implement PUT API for saving post edits, including frontmatter parsing and error handling. Update welcome post metadata and improve UI for editing posts.

This commit is contained in:
2025-06-19 13:53:32 +02:00
parent 60b66ef57c
commit 1b77b028d0
4 changed files with 126 additions and 15 deletions

View File

@@ -0,0 +1,22 @@
import { NextRequest, NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';
const postsDirectory = path.join(process.cwd(), 'posts');
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const slug = searchParams.get('slug');
const folderPath = searchParams.get('path') || '';
if (!slug) {
return NextResponse.json({ error: 'Missing slug' }, { status: 400 });
}
const filePath = folderPath && folderPath.trim() !== ''
? path.join(postsDirectory, folderPath, `${slug}.md`)
: path.join(postsDirectory, `${slug}.md`);
if (!fs.existsSync(filePath)) {
return NextResponse.json({ error: 'File does not exist' }, { status: 404 });
}
const content = fs.readFileSync(filePath, 'utf8');
return new NextResponse(content, { status: 200 });
}

View File

@@ -63,4 +63,26 @@ export async function PATCH(request: Request) {
{ status: 500 }
);
}
}
export async function PUT(request: Request) {
try {
const body = await request.json();
const { slug, path: folderPath, content } = body;
if (!slug || typeof content !== 'string') {
return NextResponse.json({ error: 'Missing slug or content' }, { status: 400 });
}
// Compute file path
const filePath = folderPath && folderPath.trim() !== ''
? path.join(postsDirectory, folderPath, `${slug}.md`)
: path.join(postsDirectory, `${slug}.md`);
if (!fs.existsSync(filePath)) {
return NextResponse.json({ error: 'File does not exist' }, { status: 404 });
}
fs.writeFileSync(filePath, content, 'utf8');
return NextResponse.json({ success: true });
} catch (error) {
console.error('Error editing post:', error);
return NextResponse.json({ error: 'Error editing post' }, { status: 500 });
}
}