Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Warehouse/GetAllWarehouseEndpoint.cs
2025-10-09 15:16:08 +02:00

46 lines
1.3 KiB
C#

using API.DTO.Warehouse.Response;
using API.DTO.Warehouse.Request;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Warehouse;
public class GetAllWarehouseEndpoint(PyroFetesDbContext db)
: EndpointWithoutRequest<List<GetWarehouseDto>>
{
public override void Configure()
{
Get("/api/warehouses");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
// 🔹 On inclut les relations avec WarehouseProducts et Product
var warehouses = await db.Warehouses
.Include(w => w.WarehouseProducts)
.ThenInclude(wp => wp.Product)
.ToListAsync(ct);
var response = warehouses.Select(w => new GetWarehouseDto
{
Id = w.Id,
Name = w.Name,
MaxWeight = w.MaxWeight,
Current = w.Current,
MinWeight = w.MinWeight,
Adress = w.Address,
ZipCode = w.ZipCode,
City = w.City,
Products = w.WarehouseProducts.Select(wp => new WarehouseProductDto
{
ProductId = wp.ProductId,
ProductName = wp.Product?.Name,
Quantity = wp.Quantity
}).ToList()
}).ToList();
await Send.OkAsync(response, ct);
}
}