Files
PF3-fork/PyroFetes/Endpoints/Sound/UpdateSoundEndpoint.cs
T
cernont b3612f5bec Remove /api prefix from all routes and fix CityId FK constraint
- Strip /api prefix from all endpoint routes
- Make Show.CityId nullable (no longer required FK)
- Drop CityId FK constraint and alter column to NULL at startup via raw SQL
- Add migration MakeCityIdNullable for schema consistency
- Update Show DTOs to reflect nullable CityId

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 20:32:55 +02:00

69 lines
1.9 KiB
C#

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("/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);
}
}