public async Task <ServiceResponseWithPagination <List <BookDTO_ToReturn> > > SearchPagination(BookDTO_Filter filter) { var book = _dbContext.Books.Include(x => x.CategoryBooks).AsQueryable(); if (!string.IsNullOrWhiteSpace(filter.Name)) { book = book.Where(x => x.Name.Contains(filter.Name)); } book = book.Where(x => x.CategoryBooks.CodeType == filter.CategoryCode); book = book.Where(x => x.BorrowPrice >= filter.MinPrice); // 2. Order => Order by if (!string.IsNullOrWhiteSpace(filter.OrderingField)) { try { book = book.OrderBy($"{filter.OrderingField} {(filter.AscendingOrder ? "ascending" : "descending")}"); } catch { return(ResponseResultWithPagination.Failure <List <BookDTO_ToReturn> >($"Could not order by field: {filter.OrderingField}")); } } var paginationResult = await _httpContext.HttpContext.InsertPaginationParametersInResponse(book, filter.RecordsPerPage, filter.Page); // var custom = await cus.Paginate(filter).ToListAsync(); var result = _mapper.Map <List <BookDTO_ToReturn> >(await book.Paginate(filter).ToListAsync()); return(ResponseResultWithPagination.Success(result, paginationResult)); }
public async Task <IActionResult> SearchBooksPaginate(BookDTO_Filter filter) { var result = await _bookService.SearchPaginate(filter); return(Ok(result)); }
public async Task <IActionResult> SearchPagination([FromQuery] BookDTO_Filter filter) { return(Ok(await _bookService.SearchPagination(filter))); }
public async Task <ServiceResponseWithPagination <List <BookDTO_ToReturn> > > SearchPaginate(BookDTO_Filter filter) { // 1. Filter => Search var queryable = _db.Books.AsQueryable(); if (!string.IsNullOrWhiteSpace(filter.Name)) { queryable = queryable.Where(x => (x.Name).Contains(filter.Name)); } queryable = queryable.Where(x => x.Price >= filter.MinPrice); queryable = queryable.Where(x => x.Price <= filter.MaxPrice); // 2. Order => Order by if (!string.IsNullOrWhiteSpace(filter.OrderingField)) { try { queryable = queryable.OrderBy($"{filter.OrderingField} {(filter.AscendingOrder ? "ascending" : "descending")}"); } catch { return(ResponseResultWithPagination.Failure <List <BookDTO_ToReturn> >($"Could not order by field: {filter.OrderingField}")); } } // 3. Add Paginate => Page,total,Perpage var paginationResult = await _httpContext.HttpContext.InsertPaginationParametersInResponse(queryable, filter.RecordsPerPage, filter.Page); // 4. Execute Query var books = await queryable.Paginate(filter).ToListAsync(); // 5. Return result var result = _mapper.Map <List <BookDTO_ToReturn> >(books); return(ResponseResultWithPagination.Success(result, paginationResult)); }