forked from sanchezvem/PyroFetes
commentaire endpoint
This commit is contained in:
@@ -4,34 +4,34 @@ using FastEndpoints;
|
||||
|
||||
namespace PyroFetes.Endpoints.ProductCategory;
|
||||
|
||||
public class CreateProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateProductCategoryDto, GetProductCategoryDto>
|
||||
public class CreateProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateProductCategoryDto, GetProductCategoryDto> //Instanciation d'une connexion à la bdd dans un endpoint, utilise l'élément de requête CreateProductCategoryDto et l'élement de réponse GetProductCategoryDto
|
||||
{
|
||||
public override void Configure()
|
||||
public override void Configure() //Configuration de l'endpoint
|
||||
{
|
||||
Post("/api/productcategories");
|
||||
AllowAnonymous();
|
||||
Post("/api/productcategories"); //Créer une catégorie de produits
|
||||
AllowAnonymous(); //Autorise l'accès sans authentification
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateProductCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory productCategory = new ()
|
||||
Models.ProductCategory productCategory = new () //Création d'un label rentré par l'utilisateur
|
||||
{
|
||||
Label = req.Label,
|
||||
|
||||
};
|
||||
|
||||
pyrofetesdbcontext.ProductCategories.Add(productCategory);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
pyrofetesdbcontext.ProductCategories.Add(productCategory); //Ajout du catégorie de la produit à la bdd
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde du catégorie de la produit dans la bdd
|
||||
|
||||
Console.WriteLine("Category créé avec succès !");
|
||||
|
||||
GetProductCategoryDto responseDto = new ()
|
||||
GetProductCategoryDto responseDto = new () //renvoie l'id et le label
|
||||
{
|
||||
Id = productCategory.Id,
|
||||
Label = productCategory.Label
|
||||
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(responseDto, ct); //Réponse au client
|
||||
}
|
||||
}
|
||||
@@ -12,25 +12,25 @@ public class DeleteProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/productcategories/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Delete("/api/productcategories/{@id}", x => new { x.Id }); //endpoint qui supprime une catégorie de produit grâce à son id
|
||||
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteProductCategoryRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory? productCategoryToDelete = await pyrofetesdbcontext
|
||||
.ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct);
|
||||
Models.ProductCategory? productCategoryToDelete = await pyrofetesdbcontext //Récupère une catégorie de produit dans la bdd et le stocke dans productCategoryToDelete
|
||||
.ProductCategories //Recherche la couleur dans la table ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct); //Recherche une catégorie de produit dont l'id correspond à req.Id
|
||||
|
||||
if (productCategoryToDelete == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
|
||||
return; //Arrêt de la méthode
|
||||
}
|
||||
|
||||
pyrofetesdbcontext.ProductCategories.Remove(productCategoryToDelete);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
pyrofetesdbcontext.ProductCategories.Remove(productCategoryToDelete); //Supprime la catégorie de produit 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
|
||||
}
|
||||
}
|
||||
@@ -8,20 +8,20 @@ public class GetAllProductCategoriesEndpoint(PyroFetesDbContext pyrofetesdbconte
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/productcategories");
|
||||
AllowAnonymous();
|
||||
Get("/api/productcategories"); //Endpoint qui affiche toutes les catégories de produits
|
||||
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetProductCategoryDto> responseDto = await pyrofetesdbcontext.ProductCategories
|
||||
.Select(pc => new GetProductCategoryDto()
|
||||
List<GetProductCategoryDto> responseDto = await pyrofetesdbcontext.ProductCategories //Création d'une liste qui récupère toutes les catégories de produits dans la bdd
|
||||
.Select(pc => new GetProductCategoryDto() //Sélectionne dans la liste chaque catégorie de produits
|
||||
{
|
||||
Id = pc.Id,
|
||||
Label = pc.Label
|
||||
Id = pc.Id, //Récupère l'id
|
||||
Label = pc.Label //Récupère le label
|
||||
}
|
||||
).ToListAsync(ct);
|
||||
).ToListAsync(ct); //Retourne la liste
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
await Send.OkAsync(responseDto, ct); //Envoie de la réponse réussite 200 au client
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,29 +14,29 @@ public class GetProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) :
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/productcategory/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Get("/api/productcategory/{@id}", x => new { x.Id }); //endpoint qui récupère une catégorie de produit grâce à son id
|
||||
AllowAnonymous(); //Autorise l'accès sans authentification
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetProductCategoryRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory? productCategory = await pyrofetesdbcontext
|
||||
.ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct);
|
||||
Models.ProductCategory? productCategory = await pyrofetesdbcontext //Récupère une catégorie de produits dans la bdd et le stocke dans productCategory
|
||||
.ProductCategories //Recherche dans la table ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct);//Recherche une catégorie de produits dont l'id correspond à req.Id
|
||||
|
||||
if (productCategory == null)
|
||||
if (productCategory == null) //Si la catégorie de produit n'est pas trouvé
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
|
||||
return; //Arrêt de la méthode
|
||||
}
|
||||
|
||||
GetProductCategoryDto responseDto = new()
|
||||
{
|
||||
Id = productCategory.Id,
|
||||
Label = productCategory.Label
|
||||
Id = productCategory.Id, //Affiche l'id lors de la réponse
|
||||
Label = productCategory.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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,31 +9,31 @@ public class UpdateProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/productcategory/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Put("/api/productcategory/{@id}", x => new { x.Id }); //endpoint qui modifie la catégorie de produit grâce à son id
|
||||
AllowAnonymous(); //Ignorer les requêtes non authentifiées
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateProductCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory? productCategoryToEdit = await pyrofetesdbcontext
|
||||
.ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct);
|
||||
Models.ProductCategory? productCategoryToEdit = await pyrofetesdbcontext //Récupère une catégorie de produits dans la bdd et le stocke dans productCategoryToEdit
|
||||
.ProductCategories //Recherche dans la table ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct); //Recherche une catégorie de produits dont l'id correspond à req.Id
|
||||
|
||||
if (productCategoryToEdit == null)
|
||||
if (productCategoryToEdit == null) //Si la catégorie de produit n'est pas trouvé
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
|
||||
return; //Arrêt de la méthode
|
||||
}
|
||||
|
||||
productCategoryToEdit.Label = req.Label;
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
productCategoryToEdit.Label = req.Label; //Modification du label
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct); //Sauvegarde les changements effectués dans la bdd
|
||||
|
||||
GetProductCategoryDto responseDto = new()
|
||||
{
|
||||
Id = req.Id,
|
||||
Label = req.Label,
|
||||
Id = req.Id, //Affiche l'id
|
||||
Label = req.Label, //Affiche le label
|
||||
};
|
||||
|
||||
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