feat: mockup template CRUD API routes
This commit is contained in:
@@ -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 })
|
||||
}
|
||||
}
|
||||
@@ -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