Files
Momento/memento-note/app/api/notebooks/[id]/route.ts
Antigravity 916fb78dfb
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 2m9s
feat: hierarchical notebook system - trash, selectors, breadcrumb, sidebar tree
- Schema: soft delete with trashedAt on Notebook model
- API: PATCH/GET notebooks support trashedAt filtering with cascade
- Sidebar: recursive tree rendering with collapse/expand, visual guides, hover actions
- HierarchicalNotebookSelector: portal-based dropdown with search, breadcrumbs, dropUp support
- AI chat: context selector with Toutes mes notes + notebook selector
- Agent detail: flat selects replaced with HierarchicalNotebookSelector
- Breadcrumb: notebook path display on home page
- Trash view: card grid with countdown, restore/permanent delete
- CSS: design tokens (ink, paper, blueprint, concrete, etc.)
- Types: parentId, trashedAt added to Notebook interface
2026-05-10 10:52:26 +00:00

132 lines
3.4 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { auth } from '@/auth'
import { revalidatePath } from 'next/cache'
async function getDescendantIds(notebookId: string): Promise<string[]> {
const ids: string[] = []
const children = await prisma.notebook.findMany({
where: { parentId: notebookId },
select: { id: true },
})
for (const child of children) {
ids.push(child.id)
ids.push(...await getDescendantIds(child.id))
}
return ids
}
export async function PATCH(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
try {
const { id } = await params
const body = await request.json()
const { name, icon, color, order, trashedAt } = body
const existing = await prisma.notebook.findUnique({
where: { id },
select: { userId: true }
})
if (!existing) {
return NextResponse.json(
{ success: false, error: 'Notebook not found' },
{ status: 404 }
)
}
if (existing.userId !== session.user.id) {
return NextResponse.json(
{ success: false, error: 'Forbidden' },
{ status: 403 }
)
}
const updateData: any = {}
if (name !== undefined) updateData.name = name.trim()
if (icon !== undefined) updateData.icon = icon
if (color !== undefined) updateData.color = color
if (order !== undefined) updateData.order = order
if (trashedAt !== undefined) updateData.trashedAt = trashedAt
if (trashedAt !== undefined) {
const descendantIds = await getDescendantIds(id)
const allIds = [id, ...descendantIds]
await prisma.notebook.updateMany({
where: { id: { in: allIds }, userId: session.user.id },
data: { trashedAt },
})
} else {
await prisma.notebook.update({
where: { id },
data: updateData,
})
}
try { revalidatePath('/') } catch {}
return NextResponse.json({ success: true })
} catch (error) {
console.error('Error updating notebook:', error)
return NextResponse.json(
{ success: false, error: 'Failed to update notebook' },
{ status: 500 }
)
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
try {
const { id } = await params
const notebook = await prisma.notebook.findUnique({
where: { id },
select: { userId: true, name: true }
})
if (!notebook) {
return NextResponse.json(
{ success: false, error: 'Notebook not found' },
{ status: 404 }
)
}
if (notebook.userId !== session.user.id) {
return NextResponse.json(
{ success: false, error: 'Forbidden' },
{ status: 403 }
)
}
await prisma.notebook.delete({ where: { id } })
try { revalidatePath('/') } catch {}
return NextResponse.json({
success: true,
message: `Notebook "${notebook.name}" permanently deleted`,
})
} catch (error) {
console.error('Error deleting notebook:', error)
return NextResponse.json(
{ success: false, error: 'Failed to delete notebook' },
{ status: 500 }
)
}
}