Esempio n. 1
0
        public async Task <PagedList <Product> > SearchProductsAsync(string storeId, string culture, string categorySlug, IEnumerable <string> pricelistIds, int skip, int take)
        {
            var browseQuery = new DataContracts.BrowseQuery
            {
                Outline    = categorySlug,
                PriceLists = pricelistIds.ToArray(),
                Skip       = skip,
                Take       = take
            };

            var apiResponse = await _apiClient.BrowseClient.GetProductsAsync(storeId, culture, browseQuery, DataContracts.ItemResponseGroups.ItemMedium).ConfigureAwait(false);

            if (apiResponse == null)
            {
                return(null);
            }

            int pageIndex = skip / take + 1;

            var productIds = apiResponse.Items.Select(i => i.Id);

            var priceModels = await _priceService.GetPricesAsync(pricelistIds, productIds).ConfigureAwait(false);

            var productModels = apiResponse.Items.Select(i => i.ToViewModel(priceModels.FirstOrDefault(p => p.ProductId == i.Id)));

            return(new PagedList <Product>(productModels, pageIndex, take, apiResponse.TotalCount));
        }
        /// <summary>
        /// Binds the model to a value by using the specified controller context and binding context.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="bindingContext">The binding context.</param>
        /// <returns>The bound value.</returns>
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var parameters = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            var sp = parameters != null ? parameters.RawValue as BrowseQuery : null;
            if (sp == null)
            {
                var qs = GetParams(controllerContext);
                var qsDict = NvToDict(qs);
                var facets = qsDict.Where(k => FacetRegex.IsMatch(k.Key)).Select(k => k.WithKey(FacetRegex.Replace(k.Key, ""))).ToDictionary(x => x.Key, y => y.Value.Split(','));

                sp = new BrowseQuery
                {
                    Search = qs["q"].EmptyToNull(),
                    Skip = qs["p"].TryParse(null),
                    Take = qs["pageSize"].TryParse(BrowseQuery.DefaultPageSize),
                    SortProperty = qs["sort"].EmptyToNull(),
                    SortDirection = qs["sortorder"].EmptyToNull(),
                    Filters = facets
                };
                if (!string.IsNullOrEmpty(sp.Search))
                {
                    sp.Search = sp.Search.EscapeSearchTerm();
                }
            }
            return sp;
        }
        public async Task<PagedList<Product>> SearchProductsAsync(string storeId, string culture, string categorySlug, IEnumerable<string> pricelistIds, int skip, int take)
        {
            var browseQuery = new DataContracts.BrowseQuery
            {
                Outline = categorySlug,
                PriceLists = pricelistIds.ToArray(),
                Skip = skip,
                Take = take
            };

            var apiResponse = await _apiClient.BrowseClient.GetProductsAsync(storeId, culture, browseQuery, DataContracts.ItemResponseGroups.ItemMedium).ConfigureAwait(false);

            if (apiResponse == null)
            {
                return null;
            }

            int pageIndex = skip / take + 1;

            var productIds = apiResponse.Items.Select(i => i.Id);

            var priceModels = await _priceService.GetPricesAsync(pricelistIds, productIds).ConfigureAwait(false);

            var productModels = apiResponse.Items.Select(i => i.ToViewModel(priceModels.FirstOrDefault(p => p.ProductId == i.Id)));

            return new PagedList<Product>(productModels, pageIndex, take, apiResponse.TotalCount);
        }
		public async Task<ActionResult> Category(string id)
		{
			CategoryResults retVal = new CategoryResults();

			var category = await SearchClient.GetCategoryByCodeAsync("MarketPlace", "en-US", id);

			if (category != null)
			{
				var query = new BrowseQuery();
				query.Filters = new Dictionary<string, string[]>();
				query.Outline = GetOutline(category);

				var products = await SearchClient.GetProductsAsync("MarketPlace", "en-Us", query, ItemResponseGroups.ItemLarge);
				retVal.Modules.AddRange(products.Items.Select(i => i.ToWebModel()));
				foreach (var module in retVal.Modules)
				{
					var reviews = new ResponseCollection<Review>(); //await ReviewsClient.GetReviewsAsync(module.Keyword);
					module.Reviews.AddRange(reviews.Items.Select(i => i.ToWebModel(module.Keyword)));
				}

				retVal.CategoryCode = category.Code;
				retVal.CategoryName = category.Name;
			}

			if(category.Seo.Any())
			{
				ViewBag.Title = category.Seo[0].Title;
				ViewBag.Description = category.Seo[0].MetaDescription;
			}

			return View(retVal);
		}
