added CRUD Customers
This commit is contained in:
103
app/api/adherents/[id]/route.ts
Normal file
103
app/api/adherents/[id]/route.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Obtenir un adhérent 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 adherent = await prisma.adherent.findUnique({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
if (!adherent) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Adhérent non trouvé' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(adherent);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération de l\'adhérent:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Mettre à jour un adhérent
|
||||
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, dateNaissance, adresse, email, telephone, situation, prescripteur, facturation, commentaire, telephoneSecondaire, instructions } = body;
|
||||
|
||||
const updateData: any = {};
|
||||
if (nom) updateData.nom = nom;
|
||||
if (prenom) updateData.prenom = prenom;
|
||||
if (dateNaissance) updateData.dateNaissance = new Date(dateNaissance);
|
||||
if (adresse) updateData.adresse = adresse;
|
||||
if (email) updateData.email = email;
|
||||
if (telephone) updateData.telephone = telephone;
|
||||
if (situation !== undefined) updateData.situation = situation || null;
|
||||
if (prescripteur !== undefined) updateData.prescripteur = prescripteur || null;
|
||||
if (facturation !== undefined) updateData.facturation = facturation || null;
|
||||
if (commentaire !== undefined) updateData.commentaire = commentaire || null;
|
||||
if (telephoneSecondaire !== undefined) updateData.telephoneSecondaire = telephoneSecondaire || null;
|
||||
if (instructions !== undefined) updateData.instructions = instructions || null;
|
||||
|
||||
const adherent = await prisma.adherent.update({
|
||||
where: { id: params.id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return NextResponse.json(adherent);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la mise à jour de l\'adhérent:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Supprimer un adhérent
|
||||
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.adherent.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la suppression de l\'adhérent:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
91
app/api/adherents/route.ts
Normal file
91
app/api/adherents/route.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Liste tous les adhérents 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 adhérents (SQLite ne supporte pas bien les recherches complexes)
|
||||
const allAdherents = await prisma.adherent.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
// Filtrer en JavaScript pour la recherche insensible à la casse
|
||||
const adherents = search
|
||||
? allAdherents.filter(
|
||||
(adherent) => {
|
||||
const searchLower = search.toLowerCase();
|
||||
return (
|
||||
adherent.nom.toLowerCase().includes(searchLower) ||
|
||||
adherent.prenom.toLowerCase().includes(searchLower) ||
|
||||
adherent.email.toLowerCase().includes(searchLower) ||
|
||||
adherent.telephone.includes(search) ||
|
||||
adherent.adresse.toLowerCase().includes(searchLower) ||
|
||||
(adherent.telephoneSecondaire && adherent.telephoneSecondaire.includes(search))
|
||||
);
|
||||
}
|
||||
)
|
||||
: allAdherents;
|
||||
|
||||
return NextResponse.json(adherents);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des adhérents:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Créer un nouvel adhérent
|
||||
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, dateNaissance, adresse, email, telephone, situation, prescripteur, facturation, commentaire, telephoneSecondaire, instructions } = body;
|
||||
|
||||
if (!nom || !prenom || !dateNaissance || !adresse || !email || !telephone) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Tous les champs obligatoires sont requis' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const adherent = await prisma.adherent.create({
|
||||
data: {
|
||||
nom,
|
||||
prenom,
|
||||
dateNaissance: new Date(dateNaissance),
|
||||
adresse,
|
||||
email,
|
||||
telephone,
|
||||
situation: situation || null,
|
||||
prescripteur: prescripteur || null,
|
||||
facturation: facturation || null,
|
||||
commentaire: commentaire || null,
|
||||
telephoneSecondaire: telephoneSecondaire || null,
|
||||
instructions: instructions || null,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(adherent, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la création de l\'adhérent:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user