forked from sanchezvem/PyroFetes
09/10
This commit is contained in:
54
PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs
Normal file
54
PyroFetes/Endpoints/Warehouse/UpdateWarehouseEndpoint.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using API.DTO.Warehouse.Request;
|
||||
using API.DTO.Warehouse.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Warehouse;
|
||||
|
||||
public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateWarehouseDto, GetWarehouseDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/warehouses/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateWarehouseDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Warehouse? warehouseToEdit = await pyrofetesdbcontext
|
||||
.Warehouses
|
||||
.SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (warehouseToEdit == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun entrepôt avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Mise à jour des champs
|
||||
warehouseToEdit.Name = req.Name;
|
||||
warehouseToEdit.MaxWeight = req.MaxWeight;
|
||||
warehouseToEdit.Current = req.Current;
|
||||
warehouseToEdit.MinWeight = req.MinWeight;
|
||||
warehouseToEdit.Address = req.Adress;
|
||||
warehouseToEdit.ZipCode = req.ZipCode;
|
||||
warehouseToEdit.City = req.City;
|
||||
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
GetWarehouseDto responseDto = new()
|
||||
{
|
||||
Id = warehouseToEdit.Id,
|
||||
Name = warehouseToEdit.Name,
|
||||
MaxWeight = warehouseToEdit.MaxWeight,
|
||||
Current = warehouseToEdit.Current,
|
||||
MinWeight = warehouseToEdit.MinWeight,
|
||||
Adress = warehouseToEdit.Address,
|
||||
ZipCode = warehouseToEdit.ZipCode,
|
||||
City = warehouseToEdit.City
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user