[HttpPut("[action]")]
public async Task<IQueryable<ProductDto>> Put([FromBody] ProductsQueryAll query)
{
var response = await _mediator.Send(query);
return response;
}
public class ProductsQueryAll : IRequest<IQueryable<ProductDto>>
{
public string[] ProductGroups { get; set; }
public int[] SupplierIds { get; set; }
public string[] Categories { get; set; }
}
public class ProductsQueryAllHandler : IRequestHandler<ProductsQueryAll, IQueryable<ProductDto>>
{
private readonly DbContext _dbContext;
private readonly IMapper _mapper;
public ProductsQueryAllHandler(DbContext dbContext, IMapper mapper)
{
_dbContext = dbContext;
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public Task<IQueryable<ProductDto>> Handle(ProductsQueryAll request, CancellationToken cancellationToken)
{
var predicate = PredicateBuilder.True<ProductDm>();
if (request.ProductGroups?.Length > 0)
{
predicate = predicate.And(x => request.ProductGroups.Any(y => y == x.ProductGroup));
}
if (request.SupplierIds?.Length > 0)
{
predicate = predicate.And(x => request.SupplierIds.Any(y => y == x.SupplierId));
}
if (request.Categories?.Length > 0)
{
predicate = predicate.And(x => request.Categories.Any( y => y == x.Category));
}
var entityList = _dbContext.Products
.AsNoTracking()
.Where(predicate);
return Task.FromResult(_mapper.ProjectTo<ProductDto>(entityList));
}
}
var query = _dbContext.Products.AsQueryable();
if (request.ProductGroups?.Length > 0)
{
query = query.Where(x => request.ProductGroups.Any(y => y == x.ProductGroup));
}
if (request.SupplierIds?.Length > 0)
{
query = query.Where(x => request.SupplierIds.Any(y => y == x.SupplierId));
}
if (request.Categories?.Length > 0)
{
query = query.Where(x => request.Categories.Any( y => y == x.Category));
}
return Task.FromResult(_mapper.ProjectTo<ProductDto>(query));