Finalisation endpoints
This commit is contained in:
44
PyroFetes/Endpoints/Sound/DeleteSoundEndpoint.cs
Normal file
44
PyroFetes/Endpoints/Sound/DeleteSoundEndpoint.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user