public coreModel.Category[] GetByIds(string[] categoryIds, coreModel.CategoryResponseGroup responseGroup, string catalogId = null)
        {
            coreModel.Category[] result;

            using (var repository = _catalogRepositoryFactory())
            {
                result = repository.GetCategoriesByIds(categoryIds, responseGroup)
                         .Select(c => c.ToCoreModel())
                         .ToArray();
            }

            // Fill outlines for products
            if (responseGroup.HasFlag(coreModel.CategoryResponseGroup.WithOutlines))
            {
                _outlineService.FillOutlinesForObjects(result, catalogId);
            }

            // Fill SEO info
            if ((responseGroup & coreModel.CategoryResponseGroup.WithSeo) == coreModel.CategoryResponseGroup.WithSeo)
            {
                var objectsWithSeo = new List <ISeoSupport>(result);

                var outlineItems = result
                                   .Where(c => c.Outlines != null)
                                   .SelectMany(c => c.Outlines.SelectMany(o => o.Items));
                objectsWithSeo.AddRange(outlineItems);

                _commerceService.LoadSeoForObjects(objectsWithSeo.ToArray());
            }

            return(result);
        }
Esempio n. 2
0
        protected virtual Dictionary <string, Category> PreloadCategories(string catalogId)
        {
            var cacheKey = CacheKey.With(GetType(), "PreloadCategories", catalogId);

            return(_memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(CatalogCacheRegion.CreateChangeToken());
                CategoryEntity[] entities;
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();

                    entities = repository.GetCategoriesByIds(repository.Categories.Select(x => x.Id).ToArray());
                }
                var result = entities.Select(x => x.ToModel(AbstractTypeFactory <Category> .TryCreateInstance())).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);

                LoadDependencies(result.Values, result);
                ApplyInheritanceRules(result.Values.OrderBy(x => x.Level));

                // Fill outlines for categories
                _outlineService.FillOutlinesForObjects(result.Values, catalogId);

                var objectsWithSeo = new List <ISeoSupport>(result.Values);
                var outlineItems = result.Values.Where(c => c.Outlines != null).SelectMany(c => c.Outlines.SelectMany(o => o.Items));
                objectsWithSeo.AddRange(outlineItems);
                //TODO: convert to async
                _seoService.LoadSeoForObjectsAsync(objectsWithSeo.ToArray());
                return result;
            }));
        }
        protected virtual Dictionary <string, Category> PreloadCategories(string catalogId)
        {
            return(_cacheManager.Get($"AllCategories-{catalogId}", CatalogConstants.CacheRegion, () =>
            {
                CategoryEntity[] entities;
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();

                    entities = repository.GetCategoriesByIds(repository.Categories.Select(x => x.Id).ToArray(), CategoryResponseGroup.Full);
                }
                var result = entities.Select(x => x.ToModel(AbstractTypeFactory <Category> .TryCreateInstance())).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);

                LoadDependencies(result.Values, result);
                ApplyInheritanceRules(result.Values);

                // Fill outlines for categories
                _outlineService.FillOutlinesForObjects(result.Values, catalogId);

                var objectsWithSeo = new List <ISeoSupport>(result.Values);
                var outlineItems = result.Values.Where(c => c.Outlines != null).SelectMany(c => c.Outlines.SelectMany(o => o.Items));
                objectsWithSeo.AddRange(outlineItems);
                _commerceService.LoadSeoForObjects(objectsWithSeo.ToArray());

                return result;
            }));
        }
        protected virtual async Task <IDictionary <string, Category> > PreloadCategoriesAsync(string catalogId)
        {
            var cacheKey = CacheKey.With(GetType(), "PreloadCategories", catalogId);

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(CatalogCacheRegion.CreateChangeToken());

                CategoryEntity[] entities;
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();

                    entities = await repository.GetCategoriesByIdsAsync(repository.Categories.Select(x => x.Id).ToArray(), CategoryResponseGroup.Full);
                }
                var result = entities.Select(x => x.ToModel(AbstractTypeFactory <Category> .TryCreateInstance()))
                             .ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase)
                             .WithDefaultValue(null);

                await LoadDependenciesAsync(result.Values, result);
                ApplyInheritanceRules(result.Values);

                // Fill outlines for categories
                _outlineService.FillOutlinesForObjects(result.Values, catalogId);
                return result;
            }));
        }
        public coreModel.CatalogProduct[] GetByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup, string catalogId = null)
        {
            coreModel.CatalogProduct[] result;

            using (var repository = _catalogRepositoryFactory())
            {
                result = repository.GetItemByIds(itemIds, respGroup)
                         .Select(x => x.ToCoreModel())
                         .ToArray();
            }

            // Fill outlines for products
            if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines))
            {
                _outlineService.FillOutlinesForObjects(result, catalogId);
            }

            // Fill SEO info for products, variations and outline items
            if ((respGroup & coreModel.ItemResponseGroup.Seo) == coreModel.ItemResponseGroup.Seo)
            {
                var objectsWithSeo = new List <ISeoSupport>(result);

                var variations = result.Where(p => p.Variations != null)
                                 .SelectMany(p => p.Variations);
                objectsWithSeo.AddRange(variations);

                var outlineItems = result.Where(p => p.Outlines != null)
                                   .SelectMany(p => p.Outlines.SelectMany(o => o.Items));
                objectsWithSeo.AddRange(outlineItems);

                _commerceService.LoadSeoForObjects(objectsWithSeo.ToArray());
            }

            return(result);
        }
        public virtual async Task <CatalogProduct[]> GetByIdsAsync(string[] itemIds, string respGroup, string catalogId = null)
        {
            var itemResponseGroup = EnumUtility.SafeParseFlags(respGroup, ItemResponseGroup.ItemLarge);

            var cacheKey = CacheKey.With(GetType(), nameof(GetByIdsAsync), string.Join("-", itemIds), itemResponseGroup.ToString(), catalogId);
            var result   = await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                var products = Array.Empty <CatalogProduct>();

                if (!itemIds.IsNullOrEmpty())
                {
                    using (var repository = _repositoryFactory())
                    {
                        //Optimize performance and CPU usage
                        repository.DisableChangesTracking();

                        //It is so important to generate change tokens for all ids even for not existing objects to prevent an issue
                        //with caching of empty results for non - existing objects that have the infinitive lifetime in the cache
                        //and future unavailability to create objects with these ids.
                        cacheEntry.AddExpirationToken(ItemCacheRegion.CreateChangeToken(itemIds));
                        cacheEntry.AddExpirationToken(CatalogCacheRegion.CreateChangeToken());

                        products = (await repository.GetItemByIdsAsync(itemIds, respGroup))
                                   .Select(x => x.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance()))
                                   .ToArray();
                    }

                    if (products.Any())
                    {
                        products = products.OrderBy(x => Array.IndexOf(itemIds, x.Id)).ToArray();
                        await LoadDependenciesAsync(products);
                        ApplyInheritanceRules(products);

                        var productsWithVariationsList = products.Concat(products.Where(p => p.Variations != null)
                                                                         .SelectMany(p => p.Variations)).ToArray();

                        // Fill outlines for products and variations
                        if (itemResponseGroup.HasFlag(ItemResponseGroup.Outlines))
                        {
                            _outlineService.FillOutlinesForObjects(productsWithVariationsList, catalogId);
                        }
                        //Add change tokens for products with variations
                        cacheEntry.AddExpirationToken(ItemCacheRegion.CreateChangeToken(productsWithVariationsList));

                        //Reduce details according to response group
                        foreach (var product in productsWithVariationsList)
                        {
                            product.ReduceDetails(itemResponseGroup.ToString());
                        }
                    }
                }

                return(products);
            });

            return(result.Select(x => x.Clone() as CatalogProduct).ToArray());
        }
