forked from sanchezvem/PyroFetes
commentaire endpoint
This commit is contained in:
@@ -7,36 +7,36 @@ namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class UpdateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateMaterialDto, GetMaterialDto>
|
||||
{
|
||||
public override void Configure()
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Put("Api/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Put("Api/materials/{@id}", x => new { x.Id }); //Création d'un endpoint qui modifie le matériel grâce à son id
|
||||
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateMaterialDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Material? materialToEdit = await pyrofetesdbcontext
|
||||
.Materials
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
||||
Models.Material? materialToEdit = await pyrofetesdbcontext //Récupère un matériel dans la bdd et le stocke dans materialToEdit
|
||||
.Materials //Recherche le matériel dans la table Materials
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);//Recherche un matériel dont l'id correspond à req.Id
|
||||
|
||||
if (materialToEdit == null)
|
||||
if (materialToEdit == null) //Si le matériel n'est pas trouvé
|
||||
{
|
||||
Console.WriteLine("Aucun matériel avec l'id {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
Console.WriteLine("Aucun matériel avec l'id {req.Id} trouvé."); //Afficher aucun matériel avec l'id ... trouvé
|
||||
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
|
||||
return; //Arrêt de la méthode
|
||||
|
||||
}
|
||||
materialToEdit.Name = req.Label;
|
||||
materialToEdit.WarehouseId = req.WarehouseId;
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
materialToEdit.Name = req.Label; //Modification du label
|
||||
materialToEdit.WarehouseId = req.WarehouseId; //Modification de warehouseId
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde les changements effectués dans la bdd
|
||||
|
||||
GetMaterialDto responseDto = new()
|
||||
GetMaterialDto responseDto = new() //Constuire l'objet de réponse pour retourner l'id, le label et warehouseId du nouveau matériel
|
||||
{
|
||||
Id = req.Id,
|
||||
Label = req.Label,
|
||||
WarehouseId = req.WarehouseId,
|
||||
Id = req.Id, //Affiche l'id
|
||||
Label = req.Label, //Affiche le label
|
||||
WarehouseId = req.WarehouseId, //Affiche warehouseId
|
||||
};
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user