forked from sanchezvem/PyroFetes
17:17 09/10
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using API.DTO.Warehouse.Request;
|
||||
using API.DTO.Warehouse.Response;
|
||||
using FastEndpoints;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Endpoints.Warehouse;
|
||||
|
||||
public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateWarehouseDto, GetWarehouseDto>
|
||||
public class CreateWarehouseEndpoint(PyroFetesDbContext db)
|
||||
: Endpoint<CreateWarehouseDto, GetWarehouseDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -14,7 +16,7 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
|
||||
|
||||
public override async Task HandleAsync(CreateWarehouseDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Warehouse warehouse = new()
|
||||
var warehouse = new Models.Warehouse
|
||||
{
|
||||
Name = req.Name,
|
||||
MaxWeight = req.MaxWeight,
|
||||
@@ -24,13 +26,27 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
|
||||
ZipCode = req.ZipCode,
|
||||
City = req.City
|
||||
};
|
||||
|
||||
pyrofetesdbcontext.Warehouses.Add(warehouse);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
Console.WriteLine("Entrepôt créé avec succès !");
|
||||
|
||||
GetWarehouseDto responseDto = new()
|
||||
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,
|
||||
@@ -42,6 +58,6 @@ public class CreateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : En
|
||||
City = warehouse.City
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(response, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user