Added Pro Page
This commit is contained in:
85
app/api/univers-pro/route.ts
Normal file
85
app/api/univers-pro/route.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Liste tous les contacts univers pro avec recherche
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const search = searchParams.get('search') || '';
|
||||
|
||||
// Récupérer tous les contacts (SQLite ne supporte pas bien les recherches complexes)
|
||||
const allContacts = await prisma.universPro.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
// Filtrer en JavaScript pour la recherche insensible à la casse
|
||||
const contacts = search
|
||||
? allContacts.filter(
|
||||
(contact) => {
|
||||
const searchLower = search.toLowerCase();
|
||||
return (
|
||||
contact.nom.toLowerCase().includes(searchLower) ||
|
||||
contact.prenom.toLowerCase().includes(searchLower) ||
|
||||
contact.email.toLowerCase().includes(searchLower) ||
|
||||
contact.telephone.includes(search) ||
|
||||
contact.adresse.toLowerCase().includes(searchLower) ||
|
||||
contact.nomEntreprise.toLowerCase().includes(searchLower)
|
||||
);
|
||||
}
|
||||
)
|
||||
: allContacts;
|
||||
|
||||
return NextResponse.json(contacts);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des contacts:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Créer un nouveau contact
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { nom, prenom, telephone, email, adresse, nomEntreprise } = body;
|
||||
|
||||
if (!nom || !prenom || !telephone || !email || !adresse || !nomEntreprise) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Tous les champs sont requis' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const contact = await prisma.universPro.create({
|
||||
data: {
|
||||
nom,
|
||||
prenom,
|
||||
telephone,
|
||||
email,
|
||||
adresse,
|
||||
nomEntreprise,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(contact, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la création du contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user