private async Task <List <CategorySimpleModel> > PrepareCategorySimpleModels(GetCategorySimple request)
        {
            string cacheKey = string.Format(ModelCacheEventConst.CATEGORY_ALL_MODEL_KEY,
                                            request.Language.Id,
                                            string.Join(",", request.Customer.GetCustomerRoleIds()),
                                            request.Store.Id);

            return(await _cacheManager.GetAsync(cacheKey, () => PrepareCategorySimpleModels(request, "")));
        }
        private async Task <List <CategorySimpleModel> > PrepareCategorySimpleModels(GetCategorySimple request, string rootCategoryId,
                                                                                     bool loadSubCategories = true, IList <Category> allCategories = null)
        {
            var result = new List <CategorySimpleModel>();

            if (allCategories == null)
            {
                allCategories = await _categoryService.GetAllCategories(storeId : request.Store.Id);
            }
            var categories = allCategories.Where(c => c.ParentCategoryId == rootCategoryId).ToList();

            foreach (var category in categories)
            {
                var picture = await _pictureService.GetPictureById(category.PictureId);

                var categoryModel = new CategorySimpleModel {
                    Id                = category.Id,
                    Name              = category.GetLocalized(x => x.Name, request.Language.Id),
                    SeName            = category.GetSeName(request.Language.Id),
                    IncludeInTopMenu  = category.IncludeInTopMenu,
                    Flag              = category.GetLocalized(x => x.Flag, request.Language.Id),
                    FlagStyle         = category.FlagStyle,
                    Icon              = category.Icon,
                    ImageUrl          = await _pictureService.GetPictureUrl(picture, _mediaSettings.CategoryThumbPictureSize),
                    GenericAttributes = category.GenericAttributes
                };

                //product number for each category
                if (_catalogSettings.ShowCategoryProductNumber)
                {
                    var categoryIds = new List <string>();
                    categoryIds.Add(category.Id);
                    //include subcategories
                    if (_catalogSettings.ShowCategoryProductNumberIncludingSubcategories)
                    {
                        categoryIds.AddRange(await _mediator.Send(new GetChildCategoryIds()
                        {
                            Customer = request.Customer, Store = request.Store, ParentCategoryId = category.Id
                        }));
                    }
                    categoryModel.NumberOfProducts = _productService.GetCategoryProductNumber(request.Customer, categoryIds, request.Store.Id);
                }
                if (loadSubCategories)
                {
                    var subCategories = await PrepareCategorySimpleModels(request, category.Id, loadSubCategories, allCategories);

                    categoryModel.SubCategories.AddRange(subCategories);
                }
                result.Add(categoryModel);
            }

            return(result);
        }
        private async Task <List <CategorySimpleModel> > PrepareCategorySimpleModels(GetCategorySimple request)
        {
            var currentCategory = await _categoryService.GetCategoryById(request.CurrentCategoryId);

            string cacheKey = string.Format(CacheKeyConst.CATEGORY_ALL_MODEL_KEY,
                                            request.Language.Id,
                                            request.Store.Id,
                                            string.Join(",", request.Customer.GetCustomerGroupIds()),
                                            request.CurrentCategoryId);

            return(await _cacheBase.GetAsync(cacheKey, async() =>
            {
                var categories = new List <Category>();

                async Task PrepareCategories(string categoryId)
                {
                    var parentCategories = await _categoryService.GetAllCategories(parentId: categoryId);
                    if (parentCategories.Any())
                    {
                        categories.AddRange(parentCategories);
                        var parent = await _categoryService.GetCategoryById(parentCategories.FirstOrDefault().ParentCategoryId);
                        if (parent != null)
                        {
                            await PrepareCategories(parent.ParentCategoryId);
                        }
                    }
                }
                if (currentCategory != null)
                {
                    var currentCategories = await _categoryService.GetAllCategories(parentId: currentCategory.Id);
                    categories.AddRange(currentCategories);
                    await PrepareCategories(currentCategory.ParentCategoryId);
                }
                else
                {
                    await PrepareCategories("");
                }

                return await PrepareCategorySimpleModels(request, "", true, categories.ToList());
            }));
 public async Task <IList <CategorySimpleModel> > Handle(GetCategorySimple request, CancellationToken cancellationToken)
 {
     return(await PrepareCategorySimpleModels(request));
 }