Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Brand/DeleteBrandEndpoint.cs
2025-10-09 16:55:29 +02:00

38 lines
1.0 KiB
C#

using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Brand;
public class DeleteBrandRequest
{
public int Id { get; set; }
}
public class DeleteBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteBrandRequest>
{
public override void Configure()
{
Delete("/api/brands/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(DeleteBrandRequest req, CancellationToken ct)
{
Models.Brand? brandToDelete = await pyrofetesdbcontext
.Brands
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
if (brandToDelete == null)
{
Console.WriteLine($"Aucune marque avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
pyrofetesdbcontext.Brands.Remove(brandToDelete);
await pyrofetesdbcontext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}