public async Task <ActionResult> GetAll([FromQuery] ProductsParameters parameters)
        {
            try
            {
                var products = await UnitOfWork.ProductRepository.GetProducts(parameters);

                if (products.Count == 0)
                {
                    return(NoContent());
                }

                var metaData = new
                {
                    products.CurrentPage,
                    products.PageSize,
                    products.TotalPages,
                    products.TotalCount,
                    products.HasNext,
                    products.HasPrevious,
                };

                Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(metaData));

                var productsDto = Mapper.Map <List <ProductDTO> >(products);

                return(Ok(productsDto));
            }
            catch (Exception)
            {
                return(StatusCode(500, "An error unexpected occurred."));
            }
        }
Example #2
0
        public PagedList <Product> GetProducts(ProductsParameters parameters)
        {
            IQueryable <Product> allProducts = null;

            if (!string.IsNullOrEmpty(parameters.Description))
            {
                allProducts = FindAllByDescription(parameters.Description);
            }
            else
            {
                allProducts = FindAll();
            }

            if (parameters.SellStartDate != default)
            {
                allProducts = allProducts.Where(p => p.SellStartDate.Equals(parameters.SellStartDate));
            }

            if (!string.IsNullOrEmpty(parameters.Name))
            {
                allProducts = allProducts.Where(p => p.Name == parameters.Name);
            }

            return(PagedList <Product> .ToPagedList(allProducts.OrderBy(p => p.Name),
                                                    parameters.PageNumber,
                                                    parameters.PageSize));
        }
Example #3
0
        // GET api/products/
        public IHttpActionResult Get([FromUri] ProductsParameters parameters)
        {
            try
            {
                var allProducts = _repository.Product.GetProducts(parameters);

                if (allProducts == null || allProducts.Count() == 0)
                {
                    return(NotFound());
                }

                var metadata = new
                {
                    allProducts.TotalCount,
                    allProducts.PageSize,
                    allProducts.CurrentPage,
                    allProducts.TotalPages,
                    allProducts.HasNext,
                    allProducts.HasPrevious
                };

                HttpContext.Current.Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata));

                return(Ok(allProducts));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Example #4
0
        public async Task <PagedList <Product> > GetProducts(ProductsParameters parameters)
        {
            var query = GetAll().AsQueryable();

            return(await PagedList <Product> .ToPagedList(query, parameters.PageNumber, parameters.PageSize));
        }