forked from sanchezvem/PyroFetes
commentaire endpoint
This commit is contained in:
@@ -4,34 +4,34 @@ using FastEndpoints;
|
||||
|
||||
namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class CreateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateMaterialDto, GetMaterialDto>
|
||||
public class CreateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateMaterialDto, GetMaterialDto> //Instanciation d'une connexion à la bdd dans un endpoint, utilise l'élément de requête CreateMaterialDto et l'élement de réponse GetMaterialDto
|
||||
{
|
||||
public override void Configure()
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Post("Api/materials");
|
||||
AllowAnonymous();
|
||||
Post("Api/materials"); //Créer un matériel
|
||||
AllowAnonymous();//Autorise l'accès sans authentification
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateMaterialDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Material quantity = new()
|
||||
Models.Material quantity = new() //Création d'un nom, quantité et liaison de l'id de l'entrepôt rentré par l'utilisateur
|
||||
{
|
||||
Name = req.Label,
|
||||
Quantity = req.Quantity,
|
||||
WarehouseId = req.WarehouseId,
|
||||
};
|
||||
|
||||
pyrofetesdbcontext.Materials.Add(quantity);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
Console.WriteLine("Material added");
|
||||
pyrofetesdbcontext.Materials.Add(quantity); //Ajoute quantity à la bdd
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde les changements effectués dans la bdd
|
||||
Console.WriteLine("Material added"); //Affiche Material Added si réussi
|
||||
|
||||
GetMaterialDto responseDto = new()
|
||||
GetMaterialDto responseDto = new() //Constuire l'objet de réponse pour retourner name, quantity et warehouseId à l'utilisateur
|
||||
{
|
||||
Id = quantity.Id,
|
||||
WarehouseId = quantity.WarehouseId,
|
||||
Label = req.Label,
|
||||
Id = quantity.Id, //Affiche l'id lors de la réponse
|
||||
WarehouseId = quantity.WarehouseId, //Affiche le label lors de la réponse
|
||||
Label = req.Label, //Affiche le label lors de la réponse
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
||||
}
|
||||
}
|
||||
@@ -2,34 +2,34 @@ using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Material;
|
||||
public class DeleteMaterialRequest
|
||||
public class DeleteMaterialRequest //Création d'une classe DeleteMaterialRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int Id { get; set; } //Création d'un Id
|
||||
}
|
||||
public class DeleteMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteMaterialRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Delete("/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Delete("Api/materials/{@id}", x => new { x.Id }); //Création d'un endpoint qui supprime un matériel grâce à son id
|
||||
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteMaterialRequest req, CancellationToken ct)
|
||||
public override async Task HandleAsync(DeleteMaterialRequest req, CancellationToken ct) //Méthode asynchrone qui traite la suppression du matériel
|
||||
{
|
||||
Models.Material? materialToDelete = await pyrofetesdbcontext
|
||||
.Materials
|
||||
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
|
||||
Models.Material? materialToDelete = await pyrofetesdbcontext //Récupère un matériel dans la bdd et le stocke dans materialToDelete
|
||||
.Materials //Recherche la couleur dans la table Colors
|
||||
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct); //Recherche un matériel dont l'id correspond à req.Id
|
||||
|
||||
if (materialToDelete == null)
|
||||
if (materialToDelete == null) //Si le matériel n'est pas trouvé
|
||||
{
|
||||
Console.WriteLine($"Aucun matériel avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
pyrofetesdbcontext.Materials.Remove(materialToDelete);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
pyrofetesdbcontext.Materials.Remove(materialToDelete); //Supprime le matériel dans la bdd
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde les changements effectués dans la bdd
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
await Send.NoContentAsync(ct); //Renvoie une réponse réussite 204
|
||||
}
|
||||
}
|
||||
@@ -6,23 +6,23 @@ namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class GetAllMaterialsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetMaterialDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Get("Api/materials");
|
||||
AllowAnonymous();
|
||||
Get("Api/materials"); //Création d'un endpoint pour récupérer tous les matériaux grâce à la liste
|
||||
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetMaterialDto> responseDto = await pyrofetesdbcontext.Materials
|
||||
.Select(a => new GetMaterialDto
|
||||
List<GetMaterialDto> responseDto = await pyrofetesdbcontext.Materials //Création d'une liste qui récupère tous les matériaux de la bdd
|
||||
.Select(a => new GetMaterialDto //Sélectionne dans la liste chaque matériel
|
||||
{
|
||||
Id = a.Id,
|
||||
Label = a.Name,
|
||||
Quantity = a.Quantity,
|
||||
WarehouseId = a.WarehouseId,
|
||||
Id = a.Id, //Récupère l'id
|
||||
Label = a.Name, //Récupère le label
|
||||
Quantity = a.Quantity, //Récupère la quantité
|
||||
WarehouseId = a.WarehouseId, //Récupère l'id de l'entrepot
|
||||
}
|
||||
).ToListAsync(ct);
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
).ToListAsync(ct); //Retourne la liste du matériel
|
||||
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
||||
}
|
||||
}
|
||||
@@ -11,32 +11,32 @@ public class GetMaterialRequest
|
||||
|
||||
public class GetMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<GetMaterialRequest, GetMaterialDto>
|
||||
{
|
||||
public override void Configure()
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Get("Api/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
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)
|
||||
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
|
||||
.Materials
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
||||
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)
|
||||
if (material == null) //Si le matériel n'est pas trouvé
|
||||
{
|
||||
Console.WriteLine("Aucun matériel avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
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()
|
||||
GetMaterialDto responseDto = new() //Constuire l'objet de réponse pour retourner id, label et warehouseId à l'utilisateur
|
||||
{
|
||||
Id = material.Id,
|
||||
Label = material.Name,
|
||||
WarehouseId = material.WarehouseId,
|
||||
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);
|
||||
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
||||
|
||||
}
|
||||
}
|
||||
@@ -7,36 +7,36 @@ namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class UpdateMaterialEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateMaterialDto, GetMaterialDto>
|
||||
{
|
||||
public override void Configure()
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Put("Api/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Put("Api/materials/{@id}", x => new { x.Id }); //Création d'un endpoint qui modifie le matériel grâce à son id
|
||||
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateMaterialDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Material? materialToEdit = await pyrofetesdbcontext
|
||||
.Materials
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
||||
Models.Material? materialToEdit = await pyrofetesdbcontext //Récupère un matériel dans la bdd et le stocke dans materialToEdit
|
||||
.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 (materialToEdit == null)
|
||||
if (materialToEdit == null) //Si le matériel n'est pas trouvé
|
||||
{
|
||||
Console.WriteLine("Aucun matériel avec l'id {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
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
|
||||
|
||||
}
|
||||
materialToEdit.Name = req.Label;
|
||||
materialToEdit.WarehouseId = req.WarehouseId;
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
materialToEdit.Name = req.Label; //Modification du label
|
||||
materialToEdit.WarehouseId = req.WarehouseId; //Modification de warehouseId
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde les changements effectués dans la bdd
|
||||
|
||||
GetMaterialDto responseDto = new()
|
||||
GetMaterialDto responseDto = new() //Constuire l'objet de réponse pour retourner l'id, le label et warehouseId du nouveau matériel
|
||||
{
|
||||
Id = req.Id,
|
||||
Label = req.Label,
|
||||
WarehouseId = req.WarehouseId,
|
||||
Id = req.Id, //Affiche l'id
|
||||
Label = req.Label, //Affiche le label
|
||||
WarehouseId = req.WarehouseId, //Affiche warehouseId
|
||||
};
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user