コード例 #1
0
        public async Task <PagedResponse <FoundBookDTO> > Handle(SearchBookQuery request, CancellationToken cancellationToken)
        {
            BookSearchCondition searchCondition = new BookSearchCondition()
            {
                Title            = GetFilterValues(request.SearchCondition.Title),
                Year             = request.SearchCondition.Year,
                AmountPage       = request.SearchCondition.AmountPage,
                AuthorName       = GetFilterValues(request.SearchCondition.AuthorName),
                AuthorSurname    = GetFilterValues(request.SearchCondition.AuthorSurname),
                AuthorPatronymic = GetFilterValues(request.SearchCondition.AuthorPatronymic),
                Genre            = GetFilterValues(request.SearchCondition.Genre),
                Publisher        = GetFilterValues(request.SearchCondition.Publisher),
                Page             = request.SearchCondition.Page,
                PageSize         = request.SearchCondition.PageSize,
                SortDirection    = request.SearchCondition.SortDirection,
                SortProperty     = request.SearchCondition.SortProperty
            };

            var sortProperty = GetSortProperty(searchCondition.SortProperty);
            IReadOnlyCollection <Book> foundBook = await bookService.FindAsync(searchCondition, sortProperty);

            FoundBookDTO[] mappedAuthor = foundBook.Select(MapToFoundBookDTO).ToArray();
            var            totalCount   = await bookService.CountAsync(searchCondition);

            return(new PagedResponse <FoundBookDTO>
            {
                Items = mappedAuthor,
                TotalCount = totalCount
            });
        }
コード例 #2
0
        public async Task <IReadOnlyCollection <Book> > FindAsync(BookSearchCondition searchCondition, string sortProperty)
        {
            IQueryable <Book> query = BuildFindQuery(searchCondition);

            query = searchCondition.ListSortDirection == ListSortDirection.Ascending
                ? query.OrderBy(sortProperty)
                : query.OrderByDescending(sortProperty);

            return(await query.Page(searchCondition.Page, searchCondition.PageSize).ToListAsync());
        }
コード例 #3
0
        private IQueryable <Book> BuildFindQuery(BookSearchCondition searchCondition)
        {
            IQueryable <Book> query = dbContext.Books;

            if (searchCondition.Title.Any())
            {
                foreach (var title in searchCondition.Title)
                {
                    var upperTitle = title.ToUpper().Trim();
                    query = query.Where(x =>
                                        x.Title != null && x.Title.ToUpper().Contains(upperTitle));
                }
            }

            if (searchCondition.AuthorName.Any())
            {
                foreach (var authorName in searchCondition.AuthorName)
                {
                    var upperAuthorName = authorName.ToUpper().Trim();
                    query = query.Where(x =>
                                        x.Author.Name != null && x.Author.Name.ToUpper().Contains(upperAuthorName));
                }
            }

            if (searchCondition.AuthorPatronymic.Any())
            {
                foreach (var authorPatronymic in searchCondition.AuthorPatronymic)
                {
                    var upperAuthorPatronymic = authorPatronymic.ToUpper().Trim();
                    query = query.Where(x =>
                                        x.Author.Patronymic != null && x.Author.Patronymic.ToUpper().Contains(upperAuthorPatronymic));
                }
            }

            if (searchCondition.AuthorSurname.Any())
            {
                foreach (var authorSurname in searchCondition.AuthorSurname)
                {
                    var upperAuthorSurname = authorSurname.ToUpper().Trim();
                    query = query.Where(x =>
                                        x.Author.Surname != null && x.Author.Surname.ToUpper().Contains(upperAuthorSurname));
                }
            }

            if (searchCondition.Year.Any())
            {
                foreach (var year in searchCondition.Year)
                {
                    query = query.Where(x => x.Year == year);
                }
            }

            if (searchCondition.AmountPage.Any())
            {
                foreach (var amountPage in searchCondition.AmountPage)
                {
                    query = query.Where(x => x.AmountPage == amountPage);
                }
            }

            if (searchCondition.Genre.Any())
            {
                foreach (var genre in searchCondition.Genre)
                {
                    var upperGenre = genre.ToUpper().Trim();
                    query = query.Where(x =>
                                        x.Genre.Name != null && x.Genre.Name.ToUpper().Contains(upperGenre));
                }
            }

            if (searchCondition.Publisher.Any())
            {
                foreach (var publisher in searchCondition.Publisher)
                {
                    var upperPublisher = publisher.ToUpper().Trim();
                    query = query.Where(x =>
                                        x.Publisher.Name != null && x.Publisher.Name.ToUpper().Contains(upperPublisher));
                }
            }

            return(query);
        }
コード例 #4
0
        public async Task <long> CountAsync(BookSearchCondition searchCondition)
        {
            IQueryable <Book> query = BuildFindQuery(searchCondition);

            return(await query.LongCountAsync());
        }
コード例 #5
0
 public async Task <IActionResult> SearchBooks([FromBody] BookSearchCondition searchCondition, CancellationToken cancellationToken = default)
 {
     return(await ExecuteQueryAsync(new SearchBookQuery(searchCondition), cancellationToken : cancellationToken));
 }