Example #1
0
        public async Task <ProductGetAllResponse> AllProducts([FromQuery] ProductQueryParameter productQueryParameter)
        {
            var result = await _mediator.Send(new ProductQuery()
            {
                PageIndex      = productQueryParameter.PageIndex,
                RecordsPerPage = productQueryParameter.RecordsPerPage
            });

            var response = new ProductGetAllResponse();

            response.PageIndex      = productQueryParameter.PageIndex;
            response.RecordsPerPage = productQueryParameter.RecordsPerPage;
            response.TotalItems     = result.TotalItems;
            response.Items          = result.Products.Select(_mapper.Map <ProductResponseItem>).ToList();
            return(response);
        }
Example #2
0
        public async Task <IActionResult> GetAllProducts([FromQuery] ProductQueryParameter queryParameter)
        {
            IQueryable <Product> products = _ctx.Products;

            // Checks if Minimum price and Maximum price is Not Null,
            // And both of them are selected based on the price labeled on products
            if (queryParameter.MinPrice != null &&
                queryParameter.MaxPrice != null)
            {
                products = products.Where(
                    p => p.Price >= queryParameter.MinPrice &&
                    p.Price <= queryParameter.MaxPrice
                    );
            }

            // Checks for product with based on SKU
            if (!string.IsNullOrEmpty(queryParameter.Sku))
            {
                products = products.Where(p => p.Sku == queryParameter.Sku);
            }

            // Checks for available Product based on search criteria
            if (!string.IsNullOrEmpty(queryParameter.Name))
            {
                products = products.Where(
                    p => p.Name.ToLower().Contains(queryParameter.Name.ToLower())
                    );
            }

            // Sort the products based on Id and also with the OrderType
            if (!string.IsNullOrEmpty(queryParameter.SortBy))
            {
                if (typeof(Product).GetProperty(queryParameter.SortBy) != null)
                {
                    products = products.OrderByCustom(queryParameter.SortBy, queryParameter.SortOrder);
                }
            }

            products = products
                       .Skip(queryParameter.Size * (queryParameter.Page - 1))
                       .Take(queryParameter.Size);

            return(Ok(await products.ToListAsync()));
        }