65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|||
|
|
import { getPost, updatePost } from "@/lib/posts";
|
||
|
|
import { moveToRecycle } from "@/lib/recycle";
|
||
|
|
|
||
|
|
export async function GET(
|
||
|
|
request: NextRequest,
|
||
|
|
{ params }: { params: Promise<{ slug: string }> }
|
||
|
|
) {
|
||
|
|
try {
|
||
|
|
const { slug } = await params;
|
||
|
|
const post = getPost(slug);
|
||
|
|
if (!post) {
|
||
|
|
return NextResponse.json({ error: "Post not found" }, { status: 404 });
|
||
|
|
}
|
||
|
|
return NextResponse.json(post);
|
||
|
|
} catch (error: unknown) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: error instanceof Error ? error.message : "Unknown error" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function PUT(
|
||
|
|
request: NextRequest,
|
||
|
|
{ params }: { params: Promise<{ slug: string }> }
|
||
|
|
) {
|
||
|
|
try {
|
||
|
|
const { slug } = await params;
|
||
|
|
const body = await request.json();
|
||
|
|
const { frontMatter, content } = body;
|
||
|
|
|
||
|
|
const result = updatePost(slug, frontMatter || {}, content);
|
||
|
|
if (!result.success) {
|
||
|
|
return NextResponse.json({ error: "Post not found" }, { status: 404 });
|
||
|
|
}
|
||
|
|
return NextResponse.json({ success: true, dirPath: result.dirPath });
|
||
|
|
} catch (error: unknown) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: error instanceof Error ? error.message : "Unknown error" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function DELETE(
|
||
|
|
request: NextRequest,
|
||
|
|
{ params }: { params: Promise<{ slug: string }> }
|
||
|
|
) {
|
||
|
|
try {
|
||
|
|
const { slug } = await params;
|
||
|
|
const post = getPost(slug);
|
||
|
|
if (!post) {
|
||
|
|
return NextResponse.json({ error: "Post not found" }, { status: 404 });
|
||
|
|
}
|
||
|
|
|
||
|
|
moveToRecycle(slug, post.dirPath, post.frontMatter.title);
|
||
|
|
return NextResponse.json({ success: true });
|
||
|
|
} catch (error: unknown) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: error instanceof Error ? error.message : "Unknown error" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|