forked from sanchezvem/PyroFetes
MAJ Mathilde
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using FastEndpoints;
|
||||
using API.DTO.Product.Request;
|
||||
using API.DTO.Product.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Product.Response;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class GetAllProductsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetProductDto>>
|
||||
public class GetAllProductsEndpoint(PyroFetesDbContext db)
|
||||
: EndpointWithoutRequest<List<GetProductDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -14,27 +17,45 @@ public class GetAllProductsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : End
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetProductDto> responseDto = await pyrofetesdbcontext.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,
|
||||
ClassificationLabel = p.Classification!.Label,
|
||||
ProductCategoryId = p.ProductCategoryId,
|
||||
ProductCategoryLabel = p.ProductCategory!.Label,
|
||||
}
|
||||
).ToListAsync(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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user