Finalisation endpoints

This commit is contained in:
2025-11-13 15:11:34 +01:00
parent 5c12a45ae6
commit 3a09bfc8ad
20 changed files with 922 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
using FastEndpoints;
using PyroFetes.DTO.Sound.Request;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class CreateSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateSoundDto, ReadSoundDto>
{
public override void Configure()
{
Post("/api/sounds");
AllowAnonymous();
}
public override async Task HandleAsync(CreateSoundDto req, CancellationToken ct)
{
var sound = new PyroFetes.Models.Sound
{
Name = req.Name ?? string.Empty,
Type = req.Type,
Artist = req.Artist,
Duration = int.TryParse(req.Duration, out var duration) ? duration : null,
Kind = req.Kind,
Format = req.Format,
CreationDate = req.CreationDate,
SoundCategoryId = int.TryParse(req.SoundCategoryId, out var categoryId) ? categoryId : 0
};
pyroFetesDbContext.Sounds.Add(sound);
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadSoundDto
{
Id = sound.Id,
Name = sound.Name,
Type = sound.Type,
Artist = sound.Artist,
Duration = sound.Duration?.ToString(),
Kind = sound.Kind,
Format = sound.Format,
CreationDate = sound.CreationDate,
SoundCategoryId = sound.SoundCategoryId.ToString()
};
await Send.OkAsync(result, ct);
}
}

View 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);
}
}

View File

@@ -0,0 +1,34 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class GetAllSoundsEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<ReadSoundDto>>
{
public override void Configure()
{
Get("/api/sounds");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var sounds = await pyroFetesDbContext.Sounds
.Select(s => new ReadSoundDto
{
Id = s.Id,
Name = s.Name,
Type = s.Type,
Artist = s.Artist,
Duration = s.Duration != null ? s.Duration.Value.ToString() : null,
Kind = s.Kind,
Format = s.Format,
CreationDate = s.CreationDate,
SoundCategoryId = s.SoundCategoryId.ToString()
})
.ToListAsync(ct);
await Send.OkAsync(sounds, ct);
}
}

View File

@@ -0,0 +1,48 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Request;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class GetSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<IdSoundto, ReadSoundDto>
{
public override void Configure()
{
Get("/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
.Where(s => s.Id == req.Id.Value)
.Select(s => new ReadSoundDto
{
Id = s.Id,
Name = s.Name,
Type = s.Type,
Artist = s.Artist,
Duration = s.Duration != null ? s.Duration.Value.ToString() : null,
Kind = s.Kind,
Format = s.Format,
CreationDate = s.CreationDate,
SoundCategoryId = s.SoundCategoryId.ToString()
})
.FirstOrDefaultAsync(ct);
if (sound is null)
{
await Send.NotFoundAsync(ct);
return;
}
await Send.OkAsync(sound, ct);
}
}

View File

@@ -0,0 +1,68 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Sound.Request;
using PyroFetes.DTO.Sound.Response;
namespace PyroFetes.Endpoints.Sound;
public class UpdateSoundEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<UpdateSoundDto, ReadSoundDto>
{
public override void Configure()
{
Put("/api/sounds/{Id}");
AllowAnonymous();
}
public override async Task HandleAsync(UpdateSoundDto req, CancellationToken ct)
{
if (!req.Id.HasValue)
{
await Send.NotFoundAsync(ct);
return;
}
var sound = await pyroFetesDbContext.Sounds
.FirstOrDefaultAsync(s => s.Id == req.Id.Value, ct);
if (sound is null)
{
await Send.NotFoundAsync(ct);
return;
}
sound.Name = req.Name ?? sound.Name;
sound.Type = req.Type ?? sound.Type;
sound.Artist = req.Artist ?? sound.Artist;
if (int.TryParse(req.Duration, out var duration))
{
sound.Duration = duration;
}
sound.Kind = req.Kind ?? sound.Kind;
sound.Format = req.Format ?? sound.Format;
sound.CreationDate = req.CreationDate ?? sound.CreationDate;
if (int.TryParse(req.SoundCategoryId, out var categoryId))
{
sound.SoundCategoryId = categoryId;
}
await pyroFetesDbContext.SaveChangesAsync(ct);
var result = new ReadSoundDto
{
Id = sound.Id,
Name = sound.Name,
Type = sound.Type,
Artist = sound.Artist,
Duration = sound.Duration?.ToString(),
Kind = sound.Kind,
Format = sound.Format,
CreationDate = sound.CreationDate,
SoundCategoryId = sound.SoundCategoryId.ToString()
};
await Send.OkAsync(result, ct);
}
}