forked from sanchezvem/PyroFetes
MAJ Mathilde
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
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;
|
||||
|
||||
@@ -9,20 +11,23 @@ public class GetProductRequest
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<GetProductRequest, GetProductDto>
|
||||
public class GetProductEndpoint(PyroFetesDbContext db)
|
||||
: Endpoint<GetProductRequest, GetProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/product/{@id}", x => new { x.Id });
|
||||
Get("/api/products/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Product? product = await pyrofetesdbcontext
|
||||
.Products.Include(product => product.Classification).Include(product => product.ProductCategory)
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct);
|
||||
// Inclure toutes les relations : Prices + WarehouseProducts + Warehouse
|
||||
var product = await db.Products
|
||||
.Include(p => p.Prices)
|
||||
.Include(p => p.WarehouseProducts)
|
||||
.ThenInclude(wp => wp.Warehouse)
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, ct);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
@@ -31,7 +36,7 @@ public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint
|
||||
return;
|
||||
}
|
||||
|
||||
GetProductDto responseDto = new()
|
||||
var responseDto = new GetProductDto
|
||||
{
|
||||
Id = product.Id,
|
||||
Reference = product.References,
|
||||
@@ -45,11 +50,24 @@ public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint
|
||||
Image = product.Image,
|
||||
Link = product.Link,
|
||||
ClassificationId = product.ClassificationId,
|
||||
ClassificationLabel = product.Classification!.Label,
|
||||
ProductCategoryId = product.ProductCategoryId,
|
||||
ProductCategoryLabel = product.ProductCategory!.Label,
|
||||
ProductCategoryId = product.ProductCategoryId,
|
||||
|
||||
// Fournisseurs liés via Price
|
||||
Suppliers = product.Prices.Select(pr => new ProductSupplierPriceDto
|
||||
{
|
||||
SupplierId = pr.SupplierId,
|
||||
SellingPrice = pr.SellingPrice
|
||||
}).ToList(),
|
||||
|
||||
// Entrepôts liés via WarehouseProduct
|
||||
Warehouses = product.WarehouseProducts.Select(wp => new GetProductWarehouseDto
|
||||
{
|
||||
WarehouseId = wp.WarehouseId,
|
||||
WarehouseName = wp.Warehouse?.Name ?? string.Empty,
|
||||
Quantity = wp.Quantity
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user