Esempio n. 7
0
        public virtual async Task <CatalogProduct[]> GetByIdsAsync(string[] itemIds, string responseGroup, string catalogId = null)
        {
            var itemResponseGroup = EnumUtility.SafeParseFlags(responseGroup, ItemResponseGroup.ItemLarge);

            var cacheKey = CacheKey.With(GetType(), "GetByIdsAsync", string.Join("-", itemIds), itemResponseGroup.ToString(), catalogId);

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                CatalogProduct[] result;

                using (var repository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    repository.DisableChangesTracking();

                    result = (await repository.GetItemByIdsAsync(itemIds, itemResponseGroup))
                             .Select(x => x.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance()))
                             .ToArray();
                }

                await LoadDependenciesAsync(result);
                ApplyInheritanceRules(result);

                var productsWithVariationsList = result.Concat(result.Where(p => p.Variations != null)
                                                               .SelectMany(p => p.Variations)).ToArray();

                // Fill outlines for products and variations
                if (itemResponseGroup.HasFlag(ItemResponseGroup.Outlines))
                {
                    _outlineService.FillOutlinesForObjects(productsWithVariationsList, catalogId);
                }

                // Fill SEO info for products, variations and outline items
                if (itemResponseGroup.HasFlag(ItemResponseGroup.Seo))
                {
                    var objectsWithSeo = productsWithVariationsList.OfType <ISeoSupport>().ToList();
                    //Load SEO information for all Outline.Items
                    var outlineItems = productsWithVariationsList.Where(p => p.Outlines != null)
                                       .SelectMany(p => p.Outlines.SelectMany(o => o.Items));
                    objectsWithSeo.AddRange(outlineItems);
                    await _seoService.LoadSeoForObjectsAsync(objectsWithSeo.ToArray());
                }

                //Reduce details according to response group
                foreach (var product in productsWithVariationsList)
                {
                    product.ReduceDetails(itemResponseGroup.ToString());
                    cacheEntry.AddExpirationToken(ItemCacheRegion.CreateChangeToken(product));
                }

                return result;
            }));
        }
        public virtual CatalogProduct[] GetByIds(string[] itemIds, ItemResponseGroup respGroup, string catalogId = null)
        {
            var result = Array.Empty <CatalogProduct>();

            if (!itemIds.IsNullOrEmpty())
            {
                using (var repository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    repository.DisableChangesTracking();

                    result = repository.GetItemByIds(itemIds, respGroup)
                             .Select(x => x.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance()))
                             .ToArray();
                }

                if (result.Any())
                {
                    LoadDependencies(result);
                    ApplyInheritanceRules(result);

                    var productsWithVariationsList = result.Concat(result.Where(p => p.Variations != null)
                                                                   .SelectMany(p => p.Variations));
                    // Fill outlines for products and variations
                    if (respGroup.HasFlag(ItemResponseGroup.Outlines))
                    {
                        _outlineService.FillOutlinesForObjects(productsWithVariationsList, catalogId);
                    }
                    // Fill SEO info for products, variations and outline items
                    if ((respGroup & ItemResponseGroup.Seo) == ItemResponseGroup.Seo)
                    {
                        var objectsWithSeo = productsWithVariationsList.OfType <ISeoSupport>().ToList();
                        //Load SEO information for all Outline.Items
                        var outlineItems = productsWithVariationsList.Where(p => p.Outlines != null)
                                           .SelectMany(p => p.Outlines.SelectMany(o => o.Items));
                        objectsWithSeo.AddRange(outlineItems);
                        _commerceService.LoadSeoForObjects(objectsWithSeo.ToArray());
                    }

                    //Reduce details according to response group
                    foreach (var product in productsWithVariationsList)
                    {
                        ReduceDetails(product, respGroup);
                    }
                }
            }

            return(result);
        }
