- GET /api/notes: 获取用户笔记列表,支持搜索和筛选 - POST /api/notes: 创建新笔记 - PUT /api/notes/[noteId]: 更新笔记内容 - DELETE /api/notes/[noteId]: 删除笔记 - 支持置顶和归档操作
161 lines
3.8 KiB
TypeScript
161 lines
3.8 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { db } from '@/drizzle/db';
|
|
import { notes } from '@/drizzle/schema';
|
|
import { eq, and } from 'drizzle-orm';
|
|
import { getCurrentUser } from '@/lib/auth';
|
|
|
|
// GET /api/notes/[noteId] - 获取单个笔记
|
|
export async function GET(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ noteId: string }> }
|
|
) {
|
|
try {
|
|
const { noteId } = await params;
|
|
|
|
// 获取当前用户
|
|
const user = await getCurrentUser();
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{ error: '未登录' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const note = await db.query.notes.findFirst({
|
|
where: and(
|
|
eq(notes.noteId, noteId),
|
|
eq(notes.userId, user.userId)
|
|
),
|
|
});
|
|
|
|
if (!note) {
|
|
return NextResponse.json(
|
|
{ error: '笔记不存在' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ note });
|
|
} catch (error) {
|
|
console.error('Failed to get note:', error);
|
|
return NextResponse.json(
|
|
{ error: '获取笔记失败' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// PUT /api/notes/[noteId] - 更新笔记
|
|
export async function PUT(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ noteId: string }> }
|
|
) {
|
|
try {
|
|
const { noteId } = await params;
|
|
|
|
// 获取当前用户
|
|
const user = await getCurrentUser();
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{ error: '未登录' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// 验证笔记存在且属于当前用户
|
|
const existingNote = await db.query.notes.findFirst({
|
|
where: and(
|
|
eq(notes.noteId, noteId),
|
|
eq(notes.userId, user.userId)
|
|
),
|
|
});
|
|
|
|
if (!existingNote) {
|
|
return NextResponse.json(
|
|
{ error: '笔记不存在' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { title, content, tags, isPinned, isArchived } = body;
|
|
|
|
// 构建更新数据
|
|
const updateData: Record<string, unknown> = {
|
|
updatedAt: new Date(),
|
|
};
|
|
|
|
if (title !== undefined) updateData.title = title.trim();
|
|
if (content !== undefined) updateData.content = content.trim();
|
|
if (tags !== undefined) updateData.tags = tags;
|
|
if (isPinned !== undefined) updateData.isPinned = isPinned;
|
|
if (isArchived !== undefined) updateData.isArchived = isArchived;
|
|
|
|
const [updatedNote] = await db
|
|
.update(notes)
|
|
.set(updateData)
|
|
.where(and(
|
|
eq(notes.noteId, noteId),
|
|
eq(notes.userId, user.userId)
|
|
))
|
|
.returning();
|
|
|
|
return NextResponse.json({ note: updatedNote });
|
|
} catch (error) {
|
|
console.error('Failed to update note:', error);
|
|
return NextResponse.json(
|
|
{ error: '更新笔记失败' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// DELETE /api/notes/[noteId] - 删除笔记
|
|
export async function DELETE(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ noteId: string }> }
|
|
) {
|
|
try {
|
|
const { noteId } = await params;
|
|
|
|
// 获取当前用户
|
|
const user = await getCurrentUser();
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{ error: '未登录' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// 验证笔记存在且属于当前用户
|
|
const existingNote = await db.query.notes.findFirst({
|
|
where: and(
|
|
eq(notes.noteId, noteId),
|
|
eq(notes.userId, user.userId)
|
|
),
|
|
});
|
|
|
|
if (!existingNote) {
|
|
return NextResponse.json(
|
|
{ error: '笔记不存在' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
await db
|
|
.delete(notes)
|
|
.where(and(
|
|
eq(notes.noteId, noteId),
|
|
eq(notes.userId, user.userId)
|
|
));
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('Failed to delete note:', error);
|
|
return NextResponse.json(
|
|
{ error: '删除笔记失败' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|