Ajoute la base organisable par carnet (schéma, champs partagés, valeurs par note) avec activation guidée, tableau éditable, kanban et suppression de colonnes. Corrige le multiselect en vue tableau et enrichit sidebar, grille et i18n FR/EN. Inclut aussi les améliorations flashcards SM-2, l'audit consentement IA et la robustesse du serveur MCP (config, validation, rate-limit, métriques). Co-authored-by: Cursor <cursoragent@cursor.com>
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import prisma from '@/lib/prisma'
|
|
|
|
/**
|
|
* PATCH /api/flashcards/[id]
|
|
* Update front and/or back of a flashcard.
|
|
*/
|
|
export async function PATCH(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> },
|
|
) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { id: cardId } = await params
|
|
const body = await request.json()
|
|
const front = typeof body.front === 'string' ? body.front.trim() : undefined
|
|
const back = typeof body.back === 'string' ? body.back.trim() : undefined
|
|
|
|
if (!front && !back) {
|
|
return NextResponse.json({ error: 'Nothing to update' }, { status: 400 })
|
|
}
|
|
|
|
const card = await prisma.flashcard.findFirst({
|
|
where: { id: cardId, deck: { userId: session.user.id } },
|
|
})
|
|
if (!card) {
|
|
return NextResponse.json({ error: 'Card not found' }, { status: 404 })
|
|
}
|
|
|
|
const updated = await prisma.flashcard.update({
|
|
where: { id: cardId },
|
|
data: {
|
|
...(front !== undefined && { front }),
|
|
...(back !== undefined && { back }),
|
|
},
|
|
})
|
|
|
|
return NextResponse.json({ card: { id: updated.id, front: updated.front, back: updated.back } })
|
|
} catch (error) {
|
|
console.error('[flashcards/[id] PATCH]', error)
|
|
return NextResponse.json({ error: 'Failed to update card' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/flashcards/[id]
|
|
* Delete a single flashcard.
|
|
*/
|
|
export async function DELETE(
|
|
_request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> },
|
|
) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { id: cardId } = await params
|
|
|
|
const card = await prisma.flashcard.findFirst({
|
|
where: { id: cardId, deck: { userId: session.user.id } },
|
|
})
|
|
if (!card) {
|
|
return NextResponse.json({ error: 'Card not found' }, { status: 404 })
|
|
}
|
|
|
|
await prisma.flashcard.delete({ where: { id: cardId } })
|
|
|
|
return NextResponse.json({ ok: true })
|
|
} catch (error) {
|
|
console.error('[flashcards/[id] DELETE]', error)
|
|
return NextResponse.json({ error: 'Failed to delete card' }, { status: 500 })
|
|
}
|
|
}
|