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

@@ -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 });
}
}