예제 #1
0
        public async Task <IEnumerable <ProductModel> > GetProducts(SearchProductsModel searchModel)
        {
            //var products = new List<ProductModel>();
            //HttpClient httpClient = HttpClientFactory.CreateClient("prom");
            //foreach (var token in searchModel.SelectedPromTokens)
            //{
            //    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            //    var response = await httpClient.GetAsync("products/list");
            //    string content = await response.Content.ReadAsStringAsync();
            //    var deserializedProductList = JsonConvert.DeserializeObject<ProductListModel>(content);
            //    var deserializedProducts = deserializedProductList.Products;
            //    foreach (var deserializedProduct in deserializedProducts)
            //    {
            //        deserializedProduct.UsedToken = token;
            //    }
            //    products.AddRange(deserializedProducts);
            //}
            var products = JsonConvert.DeserializeObject <IEnumerable <ProductModel> >(File.ReadAllText(Path.Combine(WebHostEnvironment.WebRootPath, "products.json")));

            products = products.Where(_ => searchModel.SelectedPromTokens.Any(token => token == _.UsedToken));

            var filteredProducts = products.FilterProducts(searchModel);

            if (searchModel.AvailabilityBy == ProductAvailabilityBy.Name)
            {
                foreach (var filteredProduct in filteredProducts)
                {
                    var groupedByName = products.GroupBy(_ => _.UsedToken, (key, group) => new { Key = key, Count = group.Count(_ => _.Name == filteredProduct.Name) });
                    filteredProduct.AvailableInShops = groupedByName.Count(_ => _.Count > 0);
                }
            }
            else if (searchModel.AvailabilityBy == ProductAvailabilityBy.Sku)
            {
                foreach (var filteredProduct in filteredProducts)
                {
                    var groupedBySku = products.GroupBy(_ => _.UsedToken, (key, group) => new { Key = key, Count = group.Count(_ => _.Sku == filteredProduct.Sku) });
                    filteredProduct.AvailableInShops = groupedBySku.Count(_ => _.Count > 0);
                }
            }
            else
            {
                throw new ArgumentException("Incorrect value of 'AvailabilityBy' field.");
            }
            return(filteredProducts);
        }
        /// <summary>
        /// Filter products using search model.
        /// Attention, no tokens are used for filtering.
        /// This method should be used on an already selected array of products using tokens.
        /// </summary>
        public static IEnumerable <ProductModel> FilterProducts(this IEnumerable <ProductModel> products, SearchProductsModel searchModel)
        {
            if (string.IsNullOrEmpty(searchModel.SearchText))
            {
                return(products);
            }

            switch (searchModel.SearchBy)
            {
            case SearchProductsBy.Name:
                return(products.Where(_ => _.Name.Contains(searchModel.SearchText)));

            case SearchProductsBy.Description:
                return(products.Where(_ => _.Description.Contains(searchModel.SearchText)));

            case SearchProductsBy.Sku:
                return(products.Where(_ => _.Sku.Contains(searchModel.SearchText)));

            case SearchProductsBy.Keywords:
                return(products.Where(_ => _.Keywords.Contains(searchModel.SearchText)));

            default:
                throw new ArgumentException("Incorrect value of 'searchBy' field.");
            }
        }