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

42 lines
1.0 KiB
C#

using PyroFetes.DTO.Brand.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Brand;
public class GetBrandRequest
{
public int Id { get; set; }
}
public class GetBrandEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<GetBrandRequest, GetBrandDto>
{
public override void Configure()
{
Get("/api/brands/{@id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetBrandRequest req, CancellationToken ct)
{
Models.Brand? brand = await pyrofetesdbcontext
.Brands
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
if (brand == null)
{
Console.WriteLine($"Aucune marque avec l'ID {req.Id} trouvé.");
await Send.NotFoundAsync(ct);
return;
}
GetBrandDto responseDto = new()
{
Id = req.Id,
Name = brand.Name
};
await Send.OkAsync(responseDto, ct);
}
}