public async Task <IActionResult> SearchPagination([FromQuery] ProductDto_Filter filter)
 {
     return(Ok(await _productService.SearchPagination(filter)));
 }
Beispiel #2
0
        public async Task <ServiceResponseWithPagination <List <ProductDto_ToReturn> > > SearchPagination(ProductDto_Filter filter)
        {
            var product = _dbContext.Products.Include(x => x.ProductGroups).AsQueryable();


            if (!string.IsNullOrWhiteSpace(filter.Name))
            {
                product = product.Where(x => x.Name.ToLower().Contains(filter.Name.ToLower()));
            }
            product = product.Where(x => x.Price <= filter.MaxPrice);
            product = product.Where(x => x.IsActive == filter.IsActive);


            if (!string.IsNullOrWhiteSpace(filter.OrderingField))
            {
                try
                {
                    product = product.OrderBy($"{filter.OrderingField} {(filter.AscendingOrder ? "ascending" : "descending")}");
                }
                catch
                {
                    return(ResponseResultWithPagination.Failure <List <ProductDto_ToReturn> >($"Could not order by field: {filter.OrderingField}"));
                }
            }


            var paginationResult = await _httpContext.HttpContext.InsertPaginationParametersInResponse(product, filter.RecordsPerPage, filter.Page);


            var result = _mapper.Map <List <ProductDto_ToReturn> >(await product.Paginate(filter).ToListAsync());

            return(ResponseResultWithPagination.Success(result, paginationResult));
        }