forked from sanchezvem/PyroFetes
commentaire endpoint
This commit is contained in:
@@ -6,28 +6,28 @@ namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class DeleteProductRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int Id { get; set; } //Création d'un Id
|
||||
}
|
||||
|
||||
public class DeleteProductEndpoint(PyroFetesDbContext db) : Endpoint<DeleteProductRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/products/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Delete("/api/products/{@id}", x => new { x.Id }); //endpoint qui supprime un produit grâce à son id
|
||||
AllowAnonymous(); //Autorise l'accès sans authentification
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteProductRequest req, CancellationToken ct)
|
||||
{
|
||||
// Récupérer le produit
|
||||
var productToDelete = await db.Products
|
||||
var productToDelete = await db.Products
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, ct);
|
||||
|
||||
if (productToDelete is null)
|
||||
{
|
||||
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
await Send.NotFoundAsync(ct); //Renvoie une erreur 404
|
||||
return; //Arrêt de la méthode
|
||||
}
|
||||
|
||||
// Supprimer les liaisons Price
|
||||
@@ -54,6 +54,6 @@ public class DeleteProductEndpoint(PyroFetesDbContext db) : Endpoint<DeleteProdu
|
||||
await db.SaveChangesAsync(ct);
|
||||
Console.WriteLine($"Produit {req.Id}, ses prix et ses entrepôts liés ont été supprimés avec succès.");
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
await Send.NoContentAsync(ct); //Renvoie une réponse réussite 204
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user