Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Material/GetMaterialEndpoint.cs
2025-10-09 16:55:29 +02:00

43 lines
1.1 KiB
C#

using PyroFetes.DTO.Material.Response;
using FastEndpoints;
using Microsoft.AspNetCore.Authentication;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Material;
public class GetMaterialRequest
{
public int Id { get; set; }
}
public class GetMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint<GetMaterialRequest, GetMaterialDto>
{
public override void Configure()
{
Get("/api/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,
Label = material.Label,
WarehouseId = material.WarehouseId,
};
await Send.OkAsync(responseDto, ct);
}
}