public async Task <ResponseBookPreviewViewModel> GetBooks(RequestGetBookViewModel requestViewModel) { RequestGetBooksModel requestModel = _mapper.Map <RequestGetBooksModel>(requestViewModel); List <Book> books = await _bookRepository.GetByFilters(requestModel); ResponseBookPreviewViewModel result = await GetMappedBooks(books, requestModel); return(result); }
public async Task <List <Book> > GetByFilters(RequestGetBooksModel requestModel) { Expression <Func <Book, bool> > expression = null; List <Book> books; if (requestModel.PrintingEditionTypes.Count == default(int)) { expression = book => book.Price >= requestModel.MinPrice && book.Price <= requestModel.MaxPrice; } if (requestModel.PrintingEditionTypes.Count != default(int)) { expression = book => book.Price >= requestModel.MinPrice && book.Price <= requestModel.MaxPrice && requestModel.PrintingEditionTypes.Contains(book.Category); } books = await _data.Books.Where(expression).Skip(requestModel.Index).Take(requestModel.Count).ToListAsync(); return(books); }
private async Task <ResponseBookPreviewViewModel> GetMappedBooks(List <Book> books, RequestGetBooksModel requestModel) { List <int> booksIds = books.Select(book => book.Id).ToList(); List <AuthorBooks> authorBooks = await _authorBooksRepository.GetByBooksIds(booksIds); List <int> authorsIds = authorBooks.Select(item => item.AuthorId).Distinct().ToList(); List <Author> authors = await _authorRepository.GetAuthorsByIds(authorsIds); ResponseBookPreviewViewModel result = await MapBookToPreviewViewModel(books, authorBooks, authors); result.TotalCount = await _bookRepository.GetTotalCount(requestModel); return(result); }