public void AllChecked()
 {
     using (var db = new BooksDbContext()) {
         db.Books.ToList().ForEach(x => x.IsChecked = true);
         db.SaveChanges();
     }
 }
        public Book FindBook(int id)
        {
            using (var db = new BooksDbContext()) {
                var book = db.Books.SingleOrDefault(x => x.Id == id);
                if (book == null)
                {
                    return(null);
                }
                var author = db.Authors.SingleOrDefault(a => a.Id == book.Author.Id);
                if (author == null)
                {
                    return(null);
                }
                var publisher = db.Publishers.SingleOrDefault(p => p.Id == book.Publisher.Id);
                if (publisher == null)
                {
                    return(null);
                }

                return(new Book {
                    Id = book.Id,
                    Title = book.Title,
                    Author = author,
                    PublishedYear = book.PublishedYear,
                    Publisher = publisher,
                    IsChecked = book.IsChecked,
                });
            }
        }
 public void AllUnChecked()
 {
     using (var db = new BooksDbContext()) {
         db.Publishers.ToList().ForEach(x => x.IsChecked = false);
         db.SaveChanges();
     }
 }
 public void UpdateIsCheckedPublisher(Publisher p, bool isChecked)
 {
     using (var db = new BooksDbContext()) {
         var publisher = db.Publishers.Single(x => x.Id == p.Id);
         publisher.IsChecked = isChecked;
         db.SaveChanges();
     }
 }
 public void UpdateIsCheckedBook(Book b, bool isChecked)
 {
     using (var db = new BooksDbContext()) {
         var book = db.Books.Single(x => x.Id == b.Id);
         book.IsChecked = isChecked;
         db.SaveChanges();
     }
 }
 public void UpdateIsCheckedAuthor(Author a, bool isChecked)
 {
     using (var db = new BooksDbContext()) {
         var author = db.Authors.Single(x => x.Id == a.Id);
         author.IsChecked = isChecked;
         db.SaveChanges();
     }
 }
 public void UpdatePublisher(Publisher p)
 {
     using (var db = new BooksDbContext()) {
         var publisher = db.Publishers.Single(x => x.Id == p.Id);
         publisher.Name      = p.Name;
         publisher.Address   = p.Address;
         publisher.IsChecked = p.IsChecked;
         db.SaveChanges();
     }
 }
 public void DeleteBooks()
 {
     using (var db = new BooksDbContext()) {
         var books = db.Books.Where(x => x.IsChecked == true);
         if (books != null)
         {
             db.Books.RemoveRange(books);
             db.SaveChanges();
         }
     }
 }
 public void DeletePublisher(int id)
 {
     using (var db = new BooksDbContext()) {
         var publisher = db.Publishers.SingleOrDefault(x => x.Id == id);
         if (publisher != null)
         {
             db.Publishers.Remove(publisher);
             db.SaveChanges();
         }
     }
 }
 public void DeleteBook(int id)
 {
     using (var db = new BooksDbContext()) {
         var book = db.Books.SingleOrDefault(x => x.Id == id);
         if (book != null)
         {
             db.Books.Remove(book);
             db.SaveChanges();
         }
     }
 }
 public void DeleteAuthor(int id)
 {
     using (var db = new BooksDbContext()) {
         var author = db.Authors.SingleOrDefault(x => x.Id == id);
         if (author != null)
         {
             db.Authors.Remove(author);
             db.SaveChanges();
         }
     }
 }
 public void DeletePublishers()
 {
     using (var db = new BooksDbContext()) {
         var publishers = db.Publishers.Where(x => x.IsChecked == true);
         if (publishers != null)
         {
             db.Publishers.RemoveRange(publishers);
             db.SaveChanges();
         }
     }
 }
 public void UpdateAuthor(Author a)
 {
     using (var db = new BooksDbContext()) {
         var author = db.Authors.Single(x => x.Id == a.Id);
         author.Name      = a.Name;
         author.Birthday  = a.Birthday;
         author.Gender    = a.Gender;
         author.IsChecked = a.IsChecked;
         db.SaveChanges();
     }
 }
 public void InsertPublisher(Publisher p)
 {
     using (var db = new BooksDbContext()) {
         var publisher = new Publisher {
             Name      = p.Name,
             Address   = p.Address,
             IsChecked = p.IsChecked
         };
         db.Publishers.Add(publisher);
         db.SaveChanges();
     }
 }
 public void InsertAuthor(Author a)
 {
     using (var db = new BooksDbContext()) {
         var author = new Author {
             Name      = a.Name,
             Birthday  = a.Birthday,
             Gender    = a.Gender,
             IsChecked = a.IsChecked
         };
         db.Authors.Add(author);
         db.SaveChanges();
     }
 }
 public IEnumerable <Publisher> GetPublishers()
 {
     using (var db = new BooksDbContext()) {
         return(db.Publishers
                .OrderBy(x => x.Id).ToList()
                .Select(x => new Publisher {
             Id = x.Id,
             Name = x.Name,
             Address = x.Address,
             IsChecked = x.IsChecked,
             Books = x.Books
         }).ToList());
     }
 }
 public void UpdateBook(Book b)
 {
     using (var db = new BooksDbContext()) {
         var book      = db.Books.Single(x => x.Id == b.Id);
         var author    = db.Authors.Single(a => a.Id == b.Author.Id);
         var publisher = db.Publishers.Single(p => p.Id == b.Publisher.Id);
         book.Title         = b.Title;
         book.Author        = author;
         book.PublishedYear = b.PublishedYear;
         book.Publisher     = publisher;
         book.IsChecked     = b.IsChecked;
         db.SaveChanges();
     }
 }
 public IEnumerable <Author> GetAuthors()
 {
     using (var db = new BooksDbContext()) {
         return(db.Authors
                .OrderBy(x => x.Id).ToList()
                .Select(x => new Author {
             Id = x.Id,
             Name = x.Name,
             Birthday = x.Birthday,
             Gender = x.Gender,
             IsChecked = x.IsChecked,
             Books = x.Books
         }).ToList());
     }
 }
 public IEnumerable <Book> GetBooks()
 {
     using (var db = new BooksDbContext()) {
         return(db.Books
                .OrderByDescending(x => x.PublishedYear).ToList()
                .Select(x => new Book {
             Id = x.Id,
             Title = x.Title,
             Author = x.Author,
             PublishedYear = x.PublishedYear,
             Publisher = x.Publisher,
             IsChecked = x.IsChecked
         }).ToList());
     }
 }
 public void InsertBook(Book b)
 {
     using (var db = new BooksDbContext()) {
         var author    = db.Authors.Single(a => a.Id == b.Author.Id);
         var publisher = db.Publishers.Single(p => p.Id == b.Publisher.Id);
         var book      = new Book {
             Title         = b.Title,
             Author        = author,
             PublishedYear = b.PublishedYear,
             Publisher     = publisher,
             IsChecked     = b.IsChecked
         };
         db.Books.Add(book);
         db.SaveChanges();
     }
 }
 public bool?GetThreeState()
 {
     using (var db = new BooksDbContext()) {
         if (db.Publishers.All(x => x.IsChecked == true))
         {
             return(true);
         }
         else if (db.Publishers.All(x => x.IsChecked == false))
         {
             return(false);
         }
         else
         {
             return(null);
         }
     }
 }
        public Publisher FindPublisher(int id)
        {
            using (var db = new BooksDbContext()) {
                var publisher = db.Publishers.SingleOrDefault(x => x.Id == id);
                if (publisher == null)
                {
                    return(null);
                }

                return(new Publisher {
                    Id = publisher.Id,
                    Name = publisher.Name,
                    Address = publisher.Address,
                    IsChecked = publisher.IsChecked
                });
            }
        }
 public IEnumerable <string> GetPublisherNamesInBooks()
 {
     using (var db = new BooksDbContext()) {
         var ids = db.Publishers
                   .Where(x => x.IsChecked == true)
                   .Select(x => x.Id).ToList();
         return(db.Books
                .OrderByDescending(x => x.Id).ToList()
                .Where(x => ids.Contains(x.Publisher.Id))
                .Join(db.Publishers,
                      book => book.Publisher.Id,
                      publisher => publisher.Id,
                      (book, publisher) => publisher.Name)
                .Distinct()
                .ToList());
     }
 }
        public Author FindAuthor(int id)
        {
            using (var db = new BooksDbContext()) {
                var author = db.Authors.SingleOrDefault(x => x.Id == id);
                if (author == null)
                {
                    return(null);
                }

                return(new Author {
                    Id = author.Id,
                    Name = author.Name,
                    Birthday = author.Birthday,
                    Gender = author.Gender,
                    IsChecked = author.IsChecked,
                    Books = author.Books
                });
            }
        }
        public IEnumerable <Book> SearchBooks(string searchString)
        {
            var searchTerms = searchString.ToLower().Split(null);

            using (var db = new BooksDbContext()) {
                if (searchTerms.Length == 0)
                {
                    return(null);
                }
                return(db.Books
                       .Where(book => searchTerms.Any(s => book.Title.Contains(s))).ToList()
                       .Select(x => new Book {
                    Id = x.Id,
                    Title = x.Title,
                    Author = x.Author,
                    PublishedYear = x.PublishedYear,
                    Publisher = x.Publisher,
                    IsChecked = x.IsChecked
                }).ToList());
            }
        }
 public bool HasPublisherInBooks(int id)
 {
     using (var db = new BooksDbContext()) {
         return(db.Books.Any(x => x.Publisher.Id == id));
     }
 }