forked from sanchezvem/PyroFetes
AJout des DTO et endpoint sur le nouveau git
This commit is contained in:
52
PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs
Normal file
52
PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user