forked from sanchezvem/PyroFetes
42 lines
1.0 KiB
C#
42 lines
1.0 KiB
C#
using API.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);
|
|
}
|
|
} |