public bool CreateBook(List <int> authorsId, List <int> categoiesId, Book book)
        {
            //Get all the Author object from the list of authorsId passed
            var authors = _bookDbContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();

            //Get all the Category object from the list of categoriesId passed
            var categories = _bookDbContext.Categories.Where(a => categoiesId.Contains(a.Id)).ToList();

            //Looping through Author object list and populating the BookAuthor table
            foreach (var a in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = a,
                    Book   = book
                };
                _bookDbContext.Add(bookAuthor);
            }

            //Looping through Category object list and populating the BookCategory table
            foreach (var c in categories)
            {
                var bookCategory = new BookCategory()
                {
                    Category = c,
                    Book     = book
                };
                _bookDbContext.Add(bookCategory);
            }

            //Populating to Books table
            _bookDbContext.Books.Add(book);

            return(Save());
        }
        public bool CreateBook(List <int> authorsId, List <int> categoriesId, Book book)
        {
            var authors    = _bookDbContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();
            var categories = _bookDbContext.Categories.Where(c => categoriesId.Contains(c.Id)).ToList();

            foreach (var author in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = author,
                    Book   = book
                };
                _bookDbContext.Add(bookAuthor);
                // We could also create List<BookAuthor> and add all bookAuthor objects to it,
                // and then use AddRange on the DbContext to add it all at once. Either way is fine.
            }

            foreach (var category in categories)
            {
                var bookCategory = new BookCategory()
                {
                    Category = category,
                    Book     = book
                };
                _bookDbContext.Add(bookCategory);
            }

            _bookDbContext.Add(book);

            return(Save());
        }
        public bool CreateBook(List <int> authorsId, List <int> categoriesId, Book book)
        {
            var authors    = _bookDbContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();
            var categories = _bookDbContext.Categories.Where(c => categoriesId.Contains(c.Id)).ToList();

            foreach (var author in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = author,
                    Book   = book
                };
                _bookDbContext.Add(bookAuthor);
            }

            foreach (var category in categories)
            {
                var bookCategory = new BookCategory()
                {
                    Category = category,
                    Book     = book
                };
                _bookDbContext.Add(bookCategory);
            }

            _bookDbContext.Add(book);
            return(Save());
        }
Exemple #4
0
        public bool CreateBook(List <int> authorsId, List <int> categoriesId, Book book)
        {
            // Since a Book can have many authors, and many categories, we pass the author and category Ids in the arguments
            // Now we can retrieve all the authors and categories we want to associate to our new book
            var authors    = _bookContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();
            var categories = _bookContext.Categories.Where(c => categoriesId.Contains(c.Id)).ToList();

            // Then, we need to create a BookAuthor for each author, because that's how books and authors are linked together in the database
            foreach (var author in authors)
            {
                var bookAuthor = new BookAuthor
                {
                    Author = author,
                    Book   = book
                };

                // Once we have created the BookAuthor, add it to the database
                _bookContext.Add(bookAuthor);
            }

            // Then, we need to create a BookCategory for each category, because that's how books and categories are linked together in the database
            foreach (var category in categories)
            {
                var bookCategory = new BookCategory
                {
                    Category = category,
                    Book     = book
                };

                // Once we have created the BookCategory, add it to the database
                _bookContext.Add(bookCategory);
            }

            _bookContext.Add(book);
            return(Save());
        }
Exemple #5
0
 public bool CreateCountry(Country country)
 {
     _countryContext.Add(country);
     return(Save());
 }
 public bool CreateReviewer(Reviewer reviewer)
 {
     _bookDbContext.Add(reviewer);
     return(Save());
 }
 public bool CreateReviewer(Reviewer reviewer)
 {
     _reviewerContext.Add(reviewer);
     return(SaveReviewer());
 }
 public bool CreateReview(Review review)
 {
     _reviewContext.Add(review);
     return(Save());
 }
Exemple #9
0
 public bool CreateCategory(Category category)
 {
     _categoryContext.Add(category);
     return(Save());
 }
 public bool CreatedBook(Book book)
 {
     _bookContext.Add(book);
     return(Save());
 }
 public bool CreateAuthor(Author author)
 {
     _authorContext.Add(author);
     return(Save());
 }