Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs
2025-10-09 17:17:42 +02:00

55 lines
1.5 KiB
C#

using PyroFetes.DTO.Supplier.Request;
using PyroFetes.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)
{
var supplier = await pyrofetesdbcontext.Suppliers
.Include(s => s.Prices)
.SingleOrDefaultAsync(s => s.Id == req.Id, ct);
if (supplier == null)
{
Console.WriteLine($"Aucun fournisseur avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
var responseDto = new GetSupplierDto
{
Id = supplier.Id,
Name = supplier.Name!,
Email = supplier.Email!,
PhoneNumber = supplier.Phone!,
Adress = supplier.Address!,
ZipCode = supplier.ZipCode,
City = supplier.City!,
// Produits liés
Products = supplier.Prices.Select(p => new SupplierProductPriceDto
{
ProductId = p.ProductId,
SellingPrice = p.SellingPrice
}).ToList()
};
await Send.OkAsync(responseDto, ct);
}
}