61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
import type { Prisma } from '@prisma/client';
|
|
|
|
export async function GET(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const id = (await params).id;
|
|
try {
|
|
const page = await prisma.page.findUnique({
|
|
where: { id },
|
|
});
|
|
if (!page) return NextResponse.json({ error: 'Page not found' }, { status: 404 });
|
|
return NextResponse.json({ ...page, tags: JSON.parse(page.tags || "[]") });
|
|
} catch {
|
|
return NextResponse.json({ error: 'Error fetching page' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PUT(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const id = (await params).id;
|
|
try {
|
|
const body = await request.json();
|
|
// Separate update logic for flexibility (e.g. only updating title)
|
|
const updateData: Prisma.PageUncheckedUpdateInput = {};
|
|
if (body.title !== undefined) updateData.title = body.title;
|
|
if (body.content !== undefined) updateData.content = body.content;
|
|
if (body.parentId !== undefined) updateData.parentId = body.parentId;
|
|
if (body.tags !== undefined) updateData.tags = JSON.stringify(body.tags);
|
|
if (body.icon !== undefined) updateData.icon = body.icon;
|
|
if (body.isLocked !== undefined) updateData.isLocked = body.isLocked;
|
|
|
|
const page = await prisma.page.update({
|
|
where: { id },
|
|
data: updateData,
|
|
});
|
|
return NextResponse.json({ ...page, tags: JSON.parse(page.tags || "[]") });
|
|
} catch {
|
|
return NextResponse.json({ error: 'Error updating page' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const id = (await params).id;
|
|
try {
|
|
await prisma.page.delete({
|
|
where: { id },
|
|
});
|
|
return NextResponse.json({ success: true });
|
|
} catch {
|
|
return NextResponse.json({ error: 'Error deleting page' }, { status: 500 });
|
|
}
|
|
}
|