namespace NetinaShop.Repository.Handlers.Products;

public class GetProductQueryHandler : IRequestHandler<GetProductQuery, ProductLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetProductQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<ProductLDto> Handle(GetProductQuery request, CancellationToken cancellationToken)
    {
        var ent = await _repositoryWrapper.SetRepository<Product>().TableNoTracking
            .Where(b => b.Id == request.Id)
            .Select(ProductMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (ent == null)
            throw new AppException("Product not found", ApiResultStatusCode.NotFound);
        return ent;
    }
}