Files
afrov2/app/api/blog/route.ts
streaper2 5f421b418e
Some checks failed
Build and Push App / build (push) Failing after 11m28s
Maj taxonomies
maj url photo
+ page 404
+ gestion programmation d'article
2026-05-03 21:22:12 +02:00

38 lines
1.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { generateSlug } from '@/lib/utils'
// GET /api/blog
export async function GET() {
try {
const posts = await prisma.blogPost.findMany({
where: {
status: 'PUBLISHED',
publishedAt: {
lte: new Date()
}
},
orderBy: { date: 'desc' },
})
return NextResponse.json(posts)
} catch (error) {
console.error('GET /api/blog error:', error)
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
}
}
// POST /api/blog
export async function POST(request: NextRequest) {
try {
const body = await request.json()
if (!body.slug && body.title) {
body.slug = generateSlug(body.title)
}
const post = await prisma.blogPost.create({ data: body })
return NextResponse.json(post, { status: 201 })
} catch (error) {
console.error('POST /api/blog error:', error)
return NextResponse.json({ error: 'Erreur lors de la création' }, { status: 500 })
}
}