forked from sanchezvem/PyroFetes
42 lines
1.9 KiB
C#
42 lines
1.9 KiB
C#
using API.DTO.Material.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace PyroFetes.Endpoints.Material;
|
|
|
|
public class GetMaterialRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class GetMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<GetMaterialRequest, GetMaterialDto>
|
|
{
|
|
public override void Configure() //Configuration de l'endpoint
|
|
{
|
|
Get("Api/materials/{@id}", x => new { x.Id }); //Création d'un endpoint qui récupère un matériel grâce à son id
|
|
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
|
}
|
|
|
|
public override async Task HandleAsync(GetMaterialRequest req, CancellationToken ct) //Méthode asynchrone qui traite la récupération du matériel
|
|
{
|
|
Models.Material? material = await pyrofetesdbcontext //Récupère un matériel dans la bdd et le stocke dans material
|
|
.Materials //Recherche le matériel dans la table Materials
|
|
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); //Recherche un matériel dont l'id correspond à req.Id
|
|
|
|
if (material == null) //Si le matériel n'est pas trouvé
|
|
{
|
|
Console.WriteLine("Aucun matériel avec l'ID {req.Id} trouvé."); //Afficher aucun matériel avec l'id ... trouvé
|
|
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
|
|
return; //Arrêt de la méthode
|
|
}
|
|
|
|
GetMaterialDto responseDto = new() //Constuire l'objet de réponse pour retourner id, label et warehouseId à l'utilisateur
|
|
{
|
|
Id = material.Id, //Affiche l'id lors de la réponse
|
|
Label = material.Name, //Affiche le label lors de la réponse
|
|
WarehouseId = material.WarehouseId, //Affiche warehouseId lors de la réponse
|
|
};
|
|
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
|
|
|
}
|
|
} |