Example #1
0
        public async Task <IActionResult> Index([FromQuery] FilterResponseViewModel viewModel)
        {
            viewModel.CurrentPage = viewModel.CurrentPage < 1 ? 1 : viewModel.CurrentPage;
            var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;

            int.TryParse(userId, out int id);
            var catalogViewModel = await _catalogViewModelService.GetFilteredCatalogItemsAsync(viewModel, id);

            ViewData["Categories"] = string.Join(",", viewModel.Categories);
            ViewData["Authors"]    = string.Join(",", viewModel.Authors);
            ViewData["Publishers"] = string.Join(",", viewModel.Publishers);
            _logger.LogDebug(string.Join(", ", viewModel.Publishers));

            return(View(catalogViewModel));
        }
Example #2
0
        public async Task <IActionResult> Favorites([FromServices] IUserService userService, FilterResponseViewModel viewModel,
                                                    [FromServices] ICatalogViewModelService catalogViewModelService)
        {
            viewModel.CurrentPage = viewModel.CurrentPage < 1 ? 1 : viewModel.CurrentPage;

            ViewData["Categories"] = string.Join(",", viewModel.Categories);
            ViewData["Authors"]    = string.Join(",", viewModel.Authors);
            ViewData["Publishers"] = string.Join(",", viewModel.Publishers);
            //_logger.LogDebug(string.Join(", ", viewModel.Publishers));

            var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;

            int.TryParse(userId, out int id);
            var model = await userService.GetFavoritesAsync(viewModel, id);

            return(View(model));
        }
Example #3
0
        public async Task <CatalogViewModel> GetFavoritesAsync(FilterResponseViewModel responseFilters, int userId, int pageSize = 30)
        {
            if (pageSize < 1)
            {
                pageSize = 30;
            }

            var(categories, authors, publishers, pageIndex) = responseFilters;
            var favs = await _userRepository.GetFavoritesAsync(userId);

            var books = favs.Select(f => f.Book).AsQueryable();

            var minYear = books.Min(b => b.PublishedDate.Year);
            var maxYear = books.Max(b => b.PublishedDate.Year);

            var allPublishers = books.Select(b => b.Publisher).ToList();
            var allCategories = books.SelectMany(b => b.BookCategories).Select(bp => bp.Category).ToHashSet().ToList();
            var allAuthors    = books.SelectMany(b => b.BookAuthors).Select(ba => ba.Author).ToHashSet().ToList();

            if (authors.Any())
            {
                books = books.Where(b => b.BookAuthors.Any(ba => authors.Contains(ba.AuthorId)));
            }

            if (categories.Any())
            {
                books = books.Where(b => b.BookCategories.Any(bc => categories.Contains(bc.CategoryId)));
            }

            if (publishers.Any())
            {
                books = books.Where(b => publishers.Contains(b.PublisherId));
            }

            var paginatedBooks = await PaginatedList <Book> .CreateAsync(books, pageIndex, pageSize, orderBy : b => b.Title);


            var vm = new CatalogViewModel
            {
                BookItems = paginatedBooks.Select(book => new BookDetailsViewModel
                {
                    Authors     = book.BookAuthors.Select(ba => ba.Author.Name),
                    BookId      = book.Id,
                    Description = book.Description,
                    ImageUrl    = book.ImageUrl,
                    Price       = book.Price,
                    Title       = book.Title,
                    IsFavorite  = true,
                    Categories  = book.BookCategories.Select(bc => new CategoryViewModel {
                        Name = bc.Category.Name, Id = bc.Category.Id
                    }),
                    Publisher     = book.Publisher.Name,
                    PublishedDate = book.PublishedDate
                }),
                Authors = allAuthors.Select(a => new AuthorViewModel()
                {
                    Id         = a.Id,
                    Name       = a.Name,
                    IsSelected = authors.Any(selectedId => selectedId == a.Id)
                }),
                Categories = allCategories.Select(c => new CategoryViewModel()
                {
                    Id         = c.Id,
                    Name       = c.Name,
                    IsSelected = categories.Any(selectedId => selectedId == c.Id)
                }),
                Publishers = allPublishers.Select(p => new PublisherViewModel()
                {
                    Id         = p.Id,
                    Name       = p.Name,
                    IsSelected = publishers.Any(selectedId => selectedId == p.Id)
                }),
                MinYear         = minYear,
                MaxYear         = maxYear,
                TotalItemsFound = paginatedBooks.TotalItems,
                CurrentPage     = paginatedBooks.CurrentPage,
                TotalPages      = paginatedBooks.TotalPages,
                HasNextPage     = paginatedBooks.HasNextPage,
                HasPrevPage     = paginatedBooks.HasPreviouspage,
            };


            return(vm);
        }
Example #4
0
        public async Task <CatalogViewModel> GetFilteredCatalogItemsAsync(FilterResponseViewModel responseFilters, int?userId, int pageSize = 30)
        {
            _logger.LogInformation("GetFilteredCatalogItemsAsync called.");

            if (pageSize < 1)
            {
                pageSize = 30;
            }

            var(categories, authors, publishers, pageIndex) = responseFilters;

            var paginatedBooks = await _bookRepository.GetFilteredBooksWithDataAsync(
                categories, authors, publishers, pageSize, pageIndex);

            var allPublishers = await GetAllPublishers();

            var allCategories = await GetAllCategories();

            var allAuthors = await GetAllAuthors();

            var vm = new CatalogViewModel()
            {
                BookItems = paginatedBooks.Select(pb => new BookItemViewModel()
                {
                    BookId        = pb.Id,
                    Description   = pb.Description,
                    Price         = pb.Price,
                    AverageRating = (decimal?)pb.Reviews.Select(r => r.Rating).Average(),
                    ImageUrl      = pb.ImageUrl,
                    Title         = pb.Title,
                    IsFavorite    = pb.Favorites.Any(f => f.UserId == userId && f.BookId == pb.Id),
                    Authors       = pb.BookAuthors.Where(ba => ba.BookId == pb.Id).Select(ba => ba.Author.Name)
                }),
                Authors = allAuthors.Select(a => new AuthorViewModel()
                {
                    Id         = a.Id,
                    Name       = a.Name,
                    IsSelected = authors.Any(selectedId => selectedId == a.Id)
                }),
                Categories = allCategories.Select(c => new CategoryViewModel()
                {
                    Id         = c.Id,
                    Name       = c.Name,
                    IsSelected = categories.Any(selectedId => selectedId == c.Id)
                }),
                Publishers = allPublishers.Select(p => new PublisherViewModel()
                {
                    Id         = p.Id,
                    Name       = p.Name,
                    IsSelected = publishers.Any(selectedId => selectedId == p.Id)
                }),
                MinYear         = await _bookRepository.GetMinPublishedYear(),
                MaxYear         = await _bookRepository.GetMaxPublishedYear(),
                TotalItemsFound = paginatedBooks.TotalItems,
                CurrentPage     = paginatedBooks.CurrentPage,
                TotalPages      = paginatedBooks.TotalPages,
                HasNextPage     = paginatedBooks.HasNextPage,
                HasPrevPage     = paginatedBooks.HasPreviouspage,
            };

            return(vm);
        }