forked from sanchezvem/PyroFetes
09/10
This commit is contained in:
37
PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs
Normal file
37
PyroFetes/Endpoints/Warehouse/DeleteWarehouseEndpoint.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Warehouse;
|
||||
|
||||
public class DeleteWarehouseRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteWarehouseRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/warehouse/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteWarehouseRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Warehouse? warehouseToDelete = await pyrofetesdbcontext
|
||||
.Warehouses
|
||||
.SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (warehouseToDelete == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun entrepôt avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
pyrofetesdbcontext.Warehouses.Remove(warehouseToDelete);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user