Esempio n. 1
0
        /// <summary>
        /// Takes a bookUpdater and uses it to update the book record in the database.
        /// </summary>
        /// <param name="updater"></param>
        public void UpdateBook(BookUpdater updater)
        {
            using (LibraryDBEntities context = new LibraryDBEntities())
            {
                Book book = (from b in context.Books
                             where b.BookID == updater.Book.BookID
                             select b).FirstOrDefault();
                //Check the properties on the updater to see what fields need to be altered.
                if (updater.ChangeNumberOfCopies)
                {
                    book.NumberOfCopies = updater.Book.NumberOfCopies;
                }

                context.SaveChanges();
            }
            Sort();
        }
        /// <summary>
        /// Finds the book in the database with the same BookID and matches the fields to the BookBLL passed in.
        /// </summary>
        /// <param name="updater"></param>
        public void UpdateBookFields(BookUpdater updater)
        {
            //First check if a new author needs to be created
            if (updater.AuthorBuilder != null)
            {
                AddAuthorToDatabase(updater);
            }
            using (LibraryDBEntities context = new LibraryDBEntities())
            {
                Book book = (from b in context.Books
                             where b.BookID == updater.Book.BookID
                             select b).FirstOrDefault();
                //Update the fields of the book record to match any new data
                if (updater.ChangeAuthorID)
                {
                    if (updater.AuthorBuilder != null && updater.AuthorBuilder.IsNewPerson)
                    {
                        updater.AuthorID = updater.Book.AuthorID;
                    }
                    book.AuthorID = updater.AuthorID;
                }

                if (updater.ChangeTitle)
                {
                    book.Title = updater.Title;
                }
                if (updater.ChangeISBN)
                {
                    book.ISBN = updater.ISBN;
                }
                if (updater.ChangeLanguage)
                {
                    book.Language = updater.Language;
                }
                if (updater.ChangeNumberOfCopies)
                {
                    book.NumberOfCopies = updater.NumberOfCopies;
                }
                if (updater.ChangeNumPages)
                {
                    book.NumPages = updater.NumPages;
                }
                if (updater.ChangePublisher)
                {
                    book.Publisher = updater.Publisher;
                }
                if (updater.ChangeDescription)
                {
                    book.Description = updater.Description;
                }
                if (updater.ChangeSubject)
                {
                    book.Subject = updater.Subject;
                }
                if (updater.ChangeYearPublished)
                {
                    book.YearPublished = updater.YearPublished;
                }

                context.SaveChanges();
            }
            updater.FinalizeChanges();
        }