Ajouter PyroFetes/Endpoints/Material/GetMaterialEndpoint

This commit is contained in:
2025-10-08 16:44:35 +02:00
parent 75ab503dfc
commit 81ab26f765

View File

@@ -0,0 +1,43 @@
using API.DTO.Material.Response;
using FastEndpoints;
using Microsoft.AspNetCore.Authentication;
using Microsoft.EntityFrameworkCore;
namespace API.Endpoints.Material;
public class GetMaterialRequest
{
public int Id { get; set; }
}
public class GetMaterialEndpoint(AppDbContext appDbContext) : Endpoint<GetMaterialRequest, GetMaterialDto>
{
public override void Configure()
{
Get("/material/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetMaterialRequest req, CancellationToken ct)
{
Models.Material? material = await appDbContext
.Materials
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (material == null)
{
Console.WriteLine("Aucun matériel avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
GetMaterialDto responseDto = new()
{
Id = material.Id,
Name = material.Name,
WarehouseId = material.WarehouseId,
};
await Send.OkAsync(responseDto, ct);
}
}