Example #1
0
        public async Task <IEnumerable <Product> > GetProducts(ProductRouteParams resources)
        {
            bool search = false;
            var  query  = _context.Products as IQueryable <Product>;

            foreach (PropertyInfo prop in resources.GetType().GetProperties())
            {
                var val  = prop.GetValue(resources);
                var name = prop.Name;

                if (val != null)
                {
                    search = true;
                    query  = (name == "InStock" || name == "Favourite")
                        ? query.Where(name + " == @0", val)
                        : query.Where(name + ".Contains(@0)", val);
                }
            }

            if (search)
            {
                return(await query
                       .OrderBy(p => p.Name)
                       .AsNoTracking()
                       .ToListAsync());
            }

            return(await GetProducts());
        }
Example #2
0
        public async Task <IActionResult> GetProducts([FromQuery] ProductRouteParams resources)
        {
            try
            {
                var products = await _repository.GetProducts(resources);

                return(Ok(_mapper.Map <IEnumerable <ProductDto> >(products)));
            }
            catch (Exception e)
            {
                return(_helpers.ErrorResponse(e));
            }
        }
        public Task <IEnumerable <ProductDto> > GetProducts(ProductRouteParams p)
        {
            var query = _context.products.AsQueryable();

            foreach (PropertyInfo prop in p.GetType().GetProperties())
            {
                var val  = prop.GetValue(p);
                var name = prop.Name;

                if (val != null)
                {
                    query = (name == "InStock" || name == "Favourite")
                        ? query.Where(name + " == @0", val)
                        : query.Where(name + ".Contains(@0)", val);
                }
            }

            return(Task.FromResult <IEnumerable <ProductDto> >
                       (query.OrderBy(p => p.Name)));
        }
 public async Task <IEnumerable <ProductDto> > GetProducts(ProductRouteParams p)
 {
     return(await _publicHttp.GetFromJsonAsync <IEnumerable <ProductDto> >
                ($"/api/products?name={p.Name}&description={p.Description}&instock={p.InStock}&favourite={p.Favourite}"));
 }
 private async Task SearchProduct(ProductRouteParams product)
 {
     products = await _productsDataService.GetProducts(product);
 }