forked from sanchezvem/PyroFetes
AJout des DTO et endpoint sur le nouveau git
This commit is contained in:
58
PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs
Normal file
58
PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using API.DTO.Product.Request;
|
||||
using API.DTO.Product.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class UpdateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<UpdateProductDto, GetProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/products/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Product? productToEdit = await pyrofetesdbcontext
|
||||
.Products
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (productToEdit == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
productToEdit.References = req.Reference;
|
||||
productToEdit.Name = req.Name;
|
||||
productToEdit.Duration = req.Duration;
|
||||
productToEdit.Caliber = req.Caliber;
|
||||
productToEdit.ApprovalNumber = req.ApprovalNumber;
|
||||
productToEdit.Weight = req.Weight;
|
||||
productToEdit.Nec = req.Nec;
|
||||
productToEdit.SellingPrice = req.SellingPrice;
|
||||
productToEdit.Image = req.Image;
|
||||
productToEdit.Link = req.Link;
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
GetProductDto responseDto = new()
|
||||
{
|
||||
Id = req.Id,
|
||||
Reference = req.Reference,
|
||||
Name = req.Name,
|
||||
Duration = req.Duration,
|
||||
Caliber = req.Caliber,
|
||||
ApprovalNumber = req.ApprovalNumber,
|
||||
Weight = req.Weight,
|
||||
Nec = req.Nec,
|
||||
SellingPrice = req.SellingPrice,
|
||||
Image = req.Image,
|
||||
Link = req.Link
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user