public async Task <GetAllAuthorViewModel> GetAll() { var authorVMItemList = new List <AuthorGetAllAuthorViewModelItem>(); var result = new GetAllAuthorViewModel(); IEnumerable <AuthorInBook> authorsBooksList = await _authorInBookRepository.GetAllAuthors(); ILookup <Author, Book> lookupList = authorsBooksList.ToLookup(p => p.Author, p => p.Book); foreach (IGrouping <Author, Book> item in lookupList) { var authorVMItem = Mapper.Map <Author, AuthorGetAllAuthorViewModelItem>(item.Key); authorVMItem.Books = (item.ToList().FirstOrDefault() == null) ? new List <BookGetAllAuthorViewModelItem>() : Mapper.Map <List <Book>, List <BookGetAllAuthorViewModelItem> >(item.ToList()); authorVMItemList.Add(authorVMItem); } result.Authors = authorVMItemList; return(result); }
public async Task <IActionResult> GetAll() { GetAllAuthorViewModel getAllAuthorViewModel = await _service.GetAll(); return(Ok(getAllAuthorViewModel)); }