Exemple #1
0
        public async Task <ServiceResponse <List <ProductDTO_ToReturn> > > SearchProductPaginate(ProductDTO_Filter filter)
        {
            var queryable = _dbContext.Products.AsQueryable();

            //filter
            if (!String.IsNullOrWhiteSpace(filter.Name))
            {
                queryable = queryable.Where(x => (x.Name).Contains(filter.Name));
            }

            if (filter.ProductGroupId != 0)
            {
                queryable = queryable.Where(x => x.ProductGroupId == filter.ProductGroupId);
            }

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


            //Add Paginate
            var paginationResult = await _httpContext.HttpContext.InsertPaginationParametersInResponse(queryable, filter.RecordsPerPage, filter.Page);

            //Excute query
            var product = await queryable.Paginate(filter).ToListAsync();

            var result = _mapper.Map <List <ProductDTO_ToReturn> >(product);

            return(ResponseResultWithPagination.Success(result, paginationResult));
        }
Exemple #2
0
 public async Task <IActionResult> GetFilter([FromQuery] ProductDTO_Filter input)
 {
     return(Ok(await _service.GetProductFilter(input)));
 }
Exemple #3
0
        public async Task <ServiceResponseWithPagination <List <ProductDTO_ToReturn> > > GetProductFilter(ProductDTO_Filter input)
        {
            try
            {
                var queryable = _context.Products.Include(x => x.ProductGroup).AsQueryable();

                //Filter
                if (!string.IsNullOrWhiteSpace(input.Name))
                {
                    queryable = queryable.Where(x => x.Name.Contains(input.Name));
                }
                if (input.IsShowInActive.HasValue)
                {
                    if (input.IsShowInActive.Value == false)
                    {
                        queryable = queryable.Where(x => x.IsActive == true);
                    }
                }
                else
                {
                    queryable = queryable.Where(x => x.IsActive == true);
                }
                if (input.ProductGroupId.HasValue)
                {
                    queryable = queryable.Where(x => x.ProductGroupId == input.ProductGroupId);
                }

                //Ordering
                if (!string.IsNullOrWhiteSpace(input.OrderingField))
                {
                    try
                    {
                        queryable = queryable.OrderBy($"{input.OrderingField} {(input.AscendingOrder ? "asc" : "desc")}");
                    }
                    catch (System.Exception ex)
                    {
                        _logger.LogError(ex.Message);
                        return(ResponseResultWithPagination.Failure <List <ProductDTO_ToReturn> >(ex.Message));
                    }
                }

                var paginationResult = await _httpContext.HttpContext.InsertPaginationParametersInResponse(queryable, input.RecordsPerPage, input.Page);

                var data = await queryable.Paginate(input).ToListAsync();

                var dto = _mapper.Map <List <ProductDTO_ToReturn> >(data);

                return(ResponseResultWithPagination.Success(dto, paginationResult));
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex.Message);
                return(ResponseResultWithPagination.Failure <List <ProductDTO_ToReturn> >(ex.Message));
            }
        }
Exemple #4
0
        public async Task <IActionResult> SearchProductPaginate([FromQuery] ProductDTO_Filter filter)
        {
            var result = await _product.SearchProductPaginate(filter);

            return(Ok(result));
        }