using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Sound.Request; namespace PyroFetes.Endpoints.Sound; public class DeleteSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint { 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); } }