Пример #1
0
        public async Task <IActionResult> Index()
        {
            var vm       = new BooksViewModel();
            var allBooks = await _bookService.ReadAllAsync();

            var allCategories = await _categoryService.ReadAllAsync();

            var allCategoryAreas = await _categoryAreaService.ReadAllAsync();

            foreach (var area in allCategoryAreas.OrderBy(ca => ca.Name))
            {
                var areaVm = new CategoryAreaItemViewModel
                {
                    Id   = area.Id,
                    Name = area.Name,
                    Ref  = $"area-{area.Id}"
                };
                var categories = allCategories.Where(c => c.CategoryArea?.Id == area.Id)
                                 .OrderBy(c => c.Name)
                                 .ToList();
                foreach (var category in categories)
                {
                    var categoryVm = new CategoryItemViewModel
                    {
                        Id   = category.Id,
                        Name = category.Name,
                        Ref  = $"category-{category.Id}"
                    };
                    var books = allBooks.Where(b => b.Category?.Id == category.Id)
                                .OrderByDescending(b => b.PublishYear)
                                .ThenBy(b => b.Name)
                                .ToList();
                    foreach (var book in books)
                    {
                        var bookVm = new BookItemViewModel
                        {
                            Id          = book.Id,
                            Title       = book.Name,
                            Year        = book.PublishYear,
                            Authors     = book.Authors,
                            Rating      = book.Rating,
                            ImageUrl    = book.ImageUri,
                            AmazonUrl   = book.AmazonUri,
                            DownloadUrl = book.ContentUri,
                            Reflection  = book.Reflection
                        };
                        categoryVm.Books.Add(bookVm);
                    }
                    areaVm.Categories.Add(categoryVm);
                }
                vm.CategoryAreas.Add(areaVm);
            }

            return(View(vm));
        }
Пример #2
0
        public async Task <IEnumerable <CategoryAreaDto> > Get()
        {
            var categoryAreas = await _categoryAreaService.ReadAllAsync();

            return(categoryAreas.Select(ca => CategoryAreaDto.ConvertFrom(ca)).ToList());
        }