Beispiel #1
0
 private IQueryable <Book> SearchForValue(IQueryable <Book> queryable, PaginationandFilterParameters filterParameters)
 {
     return(queryable.Where(o => (o.Title.ToLower().Contains(filterParameters.SearchText.Trim().ToLower()) ||
                                  o.ShortDescr.ToLower().Contains(filterParameters.SearchText.Trim().ToLower()) ||
                                  o.ISBN.ToLower().Contains(filterParameters.SearchText.Trim().ToLower()) ||
                                  o.LongDescr.ToLower().Contains(filterParameters.SearchText.Trim().ToLower()))));
 }
Beispiel #2
0
        public async Task GetAllTest_returnsRequestedNumberOfResultsPerPage()
        {
            //setup
            var books          = CreateBooks(30);
            var mockParameters = new PaginationandFilterParameters();

            mockParameters.PageSize   = 7;
            mockParameters.PageNumber = 1;

            PagedResponse <Book> mockData = new PagedResponse <Book>();

            mockData.Results     = books;
            mockData.PageSize    = 7;
            mockData.CurrentPage = 1;

            //  Arrange
            _mockBookService.Setup(b => b.GetBooksAsync(mockParameters)).Returns(Task.FromResult(mockData));
            var sut = new BooksController(_mockLogger.Object, _mockBookService.Object, _mockMapper.Object);

            //  Act
            var result = (OkObjectResult)await sut.GetAll(mockParameters);

            var objectValue = (PagedResponse <Book>)result.Value;

            //  Assert
            Assert.AreEqual(mockParameters.PageSize, objectValue.PageSize);
        }
Beispiel #3
0
        public async Task <IActionResult> GetAll([FromQuery] PaginationandFilterParameters paginationParameters = null)
        {
            _logger.LogInformation("Get all books");

            var books = await _bookService.GetBooksAsync(paginationParameters);

            return(Ok(books));
        }
Beispiel #4
0
        public async Task <PagedResponse <Book> > GetBooksAsync(PaginationandFilterParameters paginationParameters = null)
        {
            PagedResponse <Book> result = new PagedResponse <Book>();

            try
            {
                var queryable = _dataContext.Books.AsQueryable();
                if (!String.IsNullOrEmpty(paginationParameters.SearchText))
                {
                    queryable = SearchForValue(queryable, paginationParameters);
                }
                result = await PaginateBookRecords(queryable, paginationParameters);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to retrieve records" + ex.ToString());
            }
            return(result);
        }
Beispiel #5
0
        private async Task <PagedResponse <Book> > PaginateBookRecords(IQueryable <Book> queryable, PaginationandFilterParameters paginationParameters = null)
        {
            PagedResponse <Book> result = new PagedResponse <Book>();

            if (paginationParameters != null)
            {
                result = await Utilities.GetPaged <Book>(queryable, paginationParameters.PageNumber, paginationParameters.PageSize);
            }
            else
            {
                //if pagination parameters are null return all records on page 1
                result = await Utilities.GetPaged <Book>(queryable, 1, queryable.Count());
            }
            return(result);
        }