public IActionResult Browse(int?categoryId, string?searchInput) { List <Book> bookList = null; Category selectedCategory = null; if (categoryId == null) { bookList = _dbContext.Books.ToList(); } else { bookList = _dbContext.Books .Where(x => x.BookCategories.Any(c => c.CategoryId == categoryId)).ToList(); selectedCategory = _dbContext.Categories.Find(categoryId); } if (!IsNullOrEmpty(searchInput)) { bookList = bookList.Where(x => x.Title.ToUpper().Contains(searchInput.ToUpper()) || x.Author.ToUpper().Contains(searchInput.ToUpper())).ToList(); } var browseBooksViewModel = new BrowseBooksViewModel() { Categories = _dbContext.Categories.OrderBy(c => c.CategoryName).ToList(), BooksToDisplay = bookList, SelectedCategory = selectedCategory, SearchInput = searchInput }; return(View(browseBooksViewModel)); }
public IActionResult BrowseBooks(BrowseBooksViewModel model) { if (!model.PagesRangeSelected) { model.PagesMin = null; model.PagesMax = null; } if (model.Category == null) { var firstCategory = _manage.GetAllCategories().FirstOrDefault(); if (firstCategory != default(Category)) { model.Category = firstCategory.Name; } } model.Books = _manage.GetBooks(model.Title, model.ISBN, model.Author, model.PagesMin, model.PagesMax, model.Publisher, model.Category); model.Categories = _manage.GetAllCategories(); var elementsCount = 0; if (!(model.Books is null)) { elementsCount = model.Books.Count(); } var allPagesCount = elementsCount / model.ElementsOnPage; var elementsToTake = model.ElementsOnPage; if (elementsCount % model.ElementsOnPage != 0) { allPagesCount++; } if (model.Page < 1) { model.Page = 1; } if (model.Page > allPagesCount) { model.Page = model.AllPagesCount; } model.AllPagesCount = allPagesCount; if (model.Page.Equals(model.AllPagesCount)) { elementsToTake = elementsCount - ((model.AllPagesCount - 1) * model.ElementsOnPage); } if (elementsCount > 0) { model.Books = model.Books.Skip((model.Page - 1) * model.ElementsOnPage).Take(elementsToTake); model.AnyElements = true; } else { model.AnyElements = false; } if (elementsCount <= model.ElementsOnPage) { model.MoreThanOnePage = false; } return(View(model)); }