forked from sanchezvem/PyroFetes
58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PyroFetes.DTO.Product.Request;
|
|
using PyroFetes.DTO.Product.Response;
|
|
|
|
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);
|
|
}
|
|
} |