forked from sanchezvem/PyroFetes
42 lines
2.0 KiB
C#
42 lines
2.0 KiB
C#
using API.DTO.Material.Request;
|
|
using API.DTO.Material.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace PyroFetes.Endpoints.Material;
|
|
|
|
public class UpdateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateMaterialDto, GetMaterialDto>
|
|
{
|
|
public override void Configure() //Configuration de l'endpoint
|
|
{
|
|
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 //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) //Si le matériel n'est pas trouvé
|
|
{
|
|
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; //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() //Constuire l'objet de réponse pour retourner l'id, le label et warehouseId du nouveau matériel
|
|
{
|
|
Id = req.Id, //Affiche l'id
|
|
Label = req.Label, //Affiche le label
|
|
WarehouseId = req.WarehouseId, //Affiche warehouseId
|
|
};
|
|
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
|
}
|
|
|
|
} |