Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs

52 lines
1.6 KiB
C#

using API.DTO.Supplier.Request;
using API.DTO.Supplier.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Supplier;
public class UpdateSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateSupplierDto, GetSupplierDto>
{
public override void Configure()
{
Put("/api/suppliers/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct)
{
Models.Supplier? supplierToEdit = await pyrofetesdbcontext
.Suppliers
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
if (supplierToEdit == null)
{
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
// Mise à jour des propriétés
supplierToEdit.Name = req.Name;
supplierToEdit.Email = req.Email;
supplierToEdit.Phone = req.PhoneNumber;
supplierToEdit.Address = req.Adress;
supplierToEdit.ZipCode = req.ZipCode;
supplierToEdit.City = req.City;
await pyrofetesdbcontext.SaveChangesAsync(ct);
GetSupplierDto responseDto = new()
{
Id = supplierToEdit.Id,
Name = supplierToEdit.Name,
Email = supplierToEdit.Email,
PhoneNumber = supplierToEdit.Phone,
Adress = supplierToEdit.Address,
ZipCode = supplierToEdit.ZipCode,
City = supplierToEdit.City
};
await Send.OkAsync(responseDto, ct);
}
}