Esempio n. 9
0
        public virtual IEnumerable <CatalogProduct> GetByIds(IEnumerable <string> itemIds, string respGroup = null, string catalogId = null)
        {
            CatalogProduct[] result;
            var itemRespGroup = EnumUtility.SafeParse(respGroup, ItemResponseGroup.ItemLarge);

            using (var repository = _repositoryFactory())
            {
                //Optimize performance and CPU usage
                repository.DisableChangesTracking();

                result = repository.GetItemByIds(itemIds.ToArray(), itemRespGroup)
                         .Select(x => x.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance()))
                         .ToArray();
            }

            InnerLoadDependencies(result);
            ApplyInheritanceRules(result);

            var productsWithVariationsList = result.Concat(result.Where(p => p.Variations != null)
                                                           .SelectMany(p => p.Variations));

            // Fill outlines for products and variations
            if (itemRespGroup.HasFlag(ItemResponseGroup.Outlines))
            {
                _outlineService.FillOutlinesForObjects(productsWithVariationsList, catalogId);
            }
            // Fill SEO info for products, variations and outline items
            if ((itemRespGroup & ItemResponseGroup.Seo) == ItemResponseGroup.Seo)
            {
                var objectsWithSeo = productsWithVariationsList.OfType <ISeoSupport>().ToList();
                //Load SEO information for all Outline.Items
                var outlineItems = productsWithVariationsList.Where(p => p.Outlines != null)
                                   .SelectMany(p => p.Outlines.SelectMany(o => o.Items));
                objectsWithSeo.AddRange(outlineItems);
                //TODO: convert to async
                _seoService.LoadSeoForObjectsAsync(objectsWithSeo.ToArray());
            }

            //Reduce details according to response group
            foreach (var product in productsWithVariationsList)
            {
                product.ReduceDetails(respGroup);
            }

            return(result);
        }
        public virtual coreModel.Category[] GetByIds(string[] categoryIds, coreModel.CategoryResponseGroup responseGroup, string catalogId = null)
        {
            coreModel.Category[] result;

            using (var repository = base.CatalogRepositoryFactory())
            {
                result = repository.GetCategoriesByIds(categoryIds, responseGroup)
                         .Select(c => c.ToCoreModel(base.AllCachedCatalogs, base.AllCachedCategories))
                         .ToArray();
            }

            // Fill outlines for products
            if (responseGroup.HasFlag(coreModel.CategoryResponseGroup.WithOutlines))
            {
                _outlineService.FillOutlinesForObjects(result, catalogId);
            }

            if ((responseGroup & coreModel.CategoryResponseGroup.WithSeo) == coreModel.CategoryResponseGroup.WithSeo)
            {
                var objectsWithSeo = new List <ISeoSupport>(result);

                var outlineItems = result
                                   .Where(c => c.Outlines != null)
                                   .SelectMany(c => c.Outlines.SelectMany(o => o.Items));
                objectsWithSeo.AddRange(outlineItems);

                _commerceService.LoadSeoForObjects(objectsWithSeo.ToArray());
            }

            //Cleanup result model considered requested response group
            foreach (var category in result)
            {
                if (!responseGroup.HasFlag(coreModel.CategoryResponseGroup.WithParents))
                {
                    category.Parents = null;
                }
                if (!responseGroup.HasFlag(coreModel.CategoryResponseGroup.WithProperties))
                {
                    category.Properties = null;
                }
            }
            return(result);
        }
