Added Pro Page
This commit is contained in:
97
app/api/univers-pro/[id]/route.ts
Normal file
97
app/api/univers-pro/[id]/route.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Obtenir un contact par ID
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const contact = await prisma.universPro.findUnique({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Contact non trouvé' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(contact);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération du contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Mettre à jour un contact
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
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;
|
||||
|
||||
const updateData: any = {};
|
||||
if (nom) updateData.nom = nom;
|
||||
if (prenom) updateData.prenom = prenom;
|
||||
if (telephone) updateData.telephone = telephone;
|
||||
if (email) updateData.email = email;
|
||||
if (adresse) updateData.adresse = adresse;
|
||||
if (nomEntreprise) updateData.nomEntreprise = nomEntreprise;
|
||||
|
||||
const contact = await prisma.universPro.update({
|
||||
where: { id: params.id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return NextResponse.json(contact);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la mise à jour du contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Supprimer un contact
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
await prisma.universPro.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la suppression du contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
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