private void addButton_Click(object sender, RoutedEventArgs e) { try { //Searches for a book by ISBN, if one is found then additional copies will be added to it. //If no book is found then a new book will be created. if (iSBNTextBox.Text == "") { throw new Exception("Please enter an ISBN"); } BookSearch search = new BookSearch(books, people, iSBNTextBox.Text.Trim()); search.ISBNSearch(); if (search.FoundBooks.Count == 0) { //A new book is being added to the database //Create the bookBuilder that will make the bookBLL BookBuilder builder = new BookBuilder(books, people, iSBNTextBox.Text.Trim()); //Request author information RequestAuthorWindow dialog1 = new RequestAuthorWindow(builder); if (dialog1.ShowDialog() == true) { //Check if a new author is being created, is so ask for bio if (builder.AuthorBuilder != null) { RequestAuthorBio dialog2 = new RequestAuthorBio(builder); dialog2.ShowDialog(); } Reset(); this.NavigationService.Navigate(new NewBookKnownAuthor(builder)); } } else { BookUpdater updater = new BookUpdater(books, people, logs, search.FoundBooks[0]); Reset(); this.NavigationService.Navigate(new AddSubtractCopiesPage(updater)); } } catch (Exception ex) { errorLabel.Content = ErrorHandler.InnermostExceptionMessage(ex); } }
private void saveButton_Click(object sender, RoutedEventArgs e) { try { //Gather and trim the inputs string firstName = firstNameTextBox.Text.Trim(); string lastName = lastNameTextBox.Text.Trim(); string iSBN = iSBNTextBox.Text.Trim(); string title = titleTextBox.Text.Trim(); int numberOfCopies; if (!int.TryParse(copiesTextBox.Text, out numberOfCopies) || numberOfCopies < 0) { throw new Exception("Failed to parse the number of copies. Must enter a number, 0 or more."); } //Since numPages is a nullable int some more logic is required for formating, and parsing. int?numPages = null; if (numPagestextBox.Text != "") { if (!int.TryParse(numPagestextBox.Text, out int numPagesint)) { throw new Exception("If entering a page count please enter a valid number."); } else { //A valid number was entered, set the pages. if (numPagesint < 1) { throw new Exception("Pages should be 1 or more."); } numPages = numPagesint; } } string subject = subjectTextBox.Text.Trim(); string description = descriptionTextBox.Text.Trim(); string publisher = publisherTextBox.Text.Trim(); if (publisher.Count() > 50) { throw new Exception("Publisher name cannot be greater than 50 characters."); } string yearPublished = yearTextBox.Text.Trim(); if (yearPublished != "") { if (yearPublished.Count() != 4) { throw new Exception("If entering a year it must be 4 characters"); } } string language = languageTextBox.Text.Trim(); //Check the required fields to make sure there is something in them all if (firstName == "" || lastName == "" || title == "" || iSBN == "") { throw new Exception("Required fields cannot be left blank."); } if (titleTextBox.Text.Trim().Count() > 50) { throw new Exception("Title is too long, 50 char maximum."); } if (iSBN.Count() > 50) { throw new Exception("ISBN is too long, 50 character maximum"); } //******************* //ISBN, Author, and Copies logic is not complete yet //******************* //The values can now be sent to the BookUpdater, it will handle the logic //Required Fields updater.UpdateISBN(iSBN); updater.UpdateTitle(title); updater.UpdateNumberOfCopies(numberOfCopies); updater.UpdateAuthorID(firstName, lastName);//this might need to be turned off for testing //If a new author object is being created a bio needs to be requested if (updater.AuthorBuilder != null) { RequestAuthorBio dialog = new RequestAuthorBio(updater); dialog.ShowDialog(); } //Optional Fields updater.UpdateNumPages(numPages); updater.UpdateSubject(subject); updater.UpdateDescription(description); updater.UpdatePublisher(publisher); updater.UpdateYearPub(yearPublished); updater.UpdateLanguage(language); //Update the book record in the database books.UpdateBookFields(updater); this.NavigationService.GoBack(); } catch (Exception ex) { errorLabel.Content = ErrorHandler.InnermostExceptionMessage(ex); updater.Reset(); } }