Пример #1
0
        public async Task <IActionResult> Index(string searchExpr, int?page = 1)
        {
            IEnumerable <Book> books = new List <Book>();
            IndexBookViewModel indexViewModel;

            if (string.IsNullOrEmpty(searchExpr))
            {
                books = await _booksRepository.GetAllBooks();
            }
            else
            {
                books = await _booksRepository.GetBooksByFilter(searchExpr, _genresRepository.GetAllGenres().ToList());
            }
            indexViewModel = new IndexBookViewModel
            {
                Books = _booksRepository.PartBooksForPage(books, page.Value, COUNT_ELEMS_ON_PAGE)
                        .Select(book => BookConverter.ConvertModelToViewModel(book)).ToList(),
                PageViewModel = new PageViewModel(books.Count(), page.Value, COUNT_ELEMS_ON_PAGE),
                SearchExpr    = String.IsNullOrEmpty(searchExpr) ? string.Empty : searchExpr
                                //, ActionName = nameof(this.ShowBooks)
            };
            return(View(indexViewModel));
        }
Пример #2
0
        public async Task <IActionResult> Index(string keywords, string bookPublisher, BookCategories category, AgeGroups ageGroup, string yearFrom, string yearTo, string sortField, int page = 1)
        {
            var user = await _userManager.GetUserAsync(User);

            var books = _context.Books
                        .Include(b => b.Answers)
                        .Include(b => b.BookRatings)
                        .AsQueryable();

            CurrentPage = page == 0 ? 1 : page;

            var publishers = books.Select(b => b.Publisher);

            if (!string.IsNullOrEmpty(keywords))
            {
                books = books.Where(b => b.Title.ToUpper().Contains(keywords.ToUpper()) ||
                                    b.BookAuthor.ToUpper().Contains(keywords.ToUpper()) ||
                                    b.Isbn1.ToUpper().Contains(keywords.ToUpper()) ||
                                    b.Publisher.ToUpper().Contains(keywords.ToUpper()) ||
                                    b.Isbn2.ToUpper().Contains(keywords.ToUpper()));
            }

            if (!string.IsNullOrEmpty(bookPublisher))
            {
                books = books.Where(b => b.Publisher == bookPublisher);
            }

            if (category != 0)
            {
                books = books.Where(b => b.BookCategory == category);
            }

            if (ageGroup != 0)
            {
                books = books.Where(b => b.AgeGroup == ageGroup);
            }

            if (!string.IsNullOrEmpty(yearFrom) && int.TryParse(yearFrom, out int n))
            {
                books = books.Where(b => int.Parse(b.YearPublished) >= n);
            }

            if (!string.IsNullOrEmpty(yearTo) && int.TryParse(yearTo, out int m))
            {
                books = books.Where(b => int.Parse(b.YearPublished) <= m);
            }

            switch (sortField)
            {
            case "latest":
                books = books.OrderByDescending(b => b.DateAdded);
                break;

            case "rating":
                books = books.OrderByDescending(b => b.AverageRating);
                break;

            case "views":
                books = books.OrderByDescending(b => b.Answers.Count);
                break;

            case "class":
                books = books.OrderBy(b => b.Grade);
                break;

            case "year":
                books = books.OrderBy(b => b.YearPublished);
                break;

            case "title":
                books = books.OrderBy(b => b.Title);
                break;

            case "author":
                books = books.OrderBy(b => b.BookAuthor);
                break;

            default:
                books = books.OrderByDescending(b => b.DateAdded);
                break;
            }

            Count = books.Count();

            if (CurrentPage > TotalPages)
            {
                CurrentPage = TotalPages;
            }

            var indexVm = new IndexBookViewModel()
            {
                Books = await books.Skip((CurrentPage - 1) *PageSize)
                        .Take(PageSize)
                        .ToListAsync(),

                Publishers = new SelectList(await publishers.Distinct().ToListAsync()),

                CurrentPage    = CurrentPage,
                Count          = Count,
                PageSize       = PageSize,
                TotalPages     = TotalPages,
                EnablePrevious = EnablePrevious,
                EnableNext     = EnableNext
            };

            if (User.Identity.IsAuthenticated)
            {
                var booksIds = _context.MarkedBooks
                               .Where(mb => mb.UserId == user.Id)
                               .Select(mb => mb.BookId).ToList();

                indexVm.BooksIds = booksIds;
            }
            return(View(indexVm));
        }