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

63 lines
2.0 KiB
C#

using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Product;
public class GetAllProductsEndpoint(PyroFetesDbContext db)
: EndpointWithoutRequest<List<GetProductDto>>
{
public override void Configure()
{
Get("/api/products");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
// Inclure toutes les relations nécessaires : Prices + WarehouseProducts + Warehouse
var products = await db.Products
.Include(p => p.Prices)
.Include(p => p.WarehouseProducts)
.ThenInclude(wp => wp.Warehouse)
.ToListAsync(ct);
var responseDto = products.Select(p => new GetProductDto
{
Id = p.Id,
Reference = p.References,
Name = p.Name,
Duration = p.Duration,
Caliber = p.Caliber,
ApprovalNumber = p.ApprovalNumber,
Weight = p.Weight,
Nec = p.Nec,
SellingPrice = p.SellingPrice,
Image = p.Image,
Link = p.Link,
ClassificationId = p.ClassificationId,
ProductCategoryId = p.ProductCategoryId,
// Liste des fournisseurs liés via Price
Suppliers = p.Prices.Select(pr => new ProductSupplierPriceDto
{
SupplierId = pr.SupplierId,
SellingPrice = pr.SellingPrice
}).ToList(),
// Liste des entrepôts via WarehouseProduct
Warehouses = p.WarehouseProducts.Select(wp => new GetProductWarehouseDto
{
WarehouseId = wp.WarehouseId,
WarehouseName = wp.Warehouse?.Name ?? string.Empty,
Quantity = wp.Quantity
}).ToList()
}).ToList();
await Send.OkAsync(responseDto, ct);
}
}