Exemple #1
0
        public async Task <CategoryViewModel> GetCategoriesAsync(long?categoryId = null, int levels = 1)
        {
            CategoryViewModel result;

            if (categoryId.HasValue)
            {
                var category = await _catalogApi.GetCategoryAsync(_catalogApi.GetCategoryUri(categoryId.Value)).ConfigureAwait(false);

                if (category == null)
                {
                    result = null;
                }
                else
                {
                    var parent = CreateCategoryViewModel(category, category.Id);
                    await GenerateChildCategoryViewModels(parent, category.Categories, levels).ConfigureAwait(false);

                    result = parent;
                }
            }
            else
            {
                var categories = await _catalogApi.GetCategoriesAsync().ConfigureAwait(false);

                var categoryViewModel = new CategoryViewModel
                {
                    CategoryId  = 0,
                    DisplayName = "Root",
                    Image       = string.Empty
                };

                await GenerateChildCategoryViewModels(categoryViewModel, categories, levels).ConfigureAwait(false);

                result = categoryViewModel;
            }

            return(result);
        }