Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Warehouse/CreateWarehouseEndpoint.cs
2025-10-09 17:17:42 +02:00

63 lines
1.8 KiB
C#

using API.DTO.Warehouse.Request;
using API.DTO.Warehouse.Response;
using FastEndpoints;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Warehouse;
public class CreateWarehouseEndpoint(PyroFetesDbContext db)
: Endpoint<CreateWarehouseDto, GetWarehouseDto>
{
public override void Configure()
{
Post("/api/warehouse");
AllowAnonymous();
}
public override async Task HandleAsync(CreateWarehouseDto req, CancellationToken ct)
{
var warehouse = new Models.Warehouse
{
Name = req.Name,
MaxWeight = req.MaxWeight,
Current = req.Current,
MinWeight = req.MinWeight,
Address = req.Adress,
ZipCode = req.ZipCode,
City = req.City
};
db.Warehouses.Add(warehouse);
await db.SaveChangesAsync(ct);
// 🔹 Ajout des produits liés à cet entrepôt
if (req.Products is not null && req.Products.Any())
{
foreach (var p in req.Products)
{
var warehouseProduct = new WarehouseProduct
{
WarehouseId = warehouse.Id,
ProductId = p.ProductId,
Quantity = p.Quantity
};
db.WarehouseProducts.Add(warehouseProduct);
}
await db.SaveChangesAsync(ct);
}
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
};
await Send.OkAsync(response, ct);
}
}