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