forked from sanchezvem/PyroFetes
Ajouter PyroFetes/Endpoints/Color/UpdateColorEndpoint
This commit is contained in:
42
PyroFetes/Endpoints/Color/UpdateColorEndpoint
Normal file
42
PyroFetes/Endpoints/Color/UpdateColorEndpoint
Normal file
@@ -0,0 +1,42 @@
|
||||
using API.DTO.Color.Request;
|
||||
using API.DTO.Color.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.AspNetCore.Server.Kestrel;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Endpoints.Color;
|
||||
|
||||
public class UpdateColorEndpoint(AppDbContext appDbContext) : Endpoint<UpdateColorDto, GetColorDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/colors/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateColorDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Color? colorToEdit = await appDbContext
|
||||
.Colors
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (colorToEdit == null)
|
||||
{
|
||||
Console.WriteLine("Aucune couleur avec l'id {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
|
||||
}
|
||||
colorToEdit.Label = req.Label;
|
||||
await appDbContext.SaveChangesAsync(ct);
|
||||
|
||||
GetColorDto responseDto = new()
|
||||
{
|
||||
Id = req.Id,
|
||||
Label = req.Label,
|
||||
};
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user