Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Products/UpdateProductEndpoint.cs
T
2026-05-24 17:22:03 +01:00

32 lines
913 B
C#

using FastEndpoints;
using PyroFetes.DTO.Product.Request;
using PyroFetes.Models;
using PyroFetes.Repositories;
using PyroFetes.Specifications.Products;
namespace PyroFetes.Endpoints.Products;
public class UpdateProductEndpoint(ProductsRepository productsRepository, AutoMapper.IMapper mapper) : Endpoint<UpdateProductDto>
{
public override void Configure()
{
Put("/products/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
{
Product? product = await productsRepository.SingleOrDefaultAsync(new GetProductByIdSpec(req.Id), ct);
if (product is null)
{
await Send.NotFoundAsync(ct);
return;
}
mapper.Map(req, product);
await productsRepository.UpdateAsync(product, ct);
await Send.NoContentAsync(ct);
}
}