feat: mockup template CRUD API routes
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { NextRequest } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import { listTemplates, saveTemplate } from '@/lib/mockupTemplates'
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return Response.json({ error: 'Non authentifié' }, { status: 401 })
|
||||
const templates = await listTemplates()
|
||||
return Response.json(templates)
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return Response.json({ error: 'Non authentifié' }, { status: 401 })
|
||||
|
||||
const form = await req.formData()
|
||||
const name = form.get('name') as string
|
||||
const family = form.get('family') as string
|
||||
const description = (form.get('description') as string) ?? ''
|
||||
const imageFile = form.get('image') as File
|
||||
|
||||
if (!name || !family || !imageFile) {
|
||||
return Response.json({ error: 'name, family et image requis' }, { status: 400 })
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(await imageFile.arrayBuffer())
|
||||
const ext = imageFile.name.split('.').pop() ?? 'jpg'
|
||||
const template = await saveTemplate({ name, family, description, filename: '' }, buffer, ext)
|
||||
return Response.json(template)
|
||||
}
|
||||
Reference in New Issue
Block a user