Finalisation endpoints
This commit is contained in:
47
PyroFetes/Endpoints/Sound/CreateSoundEndpoint.cs
Normal file
47
PyroFetes/Endpoints/Sound/CreateSoundEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
34
PyroFetes/Endpoints/Sound/GetAllSoundsEndpoint.cs
Normal file
34
PyroFetes/Endpoints/Sound/GetAllSoundsEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
48
PyroFetes/Endpoints/Sound/GetSoundEndpoint.cs
Normal file
48
PyroFetes/Endpoints/Sound/GetSoundEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
68
PyroFetes/Endpoints/Sound/UpdateSoundEndpoint.cs
Normal file
68
PyroFetes/Endpoints/Sound/UpdateSoundEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user