public async Task <PagedList <Book> > GetBooks(BookParams bookParams) { var books = _context.Books.OrderBy(b => b.Title); if (!string.IsNullOrEmpty(bookParams.OrderBy)) { switch (bookParams.OrderBy) { case "datereleased": books = books.OrderByDescending(b => b.DateReleased); break; case "dateadded": books = books.OrderByDescending(b => b.DateAdded); break; case "title": books = books.OrderBy(b => b.Title); break; default: books = books.OrderBy(b => b.Title); break; } } return(await PagedList <Book> .CreateAsync(books, bookParams.PageNumber, bookParams.PageSize)); }
public async Task <PagedList <Book> > GetBooksAsync(BookParams bookParams) { var query = repository.context.Books .Include(x => x.AuthorBooks).ThenInclude(y => y.Author) .Include(x => x.BookCategories).ThenInclude(y => y.Category) .Include(x => x.Order_Receipts) .Include(x => x.Reviews) .AsQueryable(); if (bookParams.CategoryId != null) { query = query.Where(b => b.BookCategories .Any(bc => bc.CategoryId == bookParams.CategoryId)).AsQueryable(); } if (bookParams.AuthorId != null) { query = query.Where(b => b.AuthorBooks .Any(bc => bc.BookId == bookParams.AuthorId)).AsQueryable(); } if (!String.IsNullOrEmpty(bookParams.TitleSearch)) { query = query.Where(b => b.Title.ToLower().Contains(bookParams.TitleSearch.ToLower())) .AsQueryable(); } return(await PagedList <Book> .CreateAsync(query, bookParams.pageNumber, bookParams.pageSize)); }
public async Task <IActionResult> GetBooks([FromQuery] BookParams bookParams) { var bookFromRepo = await _repo.GetAsync(bookParams.BookId); if (bookFromRepo != null) { // set the book id. bookParams.BookId = bookFromRepo.Id; if (string.IsNullOrEmpty(bookParams.Topic)) { // set the book topic. bookParams.Topic = bookFromRepo.Topic; } } var books = await _repo.GetBooksWithPaginationAsync(bookParams); var booksToReturn = _mapper.Map <IEnumerable <BookForListDTO> >(books); // pass the information in the header to the client. Response.AddPagination(books.CurrentPage, books.PageSize, books.TotalCount, books.TotalPages); return(Ok(booksToReturn)); }
public async Task <PagedList <Book> > GetBooksWithPaginationAsync(BookParams bookParams) { var books = GenericDataContext.Books.OrderByDescending(book => book.PublishedDate).AsQueryable(); if (!string.IsNullOrEmpty(bookParams.Topic) && bookParams.Topic.ToLower() != "any" && bookParams.Topic.ToLower() != "null") { books = books.Where(b => b.Topic.ToLower() == bookParams.Topic.ToLower() || b.Topic.ToLower().Contains(bookParams.Topic.ToLower())); } if (bookParams.MinPrice != 1 || bookParams.MaxPrice != 999) { books = books.Where(b => b.Price >= bookParams.MinPrice && b.Price <= bookParams.MaxPrice); } if (!string.IsNullOrEmpty(bookParams.OrderBy)) { switch (bookParams.OrderBy.ToLower()) { case "publisheddate": books = books.OrderByDescending(b => b.PublishedDate); break; case "starrating": books = books.OrderByDescending(b => b.StarRating); break; } } return(await PagedList <Book> .CreateAsync(books, bookParams.PageNumber, bookParams.PageSize)); }
public async Task <ActionResult <IReadOnlyList <Book> > > GetBooks([FromQuery] BookParams bookParams) { if (bookParams != null && bookParams.Title != null) { return(Ok(await _bookRepository.SearchBooks(bookParams.Title))); } return(Ok(await _genericRepo.ListAllAsync())); }
public async Task <ActionResult <IEnumerable <Book> > > Get([FromQuery] BookParams bookParams) { var result = await service.GetBooksAsync(bookParams); Response.AddPaginationHeader(result.CurrentPage, result.PageSize, result.TotalCount, result.TotalPages); return(Ok(result)); }
public ActionResult <List <BookDTO> > GetBook([FromQuery] BookParams bookParams) { try { return(_service.GetBooks(bookParams.title, bookParams.author, bookParams.isbn).ToList()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <PagedList <Book> > GetBooksWithGenresAsync(BookParams bookParams) { IQueryable <Book> query = _context.Books .Include(b => b.Genre); if (bookParams.GenreId != 0) { query = query.Where(b => b.GenreId == bookParams.GenreId); } return(await PagedList <Book> .CreateAsync(query, bookParams.PageNumber, bookParams.PageSize)); }
public async Task <IActionResult> GetBooks([FromQuery] BookParams bookParams) { var books = await _repo.GetBooks(bookParams); var booksToReturn = _mapper.Map <IEnumerable <BookForListDto> >(books); if (books != null) { Response.AddPagination(books.CurrentPage, books.PageSize, books.TotalCount, books.TotalPages); } return(Ok(booksToReturn)); }
public async Task <ActionResult <BookFilterViewModel> > Index([FromQuery] int?pageNumber, int?genreId, int?pageSize) { var bookParams = new BookParams { PageNumber = pageNumber ?? 1, PageSize = pageSize ?? Convert.ToInt32(_config.GetValue <string>("BookPagination:PageSize")), GenreId = genreId ?? 0 }; var bookFilterViewModel = new BookFilterViewModel { Books = await _unitOfWork.Books.GetBooksWithGenresAsync(bookParams), Genres = _unitOfWork.Genres.GetAll() }; return(View(bookFilterViewModel)); }
public void SetUp() { _repo = new Mock <IBookstoreRepository>(); var mapper = new Mock <IMapper>(); var cloudinaryConfig = new Mock <ICloudinaryConfig>(); _controller = new BooksController(_repo.Object, mapper.Object, cloudinaryConfig.Object); _bookParams = new BookParams(); _book = new Book { Id = 1 }; _book.Photos = new List <Photo> { new Photo { Id = 1 } }; _repo.Setup(r => r.GetBook(_book.Id)).Returns(Task.FromResult(_book)); }
public async Task <IActionResult> GetBooks([FromQuery] BookParams bookParams) { var currentUser = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); bookParams.UserId = currentUser; if (string.IsNullOrEmpty(bookParams.MainGenre)) { bookParams.MainGenre = "ALL"; } if (string.IsNullOrEmpty(bookParams.BookName)) { bookParams.BookName = "ALL"; } var books = await _repo.GetBooks(bookParams); var booksToReturn = _mapper.Map <IEnumerable <BooksForListDto> >(books); Response.AddPagination(books.CurrentPage, books.PageSize, books.TotalCount, books.TotalPages); return(Ok(booksToReturn)); }
public async Task <PagedList <Book> > GetBooks(BookParams bookParams) { var books = _context.Books.OrderByDescending(u => u.PublishedDate).AsQueryable(); if (bookParams.Loanbook) { var bookLoans = await GetBorrowBook(bookParams.UserId); books = books.Where(u => bookLoans.Contains(u.Id)); } if (bookParams.BookName == "ALL" && bookParams.MainGenre == "ALL") { if (!string.IsNullOrEmpty(bookParams.OrderBy)) { switch (bookParams.OrderBy) { case "A-Z": books = books.OrderBy(u => u.BookName); break; default: books = books.OrderByDescending(u => u.PublishedDate); break; } } return(await PagedList <Book> .CreateAsync(books, bookParams.PageNumber, bookParams.PageSize)); } else if (bookParams.BookName == "ALL") { books = books.Where(p => p.MainGenre.ToLower() == bookParams.MainGenre.ToLower()); if (!string.IsNullOrEmpty(bookParams.OrderBy)) { switch (bookParams.OrderBy) { case "A-Z": books = books.OrderBy(u => u.BookName); break; default: books = books.OrderByDescending(u => u.PublishedDate); break; } } return(await PagedList <Book> .CreateAsync(books, bookParams.PageNumber, bookParams.PageSize)); } else if (bookParams.MainGenre == "ALL") { books = books.Where(p => p.BookName.ToLower().Contains(bookParams.BookName.ToLower())); if (!string.IsNullOrEmpty(bookParams.OrderBy)) { switch (bookParams.OrderBy) { case "A-Z": books = books.OrderBy(u => u.BookName); break; default: books = books.OrderByDescending(u => u.PublishedDate); break; } } return(await PagedList <Book> .CreateAsync(books, bookParams.PageNumber, bookParams.PageSize)); } else { books = books.Where(p => p.MainGenre.ToLower() == bookParams.MainGenre.ToLower()); books = books.Where(p => p.BookName.ToLower().Contains(bookParams.BookName.ToLower())); if (!string.IsNullOrEmpty(bookParams.OrderBy)) { switch (bookParams.OrderBy) { case "A-Z": books = books.OrderBy(u => u.BookName); break; default: books = books.OrderByDescending(u => u.PublishedDate); break; } } return(await PagedList <Book> .CreateAsync(books, bookParams.PageNumber, bookParams.PageSize)); } }
public async Task <IActionResult> GetBooks([FromQuery] BookParams param) { return(HandlePagedResult(await Mediator.Send(new List.Query { Params = param }))); }