Example #1
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            if (!_catalogSettings.ShowPopularProductTagsOnHomepage)
            {
                return(Empty());
            }

            var store = Services.StoreContext.CurrentStore;

            var cacheKey   = string.Format(ModelCacheInvalidator.PRODUCTTAG_POPULAR_MODEL_KEY, Services.WorkContext.WorkingLanguage.Id, store.Id);
            var cacheModel = await Services.CacheFactory.GetMemoryCache().GetAsync(cacheKey, async(o) =>
            {
                o.ExpiresIn(TimeSpan.FromHours(1));

                var customer = Services.WorkContext.CurrentCustomer;
                var model    = new PopularProductTagsModel();

                // TODO: (mg) This is gonna explode with large amount of tags. Rethink!
                var allTags = await _db.ProductTags
                              .Where(x => x.Published)
                              .ToListAsync();

                var tags = (from t in allTags
                            let numProducts = _productTagService.CountProductsByTagIdAsync(t.Id, customer, store.Id).Await()
                                              where numProducts > 0
                                              orderby numProducts descending
                                              select new
                {
                    Tag = t,
                    LocalizedName = t.GetLocalized(x => x.Name),
                    NumProducts = numProducts
                }).ToList();

                tags = tags
                       .OrderBy(x => x.LocalizedName.Value)
                       .Take(_catalogSettings.NumberOfProductTags)
                       .ToList();

                model.TotalTags = allTags.Count;

                foreach (var tag in tags)
                {
                    model.Tags.Add(new ProductTagModel
                    {
                        Id           = tag.Tag.Id,
                        Name         = tag.LocalizedName,
                        Slug         = tag.Tag.BuildSlug(),
                        ProductCount = tag.NumProducts
                    });
                }

                return(model);
            });

            return(View(cacheModel));
        }
        public async Task <IActionResult> ProductTagsAll()
        {
            // TODO: (mh) (core) This is nearly the same code as in PopularProductTagsViewComponent > implement helper method PreparePopularProductTagsModel?
            var store    = Services.StoreContext.CurrentStore;
            var customer = Services.WorkContext.CurrentCustomer;
            var model    = new PopularProductTagsModel();

            // TODO: (mg) This is gonna explode with large amount of tags. Rethink!
            var allTags = await _db.ProductTags
                          .Where(x => x.Published)
                          .ToListAsync();

            var tags = (from t in allTags
                        let numProducts = _productTagService.CountProductsByTagIdAsync(t.Id, customer, store.Id).Await()
                                          where numProducts > 0
                                          orderby numProducts descending
                                          select new
            {
                Tag = t,
                LocalizedName = t.GetLocalized(x => x.Name),
                NumProducts = numProducts
            })
                       .OrderBy(x => x.LocalizedName.Value)
                       .ToList();

            foreach (var tag in tags)
            {
                model.Tags.Add(new ProductTagModel
                {
                    Id           = tag.Tag.Id,
                    Name         = tag.LocalizedName,
                    Slug         = tag.Tag.BuildSlug(),
                    ProductCount = tag.NumProducts
                });
            }

            return(View(model));
        }