public async Task <IActionResult> GetAllBooks(string genre, string author, string title)
        {
            IBookSearchParams searchParams = new BookSearchParams()
            {
                Genre = genre, Author = author, Title = title
            };
            var books = await _bookRepo.GetAllBooks(searchParams);

            return(Ok(books));
        }
 /// <summary>
 /// Helper method to apply the filter using where conditions
 /// </summary>
 /// <param name="query"></param>
 /// <param name="parameters">title & description</param>
 /// <returns>query with filters</returns>
 private IQueryable <Book> getFilteredBooks(IQueryable <Book> query, BookSearchParams parameters)
 {
     if (!String.IsNullOrEmpty(parameters.title))
     {
         query = query.Where(b => b.Title.Contains(parameters.title));
     }
     if (!String.IsNullOrEmpty(parameters.description))
     {
         query = query.Where(b => b.ShortDescr.Contains(parameters.description));
     }
     return(query);
 }
示例#3
0
        public async Task GetAllbyPaging(BookSearchParams parameters)
        {
            // Arrange
            _mockBookService.Setup(b => b.GetBooksAsync(parameters)).Returns(Task.FromResult(It.IsAny <PagingResponse <Domain.Book> >()));
            var sut = new BooksController(_mockLogger.Object, _mockBookService.Object, _mockMapper.Object);

            //Act
            var result = await sut.GetAllbyPaging(parameters);

            //Assert
            _mockBookService.Verify(s => s.GetBooksAsync(parameters), Times.Once, "Expected GetBooksAsync Overload(parameters) to have called once");
        }
示例#4
0
        public async Task <IActionResult> GetAllbyPaging(BookSearchParams parameters)
        {
            try
            {
                _logger.LogInformation("Get all books by paging");

                var books = await _bookService.GetBooksAsync(parameters);

                var booksPagingResponse = _mapper.Map <PagingResponse <Book> >(books);

                return(Ok(booksPagingResponse));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(Ok(ex.Message));
            }
        }
示例#5
0
        public IEnumerable <Book> Search([FromUri] BookSearchParams param)
        {
            using (var db = new LibraryContext())
            {
                var query = db.Books.Include(i => i.Author).Include(i => i.Genre);

                if (!String.IsNullOrEmpty(param.Title))
                {
                    //search by title
                    query = query.Where(w => w.Title == param.Title);
                }
                if (!String.IsNullOrEmpty(param.Author))
                {
                    //search by author
                    query = query.Where(w => w.Author.Name == param.Author);
                }
                if (!String.IsNullOrEmpty(param.Genre))
                {
                    //search by genre
                    query = query.Where(w => w.Genre.Name == param.Genre);
                }
                return(query.ToList());
            }
        }
        /// <summary>
        /// Get All Books with search filters
        /// </summary>
        /// <param name="parameters">Object with filters title, description, page number & page size</param>
        /// <returns>Filtered Books</returns>
        public async Task <PagingResponse <Book> > GetBooksAsync(BookSearchParams parameters)
        {
            var result = await getFilteredBooks(_dataContext.Books.AsQueryable(), parameters).GetPagedAsync(parameters.pageNumber, parameters.pageSize);

            return(result);
        }