Beispiel #1
0
        public async Task <ServiceResponseWithPagination <List <mProduct> > > GetProductWithFilter(GetProductFilterDto ProductFilter)
        {
            var Queryable = _dbContext.Products
                            .Include(x => x.ProductGroup)
                            .AsQueryable();

            if (!string.IsNullOrWhiteSpace(ProductFilter.Name))
            {
                Queryable = Queryable.Where(x => x.Name.Contains(ProductFilter.Name));
            }

            if (ProductFilter.Price != null)
            {
                Queryable = Queryable.Where(x => x.Price == ProductFilter.Price);
            }

            if (ProductFilter.StockCount != null)
            {
                Queryable = Queryable.Where(x => x.StockCount == ProductFilter.StockCount);
            }


            if (ProductFilter.ProductGroupId != null)
            {
                Queryable = Queryable.Where(x => x.ProductGroupId == ProductFilter.ProductGroupId);
            }

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

                ;
            }

            var paginationResult = await _httpcontext.HttpContext.InsertPaginationParametersInResponse(Queryable, ProductFilter.RecordsPerPage, ProductFilter.Page);

            var dto = await Queryable.Paginate(ProductFilter).ToListAsync();

            return(ResponseResultWithPagination.Success(dto, paginationResult));
        }
 public async Task <IActionResult> GetProductWithFilter([FromQuery] GetProductFilterDto filter)
 {
     return(Ok(await _prodService.GetProductWithFilter(filter)));
 }