コード例 #1
0
        public List <LongBookDTO> SearchBooks(string searchQuery, int pageNumber, List <string> selectedFilters)
        {
            using (var context = new AuthContext())
            {
                var searchResultsQuery = context.Books
                                         .Where(x => x.Name.Contains(searchQuery) || x.Keyword == searchQuery)
                                         .OrderBy(x => x.Name)
                                         .GroupBy(x => x.Name)
                                         .Select(x => x.FirstOrDefault())
                                         .Include(x => x.Categories)
                                         .Include(x => x.Language)
                                         .Include(x => x.Author)
                                         .Include(x => x.YearOfIssue)
                                         .Include(x => x.LocalLibrary);

                // only apply a filter if it is listed in the selected filters
                // Nationalities
                if (selectedFilters.Intersect(context.Nationalities.Select(y => y.Name)).Any())
                {
                    searchResultsQuery = searchResultsQuery.Where(book => selectedFilters.Contains(book.Nationality.Name));
                }

                // Authors
                if (selectedFilters.Intersect(context.Authors.Select(y => y.Name)).Any())
                {
                    searchResultsQuery = searchResultsQuery.Where(book => selectedFilters.Contains(book.Author.Name));
                }

                // Categories
                if (selectedFilters.Intersect(context.Categories.Select(y => y.Name)).Any())
                {
                    searchResultsQuery = searchResultsQuery.Where(book => selectedFilters.Intersect(book.Categories.Select(y => y.Name)).Any());
                }

                // Years
                if (selectedFilters.Intersect(context.YearsOfIssue.Select(y => y.Year.ToString())).Any())
                {
                    searchResultsQuery = searchResultsQuery.Where((book => selectedFilters.Contains(book.YearOfIssue.Year)));
                }

                // Languages
                if (selectedFilters.Intersect(context.Languages.Select(y => y.Name)).Any())
                {
                    searchResultsQuery = searchResultsQuery.Where(book => selectedFilters.Contains(book.Language.Name));
                }

                var books = searchResultsQuery.OrderBy(x => x.Name).Skip(_numberOfBooksPerSearchQuery * pageNumber)
                            .Take(_numberOfBooksPerSearchQuery).ToList();

                return(books.Select(book => LongBookDTO.FromData(book, null))
                       .OrderByDescending(book => book.YearOfIssue)
                       //.ThenByDescending(book => book.Name)
                       .ToList());
            }
        }
コード例 #2
0
 public LongBookDTO GetBookForOneTimeBorrow(int libraryId, string bookName, string authorName)
 {
     using (var context = new AuthContext())
     {
         return(context.Books
                .Where(book => book.Name == bookName && book.Author.Name == authorName && book.LocalLibrary.Id == libraryId)
                .Include("Author")
                .Include("Language")
                .Include("Categories")
                .Include("LocalLibrary")
                .Include("YearOfIssue")
                .ToList()
                .Select(book => LongBookDTO.FromData(book, null))
                .FirstOrDefault());
     }
 }
コード例 #3
0
        public List <List <LongBookDTO> > GetBooksByName(string bookName, bool booksOfLibrariesWithMembership, Guid?userId)
        {
            using (var context = new AuthContext())
            {
                var result = new List <List <LongBookDTO> >();

                var userIdAsString = userId.ToString();

                var allBooksWithName = context.Books
                                       .Include(book => book.Author)
                                       .Include(book => book.Categories)
                                       .Include(book => book.Language)
                                       .Include(book => book.Nationality)
                                       .Include(book => book.YearOfIssue)
                                       .Include(book => book.LocalLibrary)
                                       .OrderBy(book => book.LocalLibrary.Name)
                                       .Include("LocalLibrary.Members")
                                       .Include("LocalLibrary.Members.OnlineUser")
                                       .Where(book => book.Name == bookName)
                                       .ToList();

                if (userId != null)
                {
                    var booksFromUsersLibraries = allBooksWithName
                                                  .Where(book =>
                    {
                        return(book.LocalLibrary.Members.Any(member =>
                        {
                            if (member.OnlineUser == null)
                            {
                                return false;
                            }

                            return member.OnlineUser.Id == userIdAsString;
                        }));
                    }).ToList();



                    var booksThatAreBorrowed = booksFromUsersLibraries.Where(
                        book => context.BatchesOfBorrowedBooks.Any(batch => batch.Books.Select(x => x.Id).Contains(book.Id)) && !book.IsReserved)
                                               .Select(book => LongBookDTO.FromData(book,
                                                                                    context.BatchesOfBorrowedBooks.FirstOrDefault(batch => batch.Books.Select(x => x.Id).Contains(book.Id))
                                                                                    .ReturnDeadline))
                                               .OrderBy(borrowedBook => borrowedBook.ReturnDeadline)
                                               .ToList();

                    var booksThatAreAvailable = booksFromUsersLibraries
                                                .Where(book => !booksThatAreBorrowed.Select(borrowedBook => borrowedBook.BookId).Contains(book.Id) && !book.IsReserved)
                                                .Select(book => LongBookDTO.FromData(book, null))
                                                .ToList();

                    result.Add(booksThatAreBorrowed.Concat(booksThatAreAvailable)
                               .GroupBy(book => book.LocalLibrary.Name)
                               .Select(bookGroup => bookGroup.LastOrDefault())
                               .ToList());
                }

                var allAvailableBooksWithName = allBooksWithName
                                                .Where(book => book.Name == bookName && !book.IsBorrowed && !book.IsReserved)
                                                .ToList();

                if (userId == null)
                {
                    result.Add(null);
                    result.Add(allAvailableBooksWithName.Select(book => LongBookDTO.FromData(book, null)).ToList());

                    return(result);
                }

                var booksFromOtherLibraries = allAvailableBooksWithName
                                              .Where(book =>
                {
                    return(!book.LocalLibrary.Members.Any(member =>
                    {
                        if (member.OnlineUser == null)
                        {
                            return false;
                        }

                        return member.OnlineUser.Id == userIdAsString;
                    }));
                }).ToList();

                result.Add(booksFromOtherLibraries.Select(book => LongBookDTO.FromData(book, null)).DistinctBy(x => x.LocalLibrary.Id).ToList());

                return(result);
            }
        }