Files
PF3-fork/PyroFetes/Endpoints/Sound/DeleteSoundEndpoint.cs
2025-11-13 15:11:34 +01:00

45 lines
1.2 KiB
C#

using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Request;
namespace PyroFetes.Endpoints.Sound;
public class DeleteSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdSoundto>
{
public override void Configure()
{
Delete("/api/sounds/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(IdSoundto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var sound = await pyroFetesDbContext.Sounds
.Include(s => s.SoundTimecodes)
.FirstOrDefaultAsync(s => s.Id == req.Id.Value, ct);
if (sound is null)
{
await Send.NotFoundAsync(ct);
return;
}
// Supprimer les relations SoundTimecode associées si nécessaire
if (sound.SoundTimecodes != null && sound.SoundTimecodes.Any())
{
pyroFetesDbContext.SoundTimecodes.RemoveRange(sound.SoundTimecodes);
}
pyroFetesDbContext.Sounds.Remove(sound);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.OkAsync(ct);
}
}