public IActionResult GetAllBooksForGivenPage([FromBody] BooksFilterDto filter) { if (!ModelState.IsValid) { return(BadRequest()); } var filteringResult = bookService.GetAllBooksForGivenPage(filter).Content; return(Ok(filteringResult)); }
// Metoda pomocnicza zwracająca książki z uwzględnieniem filtrów ustawionych przez użytkownika private IEnumerable <Book> GetFilteringResult(BooksFilterDto filter) { Func <Book, bool> containsTitleOrAuthorSearchingPhrase = x => filter.TitleOrAuthorFilter == null ? true : x.Title.Contains(filter.TitleOrAuthorFilter) || x.BooksAuthors.Any(a => a.Author.FirstName.Contains(filter.TitleOrAuthorFilter) || a.Author.LastName.Contains(filter.TitleOrAuthorFilter) || (a.Author.NickName == null ? false : a.Author.NickName.Contains(filter.TitleOrAuthorFilter))); Func <Book, bool> isElectronicOrNotLikeInFilter = x => filter.IsElectronic == null ? true : x.IsElectronic == filter.IsElectronic; Func <Book, bool> containsAnyOfFiltersCategories = x => filter.CategoriesIds == null || filter.CategoriesIds.Count() == 0 ? true : x.BooksCategories.Select(bc => bc.Category.Id).Intersect(filter.CategoriesIds).Count() > 0; Func <Book, bool> containsAnyOfFiltersLanguages = x => filter.LanguagesIds == null || filter.LanguagesIds.Count() == 0 ? true : x.BooksLanguages.Select(bl => bl.Language.Id).Intersect(filter.LanguagesIds).Count() > 0; Func <Book, bool> youngerThanMinimumReleaseDate = x => !filter.ReleaseMinimumDate.HasValue ? true : x.ReleaseDate > filter.ReleaseMinimumDate; Func <Book, bool> olderThanMaximumReleaseDate = x => !filter.ReleaseMaximumDate.HasValue ? true : x.ReleaseDate < filter.ReleaseMaximumDate; Func <Book, bool> youngerThanMinimumPurchaseDate = x => !filter.PurchaseMinimumDate.HasValue ? true : x.PurchaseByStoreDate > filter.PurchaseMinimumDate; Func <Book, bool> olderThanMaximumPurchaseDate = x => !filter.PurchaseMaximumDate.HasValue ? true : x.PurchaseByStoreDate < filter.PurchaseMaximumDate; Func <Book, bool> cheaperThanMaximumPrice = x => filter.MaximumPrice == null ? true : filter.MaximumPrice > x.Price; var books = context.Books .Include(x => x.BooksPhotos) .ThenInclude(x => x.Photo) .Include(x => x.BooksAuthors) .ThenInclude(x => x.Author) .Include(x => x.BooksCategories) .ThenInclude(x => x.Category) .Include(x => x.BooksLanguages) .ThenInclude(x => x.Language) .OrderByDescending(x => x.PurchaseByStoreDate) .AsEnumerable() .Where(isElectronicOrNotLikeInFilter) .Where(containsTitleOrAuthorSearchingPhrase) .Where(containsAnyOfFiltersCategories) .Where(containsAnyOfFiltersLanguages) .Where(youngerThanMinimumReleaseDate) .Where(olderThanMaximumReleaseDate) .Where(youngerThanMinimumPurchaseDate) .Where(olderThanMaximumPurchaseDate) .Where(cheaperThanMaximumPrice); return(books); }
// Przekazuje książki z bazy danych do panelu administracyjnego. public ContentResult <BooksFilteringResultDto> GetAllBooksForGivenPage(BooksFilterDto filter) { var books = this.GetFilteringResult(filter); var count = books.Count(); books = books.Skip((filter.Page - 1) * filter.BooksPerPage) .Take(filter.BooksPerPage); var dto = new BooksFilteringResultDto { Books = books, TotalBooksCountForThisFilter = count }; return(new ContentResult <BooksFilteringResultDto>() { Content = dto }); }