forked from sanchezvem/PyroFetes
42 lines
1.3 KiB
C#
42 lines
1.3 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 }); //endpoint qui affiche la marque en fonction de l'id
|
|
AllowAnonymous(); //Autorise l'accès sans authentification
|
|
}
|
|
|
|
public override async Task HandleAsync(GetBrandRequest req, CancellationToken ct)
|
|
{
|
|
|
|
Models.Brand? brand = await pyrofetesdbcontext //Récupère la table marque dans la bdd
|
|
.Brands
|
|
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); //récupère l'id
|
|
|
|
if (brand == null)
|
|
{
|
|
Console.WriteLine($"Aucune marque avec l'ID {req.Id} trouvé.");
|
|
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
|
|
return; //Arrêt de la méthode
|
|
}
|
|
|
|
GetBrandDto responseDto = new() //renvoie l'id et le nom
|
|
{
|
|
Id = req.Id, //Affiche l'id
|
|
Name = brand.Name //Affiche le nom
|
|
};
|
|
|
|
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
|
}
|
|
} |