首次发布git
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const pages = await prisma.page.findMany({
|
||||
orderBy: [{ order: 'asc' }, { createdAt: 'desc' }],
|
||||
});
|
||||
const parsedPages = pages.map(p => ({
|
||||
...p,
|
||||
tags: JSON.parse(p.tags || "[]")
|
||||
}));
|
||||
return NextResponse.json(parsedPages);
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error fetching pages' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { title, content, parentId, type } = body;
|
||||
|
||||
const page = await prisma.page.create({
|
||||
data: {
|
||||
title: title || '无标题',
|
||||
content: content || '',
|
||||
tags: JSON.stringify(body.tags || []),
|
||||
parentId: parentId || null,
|
||||
type: type || 'file',
|
||||
order: await (async () => {
|
||||
if (body.order !== undefined) return body.order;
|
||||
const lastPage = await prisma.page.findFirst({
|
||||
where: { parentId: parentId || null },
|
||||
orderBy: { order: 'desc' },
|
||||
});
|
||||
return (lastPage?.order ?? -1) + 1;
|
||||
})(),
|
||||
icon: body.icon || null,
|
||||
isLocked: body.isLocked || false,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ...page, tags: JSON.parse(page.tags || "[]") });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error creating page' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user