Files
Momento/memento-note/app/api/notes/publish/route.ts
Antigravity 1d614dd9c0
Some checks failed
CI / Lint, Unit Tests & Build (push) Successful in 5m0s
CI / Deploy production (on server) (push) Failing after 40s
feat: Publication de pages — notes publiques sur URL
- Migration: champs isPublic + publicSlug + publishedAt sur Note
- Route publique /p/[slug] — rendu SSR sans auth, prose styled
- Server actions: publishNote / unpublishNote / getPublishedNote
- API /api/notes/publish — toggle publication + génération slug
- PublishDialog — modal avec lien copiable + bouton dépublier
- Bouton Globe dans le toolbar (vert si publiée)
- i18n FR/EN
- Pattern inspiré de BrainstormSession.isPublic
2026-06-19 22:03:53 +00:00

53 lines
1.7 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import prisma from '@/lib/prisma'
function generateSlug(title: string): string {
const base = title
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.slice(0, 60) || 'note'
return `${base}-${Math.random().toString(36).slice(2, 8)}`
}
export async function POST(request: NextRequest) {
const session = await auth()
if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
const { noteId, action } = await request.json()
if (!noteId) return NextResponse.json({ error: 'noteId required' }, { status: 400 })
const note = await prisma.note.findFirst({
where: { id: noteId, userId: session.user.id },
select: { id: true, title: true, publicSlug: true },
})
if (!note) return NextResponse.json({ error: 'Not found' }, { status: 404 })
if (action === 'publish') {
let slug = note.publicSlug
if (!slug) {
slug = generateSlug(note.title || 'note')
const existing = await prisma.note.findUnique({ where: { publicSlug: slug } })
if (existing && existing.id !== noteId) slug = `${slug}-${Date.now().toString(36)}`
}
await prisma.note.update({
where: { id: noteId },
data: { isPublic: true, publicSlug: slug, publishedAt: new Date() },
})
return NextResponse.json({ success: true, slug })
}
if (action === 'unpublish') {
await prisma.note.update({
where: { id: noteId },
data: { isPublic: false, publicSlug: null, publishedAt: null },
})
return NextResponse.json({ success: true })
}
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
}