feat: mockup template CRUD API routes

This commit is contained in:
2026-06-22 12:10:26 +02:00
parent 01e2fcd5e5
commit 1ef69dc205
3 changed files with 55 additions and 0 deletions
@@ -0,0 +1,10 @@
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { deleteTemplate } from '@/lib/mockupTemplates'
export async function DELETE(_req: Request, { params }: { params: { id: string } }) {
const session = await getServerSession(authOptions)
if (!session) return Response.json({ error: 'Non authentifié' }, { status: 401 })
await deleteTemplate(params.id)
return Response.json({ ok: true })
}
@@ -0,0 +1,14 @@
import { readFile } from 'fs/promises'
import { getTemplateImagePath } from '@/lib/mockupTemplates'
export async function GET(_req: Request, { params }: { params: { filename: string } }) {
try {
const filePath = getTemplateImagePath(params.filename)
const buffer = await readFile(filePath)
const ext = params.filename.split('.').pop() ?? 'jpg'
const contentType = ext === 'png' ? 'image/png' : 'image/jpeg'
return new Response(buffer, { headers: { 'Content-Type': contentType } })
} catch {
return new Response('Not found', { status: 404 })
}
}
+31
View File
@@ -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)
}