Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Supplier/GetAllSupplierEndpoint.cs
2025-10-09 15:16:08 +02:00

44 lines
1.2 KiB
C#

using API.DTO.Supplier.Response;
using API.DTO.Supplier.Request;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Supplier;
public class GetAllSuppliersEndpoint(PyroFetesDbContext pyrofetesdbcontext)
: EndpointWithoutRequest<List<GetSupplierDto>>
{
public override void Configure()
{
Get("/api/suppliers");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var suppliers = await pyrofetesdbcontext.Suppliers
.Include(s => s.Prices)
.ToListAsync(ct);
var responseDto = suppliers.Select(s => new GetSupplierDto
{
Id = s.Id,
Name = s.Name!,
Email = s.Email!,
PhoneNumber = s.Phone!,
Adress = s.Address!,
ZipCode = s.ZipCode,
City = s.City!,
// 🔹 Liste des produits liés via Price
Products = s.Prices.Select(pr => new SupplierProductPriceDto
{
ProductId = pr.ProductId,
SellingPrice = pr.SellingPrice
}).ToList()
}).ToList();
await Send.OkAsync(responseDto, ct);
}
}