forked from sanchezvem/PyroFetes
38 lines
1.0 KiB
C#
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);
|
|
}
|
|
} |