forked from sanchezvem/PyroFetes
58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using API.DTO.Warehouse.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PyroFetes.Models;
|
|
|
|
namespace PyroFetes.Endpoints.Warehouse;
|
|
|
|
public class GetWarehouseRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class GetWarehouseEndpoint(PyroFetesDbContext db)
|
|
: Endpoint<GetWarehouseRequest, GetWarehouseDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
// Pas de "@id" ici, juste {id}
|
|
Get("/api/warehouses/{@id}", x => new { x.Id });
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(GetWarehouseRequest req, CancellationToken ct)
|
|
{
|
|
// 🔹 Inclut les produits associés à cet entrepôt
|
|
var warehouse = await db.Warehouses
|
|
.Include(w => w.WarehouseProducts)
|
|
.ThenInclude(wp => wp.Product)
|
|
.SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct);
|
|
|
|
if (warehouse == null)
|
|
{
|
|
Console.WriteLine($" Aucun entrepôt avec l'ID {req.Id} trouvé.");
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
var response = new GetWarehouseDto
|
|
{
|
|
Id = warehouse.Id,
|
|
Name = warehouse.Name!,
|
|
MaxWeight = warehouse.MaxWeight,
|
|
Current = warehouse.Current,
|
|
MinWeight = warehouse.MinWeight,
|
|
Adress = warehouse.Address!,
|
|
ZipCode = warehouse.ZipCode,
|
|
City = warehouse.City!,
|
|
Products = warehouse.WarehouseProducts.Select(wp => new WarehouseProductDto
|
|
{
|
|
ProductId = wp.ProductId,
|
|
ProductName = wp.Product?.Name,
|
|
Quantity = wp.Quantity
|
|
}).ToList()
|
|
};
|
|
|
|
await Send.OkAsync(response, ct);
|
|
}
|
|
} |