save avant prochaine modif
This commit is contained in:
49
app/api/businesses/[id]/route.ts
Normal file
49
app/api/businesses/[id]/route.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '../../../../lib/prisma'
|
||||
|
||||
type Params = { params: Promise<{ id: string }> }
|
||||
|
||||
// GET /api/businesses/[id]
|
||||
export async function GET(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params
|
||||
const business = await prisma.business.findUnique({
|
||||
where: { id },
|
||||
include: { owner: true, offers: true },
|
||||
})
|
||||
if (!business) return NextResponse.json({ error: 'Non trouvé' }, { status: 404 })
|
||||
return NextResponse.json(business)
|
||||
} catch (error) {
|
||||
console.error('GET /api/businesses/[id] error:', error)
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH /api/businesses/[id]
|
||||
export async function PATCH(request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params
|
||||
const body = await request.json()
|
||||
const business = await prisma.business.update({
|
||||
where: { id },
|
||||
data: body,
|
||||
include: { owner: true, offers: true },
|
||||
})
|
||||
return NextResponse.json(business)
|
||||
} catch (error) {
|
||||
console.error('PATCH /api/businesses/[id] error:', error)
|
||||
return NextResponse.json({ error: 'Erreur lors de la mise à jour' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/businesses/[id]
|
||||
export async function DELETE(_request: NextRequest, { params }: Params) {
|
||||
try {
|
||||
const { id } = await params
|
||||
await prisma.business.delete({ where: { id } })
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error('DELETE /api/businesses/[id] error:', error)
|
||||
return NextResponse.json({ error: 'Erreur lors de la suppression' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
49
app/api/businesses/route.ts
Normal file
49
app/api/businesses/route.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '../../../lib/prisma'
|
||||
|
||||
// GET /api/businesses — List all businesses
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const category = searchParams.get('category')
|
||||
const q = searchParams.get('q')
|
||||
const featured = searchParams.get('featured')
|
||||
|
||||
const where: Record<string, unknown> = {}
|
||||
if (category && category !== 'All') where.category = category
|
||||
if (featured === 'true') where.isFeatured = true
|
||||
if (q) {
|
||||
where.OR = [
|
||||
{ name: { contains: q, mode: 'insensitive' } },
|
||||
{ description: { contains: q, mode: 'insensitive' } },
|
||||
{ tags: { hasSome: [q] } },
|
||||
]
|
||||
}
|
||||
|
||||
const businesses = await prisma.business.findMany({
|
||||
where,
|
||||
include: { owner: true, offers: true },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})
|
||||
|
||||
return NextResponse.json(businesses)
|
||||
} catch (error) {
|
||||
console.error('GET /api/businesses error:', error)
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/businesses — Create a business
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const business = await prisma.business.create({
|
||||
data: body,
|
||||
include: { owner: true },
|
||||
})
|
||||
return NextResponse.json(business, { status: 201 })
|
||||
} catch (error) {
|
||||
console.error('POST /api/businesses error:', error)
|
||||
return NextResponse.json({ error: 'Erreur lors de la création' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user