Add canvas workflow and harden data import
This commit is contained in:
60
src/app/api/canvas/projects/[id]/route.ts
Executable file
60
src/app/api/canvas/projects/[id]/route.ts
Executable file
@@ -0,0 +1,60 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { deleteCanvasProject, getCanvasProject, updateCanvasProject } from '@/lib/canvas-store';
|
||||
import { getAuthenticatedUserId } from '@/lib/session-auth';
|
||||
import { normalizeCanvasState } from '@/lib/canvas-store';
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
export async function GET(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const userId = await getAuthenticatedUserId(request);
|
||||
if (!userId) return NextResponse.json({ error: '请先登录' }, { status: 401 });
|
||||
|
||||
const { id } = await context.params;
|
||||
const project = await getCanvasProject(userId, id);
|
||||
if (!project) return NextResponse.json({ error: '画布不存在' }, { status: 404 });
|
||||
|
||||
return NextResponse.json({ project });
|
||||
} catch (error) {
|
||||
console.error('[canvas/projects/:id] GET error:', error);
|
||||
return NextResponse.json({ error: '读取画布项目失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const userId = await getAuthenticatedUserId(request);
|
||||
if (!userId) return NextResponse.json({ error: '请先登录' }, { status: 401 });
|
||||
|
||||
const { id } = await context.params;
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const project = await updateCanvasProject(userId, id, {
|
||||
title: typeof body.title === 'string' ? body.title : undefined,
|
||||
state: body.state ? normalizeCanvasState(body.state) : undefined,
|
||||
});
|
||||
|
||||
if (!project) return NextResponse.json({ error: '画布不存在' }, { status: 404 });
|
||||
return NextResponse.json({ project });
|
||||
} catch (error) {
|
||||
console.error('[canvas/projects/:id] PUT error:', error);
|
||||
return NextResponse.json({ error: '保存画布项目失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const userId = await getAuthenticatedUserId(request);
|
||||
if (!userId) return NextResponse.json({ error: '请先登录' }, { status: 401 });
|
||||
|
||||
const { id } = await context.params;
|
||||
const deleted = await deleteCanvasProject(userId, id);
|
||||
if (!deleted) return NextResponse.json({ error: '画布不存在' }, { status: 404 });
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error) {
|
||||
console.error('[canvas/projects/:id] DELETE error:', error);
|
||||
return NextResponse.json({ error: '删除画布项目失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
31
src/app/api/canvas/projects/route.ts
Executable file
31
src/app/api/canvas/projects/route.ts
Executable file
@@ -0,0 +1,31 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createCanvasProject, listCanvasProjects } from '@/lib/canvas-store';
|
||||
import { getAuthenticatedUserId } from '@/lib/session-auth';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const userId = await getAuthenticatedUserId(request);
|
||||
if (!userId) return NextResponse.json({ error: '请先登录' }, { status: 401 });
|
||||
|
||||
const projects = await listCanvasProjects(userId);
|
||||
return NextResponse.json({ projects });
|
||||
} catch (error) {
|
||||
console.error('[canvas/projects] GET error:', error);
|
||||
return NextResponse.json({ error: '读取画布项目失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const userId = await getAuthenticatedUserId(request);
|
||||
if (!userId) return NextResponse.json({ error: '请先登录' }, { status: 401 });
|
||||
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const title = typeof body.title === 'string' ? body.title : '未命名画布';
|
||||
const project = await createCanvasProject(userId, title);
|
||||
return NextResponse.json({ project }, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('[canvas/projects] POST error:', error);
|
||||
return NextResponse.json({ error: '创建画布项目失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user