feat: MVP-2 completion - search, quick add, backlinks, guided forms
## Search & Retrieval - Improved search ranking with scoring (title match, favorites, recency) - Highlight matches with excerpt extraction - Fuzzy search with string-similarity - Unified noteQuery function ## Quick Capture - Quick Add API (POST /api/notes/quick) with type prefixes - Quick add parser with tag extraction - Global Quick Add UI (Ctrl+N shortcut) - Tag autocomplete in forms ## Note Relations - Automatic backlinks with sync on create/update/delete - Backlinks API (GET /api/notes/[id]/backlinks) - Related notes with scoring and reasons ## Guided Forms - Type-specific form fields (command, snippet, decision, recipe, procedure, inventory) - Serialization to/from markdown - Tag suggestions based on content (GET /api/tags/suggest) ## UX by Type - Command: Copy button for code blocks - Snippet: Syntax highlighting with react-syntax-highlighter - Procedure: Interactive checkboxes ## Quality - Standardized error handling across all APIs - Integration tests (28 tests passing) - Unit tests for search, tags, quick-add Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,121 +1,138 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { NextRequest } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { noteSchema } from '@/lib/validators'
|
||||
import { noteSchema, NoteInput } from '@/lib/validators'
|
||||
import { createErrorResponse, createSuccessResponse, ValidationError } from '@/lib/errors'
|
||||
import { syncBacklinks } from '@/lib/backlinks'
|
||||
|
||||
export async function GET() {
|
||||
const notes = await prisma.note.findMany({
|
||||
include: { tags: { include: { tag: true } } },
|
||||
})
|
||||
try {
|
||||
const notes = await prisma.note.findMany({
|
||||
include: { tags: { include: { tag: true } } },
|
||||
})
|
||||
|
||||
const exportData = notes.map(note => ({
|
||||
...note,
|
||||
tags: note.tags.map(nt => nt.tag.name),
|
||||
createdAt: note.createdAt.toISOString(),
|
||||
updatedAt: note.updatedAt.toISOString(),
|
||||
}))
|
||||
const exportData = notes.map(note => ({
|
||||
...note,
|
||||
tags: note.tags.map(nt => nt.tag.name),
|
||||
createdAt: note.createdAt.toISOString(),
|
||||
updatedAt: note.updatedAt.toISOString(),
|
||||
}))
|
||||
|
||||
return NextResponse.json(exportData, { status: 200 })
|
||||
return createSuccessResponse(exportData)
|
||||
} catch (error) {
|
||||
return createErrorResponse(error)
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.json()
|
||||
try {
|
||||
const body = await req.json()
|
||||
|
||||
if (!Array.isArray(body)) {
|
||||
return NextResponse.json({ error: 'Invalid format: expected array' }, { status: 400 })
|
||||
}
|
||||
|
||||
const importedNotes: Array<{ id?: string; title: string }> = []
|
||||
const errors: string[] = []
|
||||
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
const result = noteSchema.safeParse(body[i])
|
||||
if (!result.success) {
|
||||
errors.push(`Item ${i}: ${result.error.issues.map(e => e.message).join(', ')}`)
|
||||
continue
|
||||
if (!Array.isArray(body)) {
|
||||
throw new ValidationError([{ path: 'body', message: 'Invalid format: expected array' }])
|
||||
}
|
||||
importedNotes.push(result.data)
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
return NextResponse.json({ error: 'Validation failed', details: errors }, { status: 400 })
|
||||
}
|
||||
const importedNotes: NoteInput[] = []
|
||||
const errors: string[] = []
|
||||
|
||||
const parseDate = (dateStr: string | undefined): Date => {
|
||||
if (!dateStr) return new Date()
|
||||
const parsed = new Date(dateStr)
|
||||
return isNaN(parsed.getTime()) ? new Date() : parsed
|
||||
}
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
const result = noteSchema.safeParse(body[i])
|
||||
if (!result.success) {
|
||||
errors.push(`Item ${i}: ${result.error.issues.map(e => e.message).join(', ')}`)
|
||||
continue
|
||||
}
|
||||
importedNotes.push(result.data)
|
||||
}
|
||||
|
||||
let processed = 0
|
||||
if (errors.length > 0) {
|
||||
throw new ValidationError(errors)
|
||||
}
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
for (const item of importedNotes) {
|
||||
const tags = item.tags || []
|
||||
const { tags: _, ...noteData } = item
|
||||
const parseDate = (dateStr: string | undefined): Date => {
|
||||
if (!dateStr) return new Date()
|
||||
const parsed = new Date(dateStr)
|
||||
return isNaN(parsed.getTime()) ? new Date() : parsed
|
||||
}
|
||||
|
||||
const createdAt = parseDate((item as { createdAt?: string }).createdAt)
|
||||
const updatedAt = parseDate((item as { updatedAt?: string }).updatedAt)
|
||||
let processed = 0
|
||||
|
||||
if (item.id) {
|
||||
const existing = await tx.note.findUnique({ where: { id: item.id } })
|
||||
if (existing) {
|
||||
await tx.note.update({
|
||||
where: { id: item.id },
|
||||
data: { ...noteData, createdAt, updatedAt },
|
||||
})
|
||||
await tx.noteTag.deleteMany({ where: { noteId: item.id } })
|
||||
processed++
|
||||
await prisma.$transaction(async (tx) => {
|
||||
for (const item of importedNotes) {
|
||||
const tags = item.tags || []
|
||||
const { tags: _, ...noteData } = item
|
||||
|
||||
const createdAt = parseDate((item as { createdAt?: string }).createdAt)
|
||||
const updatedAt = parseDate((item as { updatedAt?: string }).updatedAt)
|
||||
|
||||
if (item.id) {
|
||||
const existing = await tx.note.findUnique({ where: { id: item.id } })
|
||||
if (existing) {
|
||||
await tx.note.update({
|
||||
where: { id: item.id },
|
||||
data: { ...noteData, createdAt, updatedAt },
|
||||
})
|
||||
await tx.noteTag.deleteMany({ where: { noteId: item.id } })
|
||||
processed++
|
||||
} else {
|
||||
await tx.note.create({
|
||||
data: {
|
||||
...noteData,
|
||||
id: item.id,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
},
|
||||
})
|
||||
processed++
|
||||
}
|
||||
} else {
|
||||
await tx.note.create({
|
||||
data: {
|
||||
...noteData,
|
||||
id: item.id,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
},
|
||||
const existingByTitle = await tx.note.findFirst({
|
||||
where: { title: item.title },
|
||||
})
|
||||
if (existingByTitle) {
|
||||
await tx.note.update({
|
||||
where: { id: existingByTitle.id },
|
||||
data: { ...noteData, updatedAt },
|
||||
})
|
||||
await tx.noteTag.deleteMany({ where: { noteId: existingByTitle.id } })
|
||||
} else {
|
||||
await tx.note.create({
|
||||
data: {
|
||||
...noteData,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
processed++
|
||||
}
|
||||
} else {
|
||||
const existingByTitle = await tx.note.findFirst({
|
||||
where: { title: item.title },
|
||||
})
|
||||
if (existingByTitle) {
|
||||
await tx.note.update({
|
||||
where: { id: existingByTitle.id },
|
||||
data: { ...noteData, updatedAt },
|
||||
})
|
||||
await tx.noteTag.deleteMany({ where: { noteId: existingByTitle.id } })
|
||||
} else {
|
||||
await tx.note.create({
|
||||
data: {
|
||||
...noteData,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
},
|
||||
})
|
||||
|
||||
const noteId = item.id
|
||||
? (await tx.note.findUnique({ where: { id: item.id } }))?.id
|
||||
: (await tx.note.findFirst({ where: { title: item.title } }))?.id
|
||||
|
||||
if (noteId && tags.length > 0) {
|
||||
for (const tagName of tags) {
|
||||
const tag = await tx.tag.upsert({
|
||||
where: { name: tagName },
|
||||
create: { name: tagName },
|
||||
update: {},
|
||||
})
|
||||
await tx.noteTag.create({
|
||||
data: { noteId, tagId: tag.id },
|
||||
})
|
||||
}
|
||||
}
|
||||
processed++
|
||||
}
|
||||
|
||||
const noteId = item.id
|
||||
? (await tx.note.findUnique({ where: { id: item.id } }))?.id
|
||||
: (await tx.note.findFirst({ where: { title: item.title } }))?.id
|
||||
|
||||
if (noteId && tags.length > 0) {
|
||||
for (const tagName of tags) {
|
||||
const tag = await tx.tag.upsert({
|
||||
where: { name: tagName },
|
||||
create: { name: tagName },
|
||||
update: {},
|
||||
})
|
||||
await tx.noteTag.create({
|
||||
data: { noteId, tagId: tag.id },
|
||||
})
|
||||
if (noteId) {
|
||||
const note = await tx.note.findUnique({ where: { id: noteId } })
|
||||
if (note) {
|
||||
await syncBacklinks(note.id, note.content)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, count: processed }, { status: 201 })
|
||||
return createSuccessResponse({ success: true, count: processed }, 201)
|
||||
} catch (error) {
|
||||
return createErrorResponse(error)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user