public async Task <GetAuthorResponseViewModel> GetAuthor(long authorId) { Author author = await _authorRepository.GetAsync(authorId); if (author == null) { throw new NotFoundException("Author with provided id was not found."); } GetAuthorResponseViewModel getAuthorResponseViewModel = _mapper.Map <Author, GetAuthorResponseViewModel>(author); IEnumerable <Book> authorBooks = await _bookRepository.GetBooksByAuthorIdAsync(authorId); if (authorBooks == null || authorBooks.Count() == 0) { return(getAuthorResponseViewModel); } IEnumerable <long> bookIdList = authorBooks.Select(b => b.Id); IEnumerable <BookGenre> booksGenres = await _bookGenreRepository.GetBooksGenresByBookIdListAsync(bookIdList); IEnumerable <IGrouping <long, BookGenre> > groupedGenres = booksGenres.GroupBy(bg => bg.BookId); getAuthorResponseViewModel.Books = authorBooks.Select(b => { IEnumerable <Genre> genres = groupedGenres.Where(g => g.Key == b.Id).SelectMany(g => g.Select(bg => bg.Genre)); var book = new BookViewModelItem { Id = b.Id, Title = b.Title, PublicationYear = b.PublicationYear, Genres = _mapper.Map <IEnumerable <Genre>, IEnumerable <GenreViewModelItem> >(genres) }; return(book); }).ToList(); return(getAuthorResponseViewModel); }
public async Task <ActionResult <GetAuthorResponseViewModel> > Get(long id) { GetAuthorResponseViewModel getAuthorResponseViewModel = await _authorService.GetAuthor(id); return(Ok(getAuthorResponseViewModel)); }