Example #1
0
        public async Task <GetAllBookViewModel> GetAll()
        {
            var bookVMItemList = new List <BookGetAllBookViewModelItem>();
            var result         = new GetAllBookViewModel();

            IEnumerable <AuthorInBook> booksAuthorsList = await _authorInBookRepository.GetAllBooks();

            IEnumerable <PublicHouseInBook> booksPublicHousesList = await _publicHouseInBookRepository.GetAllBooks();

            ILookup <Book, Author>      lookupAuthorsList      = booksAuthorsList.ToLookup(p => p.Book, p => p.Author);
            ILookup <Book, PublicHouse> lookupPublicHousesList = booksPublicHousesList.ToLookup(p => p.Book, p => p.PublicHouse);

            var authorsAndPublicHouses = lookupAuthorsList.Zip(lookupPublicHousesList, (a, ph) => new { PropertyAuthor = a, PropertyPublicHouse = ph });

            foreach (var item in authorsAndPublicHouses)
            {
                var bookVMItem = Mapper.Map <Book, BookGetAllBookViewModelItem>(item.PropertyAuthor.Key);

                bookVMItem.Authors = (item.PropertyAuthor.ToList().FirstOrDefault() == null) ?
                                     new List <AuthorGetAllBookViewModelItem>() :
                                     Mapper.Map <List <Author>, List <AuthorGetAllBookViewModelItem> >(item.PropertyAuthor.ToList());

                bookVMItem.PublicHouses = (item.PropertyPublicHouse.ToList().FirstOrDefault() == null) ?
                                          new List <PublicHouseGetAllBookViewModelItem>() :
                                          Mapper.Map <List <PublicHouse>, List <PublicHouseGetAllBookViewModelItem> >(item.PropertyPublicHouse.ToList());

                bookVMItemList.Add(bookVMItem);
            }

            result.Books = bookVMItemList;
            return(result);
        }
        public async Task <IActionResult> GetAll()
        {
            GetAllBookViewModel getAllBookViewModel = await _service.GetAll();

            return(Ok(getAllBookViewModel));
        }