Esempio n. 5
0
		// GET: Module
		private ModulesModel Modules(BrowseQuery query)
		{
			var products = Task.Run(() => SearchClient.GetProductsAsync("MarketPlace", "en-US", query, ItemResponseGroups.ItemLarge)).Result;

			var retVal = new ModulesModel();
			retVal.Items.AddRange(products.Items.Select(i => i.ToWebModel()));

			foreach (var item in retVal.Items)
			{
				var reviews = new ResponseCollection<Review>(); //Task.Run(() => ReviewsClient.GetReviewsAsync(item.Keyword)).Result;
				item.Reviews.AddRange(reviews.Items.Select(i => i.ToWebModel(item.Keyword)));
			}

			return retVal;
		}
Esempio n. 6
0
		public async Task<ActionResult> Find(string q)
		{
			var query = new BrowseQuery
			{
				Take = 15,
				Search = q.EscapeSearchTerm()
			};
			var results = await SearchClient.GetProductsAsync("MarketPlace", "en-US", query);

			var data = from i in results.Items
					   select new
					   {
						   url = string.Format(SiteUrlHelper.ResolveServerUrl("~/modules/{0}"), i.Code),
						   value = i.Name
					   };
			return Json(data.ToArray(), JsonRequestBehavior.AllowGet);
		}
Esempio n. 7
0
        public async Task <PagedList <Product> > GetProductsAsync(string storeId, string culture, string categorySlug, int skip, int take)
        {
            string outline = null;

            if (!string.IsNullOrEmpty(categorySlug))
            {
                var category = await _apiClient.BrowseClient.GetCategoryByKeywordAsync(storeId, culture, categorySlug).ConfigureAwait(false);

                var categoryViewModel = category.ToViewModel();

                outline = BuildOutline(categoryViewModel);
            }

            var browseQuery = new DataContracts.BrowseQuery
            {
                Outline = outline,
                Skip    = skip,
                Take    = take
            };

            var apiResponse = await _apiClient.BrowseClient.GetProductsAsync(storeId, culture, browseQuery, DataContracts.ItemResponseGroups.ItemMedium).ConfigureAwait(false);

            if (apiResponse == null)
            {
                return(null);
            }

            int pageIndex = skip / take + 1;

            var productIds = apiResponse.Items.Select(i => i.Id);

            var storeViewModel = await _storeService.GetStoreAsync(storeId).ConfigureAwait(false);

            var priceViewModels = await _priceService.GetPricesAsync(storeViewModel.CatalogId, storeViewModel.Currency, productIds).ConfigureAwait(false);

            var productViewModels = new List <Product>();

            foreach (var product in apiResponse.Items)
            {
                var priceViewModel = priceViewModels.FirstOrDefault(p => p.ProductId == product.Id);

                productViewModels.Add(product.ToViewModel(priceViewModel));
            }

            return(new PagedList <Product>(productViewModels, pageIndex, take, string.IsNullOrEmpty(categorySlug) ? apiResponse.Items.Count : apiResponse.TotalCount));
        }
        public async Task<PagedList<Product>> GetProductsAsync(string storeId, string culture, string categorySlug, int skip, int take)
        {
            string outline = null;
            if (!string.IsNullOrEmpty(categorySlug))
            {
                var category = await _apiClient.BrowseClient.GetCategoryByKeywordAsync(storeId, culture, categorySlug).ConfigureAwait(false);
                var categoryViewModel = category.ToViewModel();

                outline = BuildOutline(categoryViewModel);
            }

            var browseQuery = new DataContracts.BrowseQuery
            {
                Outline = outline,
                Skip = skip,
                Take = take
            };

            var apiResponse = await _apiClient.BrowseClient.GetProductsAsync(storeId, culture, browseQuery, DataContracts.ItemResponseGroups.ItemMedium).ConfigureAwait(false);

            if (apiResponse == null)
            {
                return null;
            }

            int pageIndex = skip / take + 1;

            var productIds = apiResponse.Items.Select(i => i.Id);

            var storeViewModel = await _storeService.GetStoreAsync(storeId).ConfigureAwait(false);

            var priceViewModels = await _priceService.GetPricesAsync(storeViewModel.CatalogId, storeViewModel.Currency, productIds).ConfigureAwait(false);

            var productViewModels = new List<Product>();

            foreach (var product in apiResponse.Items)
            {
                var priceViewModel = priceViewModels.FirstOrDefault(p => p.ProductId == product.Id);

                productViewModels.Add(product.ToViewModel(priceViewModel));
            }

            return new PagedList<Product>(productViewModels, pageIndex, take, string.IsNullOrEmpty(categorySlug) ? apiResponse.Items.Count : apiResponse.TotalCount);
        }
