Ejemplo n.º 1
0
        public IEnumerable <BookModel> GetBooks()
        {
            var books = Books.Get()?.ToList();

            if (books == null || !books.Any())
            {
                return(Enumerable.Empty <BookModel>().ToList());
            }

            var pubHouseIds = books.Select(t => t.PubHouseId);
            var pubHouses   = PubHouses.Get().Where(t => pubHouseIds.Contains(t.PubHouseId));

            var bookIds = books.Select(t => t.BookId);

            var authors = BookAuthors.Get().Where(t => bookIds.Contains(t.BookId)).Join(Authors.Get(),
                                                                                        ba => ba.AuthorId,
                                                                                        a => a.AuthorId,
                                                                                        (ba, a) =>
                                                                                        new
            {
                ba.BookId,
                author = a
            });

            var list = new List <BookModel>();

            books.ForEach(t =>
            {
                var thisBookAuthors = authors.Where(a => a.BookId == t.BookId).Select(q => q.author).ToList();
                var pubHouse        = pubHouses.SingleOrDefault(p => p.PubHouseId == t.PubHouseId);
                list.Add(new BookModel(t, thisBookAuthors, pubHouse));
            });
            return(list);
        }
Ejemplo n.º 2
0
 public void DeleteAuthor(long id)
 {
     if (BookAuthors.Get().Any(t => t.AuthorId == id))
     {
         throw new InvalidOperationException();
     }
     Authors.Delete(id);
 }
Ejemplo n.º 3
0
        public BookModel GetBook(long id)
        {
            var book = Books.GetById(id);

            var pubHouse = book.PubHouseId.HasValue ? PubHouses.GetById(book.PubHouseId.Value) : null;

            var authors = Authors.Get()
                          .Join(BookAuthors.Get().Where(q => q.BookId == book.BookId), a => a.AuthorId, ba => ba.AuthorId, (a, ba) =>
                                a).ToList();
            var bookModel = new BookModel(book, authors, pubHouse);

            return(bookModel);
        }