Esempio n. 11
0
        public virtual CatalogProduct[] GetByIds(string[] itemIds, ItemResponseGroup respGroup, string catalogId = null)
        {
            CatalogProduct[] result;

            using (var repository = _repositoryFactory())
            {
                //Optimize performance and CPU usage
                repository.DisableChangesTracking();

                result = repository.GetItemByIds(itemIds, respGroup)
                         .Select(x => x.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance()))
                         .ToArray();
            }

            LoadDependencies(result);
            ApplyInheritanceRules(result);

            // Fill outlines for products
            if (respGroup.HasFlag(ItemResponseGroup.Outlines))
            {
                _outlineService.FillOutlinesForObjects(result, catalogId);
            }

            // Fill SEO info for products, variations and outline items
            if ((respGroup & ItemResponseGroup.Seo) == ItemResponseGroup.Seo)
            {
                var objectsWithSeo = new List <ISeoSupport>(result);

                var variations = result.Where(p => p.Variations != null)
                                 .SelectMany(p => p.Variations);
                objectsWithSeo.AddRange(variations);

                var outlineItems = result.Where(p => p.Outlines != null)
                                   .SelectMany(p => p.Outlines.SelectMany(o => o.Items));
                objectsWithSeo.AddRange(outlineItems);

                _commerceService.LoadSeoForObjects(objectsWithSeo.ToArray());
            }

            //Reduce details according to response group
            foreach (var product in result)
            {
                if (!respGroup.HasFlag(ItemResponseGroup.ItemAssets))
                {
                    product.Assets = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.ItemAssociations))
                {
                    product.Associations = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.ReferencedAssociations))
                {
                    product.ReferencedAssociations = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.ItemEditorialReviews))
                {
                    product.Reviews = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.Inventory))
                {
                    product.Inventories = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.ItemProperties))
                {
                    product.Properties = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.Links))
                {
                    product.Links = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.Outlines))
                {
                    product.Outlines = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.Seo))
                {
                    product.SeoInfos = null;
                }
                if (!respGroup.HasFlag(ItemResponseGroup.Variations))
                {
                    product.Variations = null;
                }
            }

            return(result);
        }