/// <summary> /// Команда редактирования книги /// </summary> private void OnEditBook() { try { if (SelectedBook != null) { var book = SelectedBook; if (SelectedBook.BindingType == BooksBindingEnum.Мягкий) { book.IsSoft = true; } var model = new ModifyWindowsView(book); if (model.ShowDialog() == true) { var index = OriginalBooks.ToList().FindIndex(l => l.BookId == model.Books.BookId); OriginalBooks[index] = new Models.Books() { BookId = model.Books.BookId, BookName = model.Books.BookName, Author = model.Books.Author, PageCount = model.Books.PageCount, PublishingYear = model.Books.PublishingYear, EditionNumber = model.Books.EditionNumber, BookPrintNumber = model.Books.BookPrintNumber, BindingType = model.Books.IsSoft ? BooksBindingEnum.Мягкий : BooksBindingEnum.Твердый, PartyNumber = model.Books.PartyNumber, BooksCountInParty = model.Books.BooksCountInParty, PrintingFactoryName = model.Books.PrintingFactoryName, }; if (File.Exists(_path + "\\Books.bin")) { File.Delete(_path + "\\Books.bin"); } var result = _fileService.OnWriteFile(OriginalBooks.ToList()); if (result != true) { throw new Exception(); } } } else { MessageBox.Show("Пожалуйста, выберите книгу для редактирования!", "Уведомление!", MessageBoxButton.OK, MessageBoxImage.Information); } } catch { MessageBox.Show("Ошибка при редактировании записи!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error); } finally { RefreshCommand.Execute(null); } }
/// <summary> /// Команда удаления книги /// </summary> private void OnDeleteBook() { try { if (SelectedBook != null) { if (MessageBox.Show("Удалить выбранную запись?", "Подтвердите удаление", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { var book = OriginalBooks.Single(l => l.BookId == SelectedBook.BookId); OriginalBooks.Remove(book); if (File.Exists(_path + "\\Books.bin")) { File.Delete(_path + "\\Books.bin"); } var result = _fileService.OnWriteFile(OriginalBooks.ToList()); if (result != true) { throw new Exception(); } } } else { MessageBox.Show("Пожалуйста, выберите книгу для удаления!", "Уведомление!", MessageBoxButton.OK, MessageBoxImage.Information); } } catch { MessageBox.Show("Ошибка при удалении записи!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error); } finally { RefreshCommand.Execute(null); } }
/// <summary> /// Команда добавления новой книги /// </summary> private void OnAddBook() { try { var model = new ModifyWindowsView(new Models.Books()); if (model.ShowDialog() != true) { return; } var lastBookId = OriginalBooks.Select(l => l.BookId).Last(); var book = model.Books; book.BindingType = book.IsSoft ? BooksBindingEnum.Мягкий : BooksBindingEnum.Твердый; book.BookId = lastBookId + 1; OriginalBooks.Add(book); if (File.Exists(_path + "\\Books.bin")) { File.Delete(_path + "\\Books.bin"); } var result = _fileService.OnWriteFile(OriginalBooks.ToList()); if (result != true) { throw new Exception(); } } catch { MessageBox.Show("Ошибка при добавлении записи!", "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error); } finally { RefreshCommand.Execute(null); } }