public static IQueryable <Book> SearchBook(SeboDbContext _context, string sortOrder, params string[] keywords) { IQueryable <Book> books = (from b in _context.Book select b); if (keywords.Length > 0) { var predicate = PredicateBuilder.False <Book>(); foreach (string keyword in keywords) { string temp = keyword; predicate = predicate.Or(p => p.Title.Contains(temp)).Or(p => p.Description.Contains(temp)).Or(p => p.ISBN.Contains(temp)); } books = (from b in _context.Book select b).Where(predicate); } books = OrderingBooks.Do(books, sortOrder); return(books); }
// GET: Books public async Task <IActionResult> BooksCatalog(string UserName, string sortOrder, string currentSearchString, string SearchString, int StudyAreaFilter, int BookConditionFilter, int?Page) { if (currentSearchString != null) { string[] m = currentSearchString.Split("."); currentSearchString = m[0]; // preserves the current filter typed within the search textbox if (SearchString == null) { SearchString = currentSearchString; } if (StudyAreaFilter == 0) { StudyAreaFilter = Int32.Parse(m[1]); // preserves the select institution option in the dropdownlist } if (BookConditionFilter == 0) { BookConditionFilter = Int32.Parse(m[2]); // preserves the select Study Area option in the dropdownlist } } //Get the User ID int UserId = HelperUser.GetUserId(this.User.Identity.Name, _context); int UserBranchId = HelperUser.GetUserBranchId(this.User.Identity.Name, _context); // Cheks whether a search string was typed and prepares for search by each word string[] myString = SearchString != null?SearchString.Trim().Split(" ") : new string[0]; // Get the seach's recordset already sorted var books = StringSearch.SearchBook(_context, sortOrder, myString).Include(b => b.BookCondition).Include(b => b.StudyArea).Include(b => b.User) .Where(b => !b.Blocked) .Where(b => b.Quantity > b.QuantitySold) .Where(b => b.UserId != UserId) .Where(b => !b.IsWaitList); var users = (from u in _context.User select u).Include(u => u.InstitutionBranch); //Getting the books from the same institution var books2 = (from b in books join u in users on b.UserId equals u.UserId select new { b.Blocked, b.BookCondition, b.BookConditionId, b.BookId, b.CreationDate, b.Description, b.Edition, b.ISBN, b.IsWaitList, b.Orders, b.PhotoFileName, b.Price, b.Publisher, b.Quantity, b.QuantitySold, b.StudyArea, b.StudyAreaId, b.Title, b.User, b.UserId, b.Visualizations, u.InstitutionBranchId, u.InstitutionBranch.InstitutionBranchName }).Where(b => b.InstitutionBranchId == UserBranchId); // Applying filters on the table if (StudyAreaFilter != 0) { books2 = books2.Where(b => b.StudyAreaId == StudyAreaFilter); } if (BookConditionFilter != 0) { books2 = books2.Where(b => b.BookConditionId == BookConditionFilter); } // // Preparing Dropboxes // var StudyAreas = (from s in books2 orderby s.StudyArea.StudyAreaName select new { s.StudyAreaId, s.StudyArea.StudyAreaName }).ToList().Distinct(); var BookConditions = (from b in books2 orderby b.BookCondition select new { b.BookConditionId, b.BookCondition.Condition }).ToList().Distinct(); ViewData["StudyAreaFilter"] = new SelectList(StudyAreas, "StudyAreaId", "StudyAreaName"); ViewData["BookConditionFilter"] = new SelectList(BookConditions, "BookConditionId", "Condition"); ////////////////////////////////////// ViewData["CurrentSearchString"] = SearchString + "." + StudyAreaFilter + "." + BookConditionFilter + "." + Page; ViewData["SearchString"] = SearchString; // // Just tracking which ordering column was trigged for setting the actual ordering // ViewData["Title"] = OrderingBooks.NewOrder(sortOrder, "Title"); ViewData["StudyArea"] = OrderingBooks.NewOrder(sortOrder, "StudyArea"); ViewData["BookCondition"] = OrderingBooks.NewOrder(sortOrder, "BookCondition"); ViewData["ISBN"] = OrderingBooks.NewOrder(sortOrder, "ISBN"); ViewData["Price"] = OrderingBooks.NewOrder(sortOrder, "Price"); int PageSize = 14; return(View(await Pagination <Book> .CreateAsync(books.AsNoTracking(), Page ?? 1, PageSize))); }
public async Task <IActionResult> Index(string UserName, string sortOrder, string currentSearchString, string SearchString, int StudyAreaFilter, int BookConditionFilter, int?Page) { if (currentSearchString != null) { string[] m = currentSearchString.Split("."); currentSearchString = m[0]; // preserves the current filter typed within the search textbox if (SearchString == null) { SearchString = currentSearchString; } if (StudyAreaFilter == 0) { StudyAreaFilter = Int32.Parse(m[1]); // preserves the select institution option in the dropdownlist } if (BookConditionFilter == 0) { BookConditionFilter = Int32.Parse(m[2]); // preserves the select Study Area option in the dropdownlist } } //Get the User ID if (String.IsNullOrEmpty(UserName)) { UserName = this.User.Identity.Name; } int UserId = HelperUser.GetUserId(UserName, _context); // Cheks whether a search string was typed and prepares for search by each word string[] myString = SearchString != null?SearchString.Trim().Split(" ") : new string[0]; var x = HelperUser.isAdministrator(UserName); // Get the seach's recordset already sorted applying filtering according to user role var books = StringSearch.SearchBook(_context, sortOrder, myString).Include(b => b.BookCondition).Include(b => b.StudyArea).Include(b => b.User) .Where(b => !b.Blocked) .Where(b => b.Quantity > b.QuantitySold) .Where(b => !b.IsWaitList) .Where(b => HelperUser.isAdministrator(UserName) ? b.UserId > 0 : b.UserId == UserId); //.Where(b => UserId > 0 || UserName != null ? b.UserId == UserId : b.UserId > 0) // Applying filters on the table if (StudyAreaFilter != 0) { books = books.Where(b => b.StudyAreaId == StudyAreaFilter); } if (BookConditionFilter != 0) { books = books.Where(b => b.BookConditionId == BookConditionFilter); } // // Preparing Dropboxes // var StudyAreas = (from s in books orderby s.StudyArea.StudyAreaName select new { s.StudyAreaId, s.StudyArea.StudyAreaName }).ToList().Distinct(); var BookConditions = (from b in books orderby b.BookCondition select new { b.BookConditionId, b.BookCondition.Condition }).ToList().Distinct(); ViewData["StudyAreaFilter"] = new SelectList(StudyAreas, "StudyAreaId", "StudyAreaName"); ViewData["BookConditionFilter"] = new SelectList(BookConditions, "BookConditionId", "Condition"); ////////////////////////////////////// ViewData["CurrentSearchString"] = SearchString + "." + StudyAreaFilter + "." + BookConditionFilter + "." + Page; ViewData["SearchString"] = SearchString; // // Just tracking which ordering column was trigged for setting the actual ordering // ViewData["Title"] = OrderingBooks.NewOrder(sortOrder, "Title"); ViewData["StudyArea"] = OrderingBooks.NewOrder(sortOrder, "StudyArea"); ViewData["BookCondition"] = OrderingBooks.NewOrder(sortOrder, "BookCondition"); ViewData["ISBN"] = OrderingBooks.NewOrder(sortOrder, "ISBN"); ViewData["Price"] = OrderingBooks.NewOrder(sortOrder, "Price"); int PageSize = 14; return(View(await Pagination <Book> .CreateAsync(books.AsNoTracking(), Page ?? 1, PageSize))); }