Esempio n. 9
0
        private void LoadSearchResults()
        {
            if (!this.Performed)
            {
                return;
            }

            var pageSize = this.Context == null ? 20 : this.Context["paginate.page_size"].ToInt(20);
            var skip = this.Context == null ? 0 : this.Context["paginate.current_offset"].ToInt();
            var terms = this.Terms; //this.Context["current_query"] as string;
            var type = this.Context == null ? "product" : this.Context["current_type"] as string;

            var siteContext = SiteContext.Current;
            var service = new CommerceService();
            var searchQuery = new BrowseQuery() { Skip = skip, Take = pageSize, Search = terms};
            var response = Task.Run(() => service.SearchAsync<object>(siteContext, searchQuery)).Result;
            this.Results = response;

            this.Performed = false;
        }
Esempio n. 10
0
		public async Task<ActionResult> Search(string q)
		{
			var query = new BrowseQuery
			{
				Search = q.EscapeSearchTerm(),
				Take = 1000
			};
			var results = await SearchClient.GetProductsAsync("MarketPlace", "en-US", query, ItemResponseGroups.ItemLarge);

			SearchResult retVal = new SearchResult();
			retVal.Results.AddRange(results.Items.Select(i => i.ToWebModel()));
			foreach (var module in retVal.Results)
			{
				var reviews = await ReviewsClient.GetReviewsAsync(module.Keyword);
				module.Reviews.AddRange(reviews.Items.Select(i => i.ToWebModel(module.Keyword)));
			}

			retVal.SearchTerm = q;

			return View(retVal);
		}
Esempio n. 11
0
		public async Task<ActionResult> DeveloperExtensions(string vendorId, string sort)
		{
			var query = new BrowseQuery();
			query.Filters = new Dictionary<string, string[]>();
			query.Filters.Add("vendorId", new[] { vendorId });
			query.Take = 1000;
			var results = await SearchClient.GetProductsAsync("MarketPlace", "en-US", query, ItemResponseGroups.ItemLarge);

			DeveloperSearchResult retVal = new DeveloperSearchResult();
			retVal.Results.AddRange(results.Items.Select(i => i.ToWebModel()));
			foreach (var module in retVal.Results)
			{
				var reviews = new ResponseCollection<Review>(); //await ReviewsClient.GetReviewsAsync(module.Keyword);
				module.Reviews.AddRange(reviews.Items.Select(i => i.ToWebModel(module.Keyword)));
			}

			var customer = await CustomerServiceClient.GetContactByIdAsync(vendorId);
			retVal.VenderName = customer.FullName;
			retVal.VendorId = vendorId;

			return View(retVal);
		}
