protected RestSearchDocument PopulateRestSearchDocument(string code, string language)
 {
     var contentLink = _referenceConverter.GetContentLink(code);
     if (ContentReference.IsNullOrEmpty(contentLink))
     {
         return null;
     }
     var document = new RestSearchDocument();
     var productContent = _contentLoader.Get<FashionProduct>(contentLink);
     var variants = _contentLoader.GetItems(productContent.GetVariants(_relationRepository), CultureInfo.GetCultureInfo(language)).OfType<FashionVariant>().ToList();
     AddPrices(document, variants);
     AddColors(document, variants);
     AddSizes(document, variants);
     AddCodes(document, variants);
     document.Fields.Add(new RestSearchField("code", productContent.Code, new[] { SearchField.Store.YES, SearchField.IncludeInDefaultSearch.YES }));
     document.Fields.Add(new RestSearchField("displayname", productContent.DisplayName));
     document.Fields.Add(new RestSearchField("image_url", _assetUrlResolver.GetAssetUrl<IContentImage>(productContent)));
     document.Fields.Add(new RestSearchField("content_link", productContent.ContentLink.ToString()));
     document.Fields.Add(new RestSearchField("created", productContent.Created.ToString("yyyyMMddhhmmss")));
     document.Fields.Add(new RestSearchField("brand", productContent.Brand));
     document.Fields.Add(new RestSearchField("top_category_name", GetTopCategory(productContent).DisplayName));
     return document;
 }
 private void AddCodes(RestSearchDocument document, IEnumerable<FashionVariant> variants)
 {
     foreach (var variant in variants)
     {
         document.Fields.Add(new RestSearchField("code", variant.Code, new [] { SearchField.Store.YES, SearchField.IncludeInDefaultSearch.YES }));
     }
 }
        private void AddPrices(RestSearchDocument document, IEnumerable<FashionVariant> variants)
        {
            var prices = _priceService.GetCatalogEntryPrices(variants.Select(x => new CatalogKey(_appContext.ApplicationId, x.Code))).ToList();
            var validPrices = prices.Where(x => x.ValidFrom <= DateTime.Now && (x.ValidUntil == null || x.ValidUntil >= DateTime.Now));

            foreach (var marketPrices in validPrices.GroupBy(x => x.MarketId))
            {
                foreach (var currencyPrices in marketPrices.GroupBy(x => x.UnitPrice.Currency))
                {
                    var topPrice = currencyPrices.OrderByDescending(x => x.UnitPrice).FirstOrDefault();
                    if (topPrice == null)
                        continue;

                    var variationPrice = new RestSearchField(IndexingHelper.GetOriginalPriceField(topPrice.MarketId, topPrice.UnitPrice.Currency),
                        topPrice.UnitPrice.Amount.ToString(CultureInfo.InvariantCulture), true);

                    var discountedPrice = new RestSearchField(IndexingHelper.GetPriceField(topPrice.MarketId, topPrice.UnitPrice.Currency),
                        _promotionService.GetDiscountPrice(topPrice.CatalogKey, topPrice.MarketId, topPrice.UnitPrice.Currency).UnitPrice.Amount.ToString(CultureInfo.InvariantCulture), true);

                    document.Fields.Add(variationPrice);
                    document.Fields.Add(discountedPrice);
                }
            }
        }
 private void AddSizes(RestSearchDocument document, IEnumerable<FashionVariant> variants)
 {
     var sizes = new List<string>();
     foreach (var fashionVariant in variants)
     {
         if (!String.IsNullOrEmpty(fashionVariant.Size) && !sizes.Contains(fashionVariant.Size.ToLower()))
         {
             sizes.Add(fashionVariant.Size.ToLower());
             document.Fields.Add(new RestSearchField("size", fashionVariant.Size.ToLower()));
         }
     }
 }
 private void AddColors(RestSearchDocument document, IEnumerable<FashionVariant> variants)
 {
     var colors = new List<string>();
     foreach (var fashionVariant in variants)
     {
         if (!String.IsNullOrEmpty(fashionVariant.Color) && !colors.Contains(fashionVariant.Color.ToLower()))
         {
             colors.Add(fashionVariant.Color.ToLower());
             document.Fields.Add(new RestSearchField("color", fashionVariant.Color.ToLower()));
         }
     }
 }