Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Product/DeleteProductEndpoint.cs

37 lines
1.0 KiB
C#

using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Product;
public class DeleteProductRequest
{
public int Id { get; set; }
}
public class DeleteProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteProductRequest>
{
public override void Configure()
{
Delete("/api/products/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(DeleteProductRequest req, CancellationToken ct)
{
Models.Product? productToDelete = await pyrofetesdbcontext
.Products
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct);
if (productToDelete == null)
{
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
pyrofetesdbcontext.Products.Remove(productToDelete);
await pyrofetesdbcontext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}