public List <BookViewModel> GetBooks() { var a = _bookRepository.GetAll().ToList(); var res = a.Select(b => new BookViewModel { Id = b.Id, Name = b.Name, Date = b.Date, Authors = (from at in _authorRepository.GetAll().ToList() from ba in _bookAuthorRepository.GetAll().ToList() where ba.BookId == b.Id where ba.AuthorId == at.Id select new AuthorViewModel { Id = at.Id, FullName = at.Name + " " + at.Surname }).ToList(), PublishingHouses = (from ph in _publishingHouseRepository.GetAll().ToList() from bph in _bookPublishingHouseRepository.GetAll().ToList() where bph.BookId == b.Id where bph.PublishingHouseId == ph.Id select new PublishingHouseViewModel { Id = ph.Id, Name = ph.Name }).ToList() }).ToList(); return(res); }
public GetBookViewModel GetAll() { var bookViews = new List <GetBookViewItem>(); var bookPublishers = _bookPublisherRepository.GetAll().GroupBy(x => x.Book); var bookAuthors = _bookAuthorRepository.GetAll().GroupBy(x => x.Book); foreach (var bookPublisher in bookPublishers) { GetBookViewItem bookViewModel = Mapper.Map <GetBookViewItem>(bookPublisher.Key); var publishers = bookPublisher.Select(a => a.Publisher); var publishersViewModel = Mapper.Map <List <GetPublisherViewItem> >(publishers); bookViewModel.Publishers.AddRange(publishersViewModel); bookViews.Add(bookViewModel); } foreach (var bookAuthor in bookAuthors) { var bookViewModel = bookViews.Where(x => x.Id == bookAuthor.Key.Id).SingleOrDefault(); var authors = bookAuthor.Select(a => a.Author); var authorsViewModel = Mapper.Map <List <GetAuthorViewItem> >(authors); bookViewModel.Authors.AddRange(authorsViewModel); } var result = new GetBookViewModel(); result.Books = bookViews; return(result); }
public void Delete(int id) { _repository.Delete(id); //get related books etc. var relatedObjects = _bookAuthorRepository.GetAll().Where(x => x.AuthorId == id); //delete related books etc. foreach (var r in relatedObjects) { _bookAuthorRepository.Delete(r); } }
public GetBookViewModel GetAll() { var publicationHouseBookList = _publicationHouseBookRepository.GetAll(); var bookAuthorList = _bookAuthorRepository.GetAll(); var groupedList = publicationHouseBookList.Join(bookAuthorList, x => x.BookId, y => y.BookId, (x, y) => new BookDTO { Book = x.Book, Author = y.Author, PublicationHouse = x.PublicationHouse }).GroupBy(x => x.Book); IEnumerable <GetBookViewItem> bookItems = Mapper.Map <IEnumerable <IGrouping <Book, BookDTO> >, IEnumerable <GetBookViewItem> >(groupedList); return(Mapper.Map <IEnumerable <GetBookViewItem>, GetBookViewModel>(bookItems)); }