Esempio n. 12
0
 /// <summary>
 ///     List items matching the given query
 /// </summary>
 public virtual async Task<ProductSearchResult> GetProductsAsync(
     string store,
     string language,
     BrowseQuery query,
     ItemResponseGroups? responseGroup = null)
 {
     var parameters = new
     {
         store,
         language,
         responseGroup = responseGroup.GetHashCode()
             .ToString(CultureInfo.InvariantCulture)
     };
     return
         await GetAsync<ProductSearchResult>(
             CreateRequestUri(RelativePaths.ProductsSearch, query.GetQueryString(parameters))).ConfigureAwait(false);
 }
Esempio n. 13
0
        public void LoadSlice(int from, int? to)
        {
            var pageSize = to == null ? 50 : to - from;

            var tags = this.Context["current_tags"] as string[];

            var filters = new Dictionary<string, string[]>();
            if (tags != null && tags.Any())
            {
                var tagsArray =
                    tags.Select(t => t.Split(new[] { '_' })).Select(x => new Tuple<string, string>(x[0], x[1]));

                foreach (var tagsGroup in tagsArray.GroupBy(x => x.Item1))
                {
                    filters.Add(tagsGroup.Key, tagsGroup.Select(g => g.Item2).ToArray());
                }
            }

            var service = new CommerceService();
            var context = SiteContext.Current;

            var sortProperty = String.Empty;
            var sortDirection = "ascending";

            var sort = string.IsNullOrEmpty(this.SortBy) ? this.DefaultSortBy : this.SortBy;
            if (sort.Equals("manual"))
            {
                sortProperty = "position";
            }
            else if (sort.Equals("best-selling"))
            {
                sortProperty = "position";
            }
            else
            {
                var sortArray = sort.Split(new[] { '-' });
                if (sortArray.Length > 1)
                {
                    sortProperty = sortArray[0];
                    sortDirection = sortArray[1];
                }
            }

            var searchQuery = new BrowseQuery() { SortProperty = sortProperty, SortDirection = sortDirection, Filters = filters, Skip = from, Take = pageSize.Value, Outline = this.BuildSearchOutline() };
            var response =
                Task.Run(() => service.SearchAsync<Product>(context, 
                    searchQuery, this, responseGroups: ItemResponseGroups.ItemSmall | ItemResponseGroups.Variations)).Result;

            // populate tags with facets returned
            if (response.Facets != null && response.Facets.Any())
            {
                var values = response.Facets.SelectMany(f => f.Values.Select(v => v.AsWebModel(f.Field)));
                this.Tags = new TagCollection(values);
            }

            this.Products = response;
        }
Esempio n. 14
0
        private void AddProductsToSitemap(List<string> vendorList)
        {
            var productsSitemap = new XElement(_xmlNamespace + _xmlUrlsetTag);

            var query = new BrowseQuery
            {
                Search = string.Empty.EscapeSearchTerm(),
                Take = 1000
            };

            var products = Task.Run(() => SearchClient.GetProductsAsync(_storeName, _storeLocalization, query, ItemResponseGroups.ItemLarge)).Result;

            if (products.TotalCount > 0)
            {
                foreach (var product in products.Items)
                {
                    if (product.Properties.Any(p => p.Key == _vendorIdPropertyName))
                    {
                        vendorList.Add((string)product.Properties.First(p => p.Key == _vendorIdPropertyName).Value);
                    }

                    if (product.Seo.Any())
                    {
                        productsSitemap.Add(BuildUrlElement(product.Seo.First().Keyword));
                    }
                }

                productsSitemap.Save(Path.Combine(_path, _modulesSitemapFileName));
            }
        }