commentaire endpoint

This commit is contained in:
2025-11-05 22:38:55 +01:00
parent 4c0e7df9de
commit 3c32baac57
45 changed files with 475 additions and 479 deletions

View File

@@ -4,32 +4,32 @@ using FastEndpoints;
namespace PyroFetes.Endpoints.Classification;
public class CreateClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateClassificationDto, GetClassificationDto>
public class CreateClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateClassificationDto, GetClassificationDto> //Instanciation d'une connexion à la bdd dans un endpoint, utilise l'élément de requête CreateClassificationDto et l'élement de réponse GetClassificationDto
{
public override void Configure()
public override void Configure() //Configuration de l'endpoint
{
Post("/api/classifications");
AllowAnonymous();
Post("/api/classifications"); //Créer une classification
AllowAnonymous(); //Autorise l'accès sans authentification
}
public override async Task HandleAsync(CreateClassificationDto req, CancellationToken ct)
{
Models.Classification classification = new ()
Models.Classification classification = new () //Création d'un label rentré par l'utilisateur
{
Label = req.Label
};
pyrofetesdbcontext.Classifications.Add(classification);
await pyrofetesdbcontext.SaveChangesAsync(ct);
pyrofetesdbcontext.Classifications.Add(classification); //Ajout de la classification à la bdd
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde de la classification dans la bdd
Console.WriteLine("Classification créée avec succès !");
GetClassificationDto responseDto = new ()
GetClassificationDto responseDto = new () //Renvoie le label de la classification
{
Label = req.Label
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(responseDto, ct); //Réponse au client
}
}

View File

@@ -12,27 +12,27 @@ public class DeleteClassificationEndpoint(PyroFetesDbContext libraryDbContext) :
{
public override void Configure()
{
Delete("/api/classifications/{@id}", x => new { x.Id });
AllowAnonymous();
Delete("/api/classifications/{@id}", x => new { x.Id }); //Supprime une classification en fonction de l'id
AllowAnonymous(); //Autorise l'accès sans authentification
}
public override async Task HandleAsync(DeleteClassificationRequest req, CancellationToken ct)
{
Models.Classification? classificationToDelete = await libraryDbContext
.Classifications
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
Models.Classification? classificationToDelete = await libraryDbContext //Récupère une classification dans la bdd et le stocke dans classificationToDelete
.Classifications //Recherche la classification dans la table Classifications
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); //Recherche une classification dont l'id correspond à req.Id
if (classificationToDelete == null)
{
Console.WriteLine($"Aucune classification avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
return; //Arrêt de la méthode
}
libraryDbContext.Classifications.Remove(classificationToDelete);
await libraryDbContext.SaveChangesAsync(ct);
libraryDbContext.Classifications.Remove(classificationToDelete); //Supprime la classification dans la bdd
await libraryDbContext.SaveChangesAsync(ct); //Sauvegarde de la classification dans la bdd
await Send.NoContentAsync(ct);
await Send.NoContentAsync(ct); //Renvoie une réponse réussite 204
}
}

View File

@@ -8,21 +8,21 @@ public class GetAllClassificationsEndpoint(PyroFetesDbContext pyrofetesdbcontext
{
public override void Configure()
{
Get("/api/classifications");
AllowAnonymous();
Get("/api/classifications"); //Endpoint qui affiche toutes les classifications
AllowAnonymous(); //Autorise l'accès sans authentification
}
public override async Task HandleAsync(CancellationToken ct)
{
List<GetClassificationDto> responseDto = await pyrofetesdbcontext.Classifications
.Select(a => new GetClassificationDto
List<GetClassificationDto> responseDto = await pyrofetesdbcontext.Classifications //Création d'une liste qui récupère toutes les classifications dans la bdd
.Select(a => new GetClassificationDto //Sélectionne dans la liste chaque classification
{
Id = a.Id,
Label = a.Label,
Id = a.Id, //Récupère l'id
Label = a.Label, //Récupère le label
}
).ToListAsync(ct);
).ToListAsync(ct); //Retourne la liste de classification
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
}
}

View File

@@ -13,30 +13,30 @@ public class GetClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext) :E
{
public override void Configure()
{
Get("/api/classifications/{@id}", x => new { x.Id });
AllowAnonymous();
Get("/api/classifications/{@id}", x => new { x.Id }); //endpoint qui affiche la classification en fonction de l'id
AllowAnonymous(); //Autorise l'accès sans authentification
}
public override async Task HandleAsync(GetClassificationRequest req, CancellationToken ct)
{
Models.Classification? classification = await pyrofetesdbcontext
Models.Classification? classification = await pyrofetesdbcontext //Récupère la table marque dans la bdd
.Classifications
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); //récupère l'id
if (classification == null)
{
Console.WriteLine($"Aucune classification avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
return; //Arrêt de la méthode
}
GetClassificationDto responseDto = new()
{
Id = req.Id,
Label = classification.Label
Id = req.Id, //Affiche l'id
Label = classification.Label //Affiche le nom
};
await Send.OkAsync(responseDto, ct);
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
}
}

View File

@@ -8,27 +8,27 @@ public class UpdateClassificationEndpoint(PyroFetesDbContext pyrofetesdbcontext)
{
public override void Configure()
{
Put("/api/classifications");
AllowAnonymous();
Put("/api/classifications"); //Met à jour la classification en fonction de l'id
AllowAnonymous(); //Autorise l'accès sans authentification
}
public override async Task HandleAsync(UpdateClassificationDto req, CancellationToken ct)
{
Models.Classification classification = new()
Models.Classification classification = new() //Met à jour la classification
{
Label = req.Label
};
pyrofetesdbcontext.Add(classification);
await pyrofetesdbcontext.SaveChangesAsync(ct);
pyrofetesdbcontext.Add(classification); //ajoute la classification dans la bdd
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde les changements
GetClassificationDto response = new()
GetClassificationDto response = new() //renvoie l'id et le nom
{
Id = req.Id,
Label = req.Label
};
await Send.OkAsync(response, ct);
await Send.OkAsync(response, ct); //Envoie de la réponse réussite 200 au client
}
}