48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
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);
|
|
}
|
|
}
|