forked from sanchezvem/PyroFetes
AJout des DTO et endpoint sur le nouveau git
This commit is contained in:
46
PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs
Normal file
46
PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using API.DTO.Supplier.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Supplier;
|
||||
|
||||
public class GetSupplierRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetSupplierEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<GetSupplierRequest, GetSupplierDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/suppliers/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Supplier? supplier = await pyrofetesdbcontext
|
||||
.Suppliers
|
||||
.SingleOrDefaultAsync(s => s.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (supplier == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
GetSupplierDto responseDto = new()
|
||||
{
|
||||
Id = supplier.Id,
|
||||
Name = supplier.Name!,
|
||||
Email = supplier.Email!,
|
||||
PhoneNumber = supplier.Phone!,
|
||||
Adress = supplier.Address!,
|
||||
ZipCode = supplier.ZipCode,
|
||||
City = supplier.City!
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user