ajout de la gestion des favoris
All checks were successful
Build and Push App / build (push) Successful in 13m38s

This commit is contained in:
2026-04-27 21:44:48 +02:00
parent 38caf36bee
commit a814c7b577
11 changed files with 423 additions and 8 deletions

View File

@@ -0,0 +1,76 @@
import { NextRequest, NextResponse } from 'next/server';
import prisma from '@/lib/prisma';
// GET /api/favorites - Get user's favorites
export async function GET(request: NextRequest) {
try {
const userId = request.headers.get('x-user-id');
if (!userId) {
return NextResponse.json({ error: 'Utilisateur non identifié' }, { status: 401 });
}
const favorites = await prisma.favorite.findMany({
where: { userId },
include: {
business: {
include: {
categoryRef: true,
country: true
}
}
},
orderBy: { createdAt: 'desc' }
});
return NextResponse.json(favorites);
} catch (error) {
console.error('GET /api/favorites error:', error);
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
}
}
// POST /api/favorites - Toggle favorite
export async function POST(request: NextRequest) {
try {
const userId = request.headers.get('x-user-id');
const body = await request.json();
const { businessId } = body;
if (!userId) {
return NextResponse.json({ error: 'Utilisateur non identifié' }, { status: 401 });
}
if (!businessId) {
return NextResponse.json({ error: 'businessId requis' }, { status: 400 });
}
// Check if already favorited
const existing = await prisma.favorite.findUnique({
where: {
userId_businessId: {
userId,
businessId
}
}
});
if (existing) {
// Unfavorite
await prisma.favorite.delete({
where: { id: existing.id }
});
return NextResponse.json({ favorited: false });
} else {
// Favorite
await prisma.favorite.create({
data: {
userId,
businessId
}
});
return NextResponse.json({ favorited: true });
}
} catch (error) {
console.error('POST /api/favorites error:', error);
return NextResponse.json({ error: 'Erreur lors du changement de favori' }, { status: 500 });
}
}