forked from sanchezvem/PyroFetes
64 lines
2.2 KiB
C#
64 lines
2.2 KiB
C#
using PyroFetes.DTO.Product.Response;
|
||
using FastEndpoints;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using PyroFetes.DTO.Product.Request;
|
||
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,
|
||
// Le modèle Product contient "Reference" (string) — pas "References" (int)
|
||
Reference = int.TryParse(p.Reference, out var refInt) ? refInt : 0,
|
||
Name = p.Name,
|
||
Duration = p.Duration,
|
||
Caliber = p.Caliber,
|
||
ApprovalNumber = p.ApprovalNumber,
|
||
Weight = p.Weight,
|
||
Nec = p.Nec,
|
||
// Le prix de vente n’est pas dans Product, on le récupère via Prices
|
||
SellingPrice = p.Prices.FirstOrDefault()?.SellingPrice ?? 0,
|
||
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 liés 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);
|
||
}
|
||
}
|