Files
Projet4/PyroFetes/Endpoints/Contact/DeleteContactEndpoint.cs
carteronm 4e64112a31 First Commit 09/10
# Conflicts:
#	PyroFetes/PyroFetes.csproj
2025-10-16 17:34:55 +02:00

30 lines
972 B
C#

using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Contact.Request;
using PyroFetes.DTO.Contact.Response;
namespace PyroFetes.Endpoints.Contact;
public class DeleteContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <GetContactRequest, GetContactDto>
{
public override void Configure()
{
Delete ("/api/Contacts/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetContactRequest req, CancellationToken ct)
{
Models.Contact? databaseContact = await pyroFetesDbContext.Contacts.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseContact == null)
{
await Send.NotFoundAsync(ct);
return;
}
pyroFetesDbContext.Contacts.Remove(databaseContact);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}