Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs

64 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 nest 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);
}
}