// TODO: (mg) (core) Check whether IProductAttributeMaterializer.PrefetchProductVariantAttributes is still required.
        // Looks like it can be done by MaterializeProductVariantAttributeValuesAsync.

        public virtual async Task <IList <ProductVariantAttribute> > MaterializeProductVariantAttributesAsync(ProductVariantAttributeSelection selection)
        {
            Guard.NotNull(selection, nameof(selection));

            var ids = selection.AttributesMap
                      .Select(x => x.Key)
                      .ToArray();

            if (!ids.Any())
            {
                return(new List <ProductVariantAttribute>());
            }

            var cacheKey = ATTRIBUTES_BY_IDS_KEY + string.Join(",", ids);

            var result = await _requestCache.GetAsync(cacheKey, async() =>
            {
                var query = _db.ProductVariantAttributes
                            .AsNoTracking()
                            .Include(x => x.Product)
                            .Include(x => x.ProductAttribute)
                            .Include(x => x.ProductVariantAttributeValues)
                            .Where(x => ids.Contains(x.Id))
                            .OrderBy(x => x.DisplayOrder);

                var attributes = await query.ToListAsync();

                return(attributes.OrderBySequence(ids).ToList());
            });

            return(result);
        }
示例#2
0
        public async Task <ExpandoObject> GetThemeVariablesAsync(string themeName, int storeId)
        {
            if (themeName.IsEmpty())
            {
                return(null);
            }

            if (!_themeRegistry.ContainsTheme(themeName))
            {
                return(null);
            }

            string key = string.Format(THEMEVARS_BY_THEME_KEY, themeName, storeId);

            return(await _requestCache.GetAsync(key, async() =>
            {
                var result = new ExpandoObject();
                var dict = result as IDictionary <string, object>;

                // First get all default (static) var values from manifest...
                var manifest = _themeRegistry.GetThemeManifest(themeName);
                manifest.Variables.Values.Each(v =>
                {
                    dict.Add(v.Name, v.DefaultValue);
                });

                // ...then merge with persisted runtime records
                var dbVars = await _db.ThemeVariables
                             .AsNoTracking()
                             .Where(v => v.StoreId == storeId && v.Theme == themeName)
                             .ToListAsync();

                dbVars.Each(v =>
                {
                    if (v.Value.HasValue() && dict.ContainsKey(v.Name))
                    {
                        dict[v.Name] = v.Value;
                    }
                });

                return result;
            }));
        }