//Add an author. private void btnOK_Click(object sender, RoutedEventArgs e) { if (!_proxy.CheckAuthor(txtName.Text, txtSurname.Text)) { if (id == null) { int?authId = _proxy.AddAuthor(txtName.Text, txtSurname.Text); if (authId == null) { MessageBox.Show("Something went wrong. This author was not added.", "Wrong", MessageBoxButton.OK, MessageBoxImage.Error); return; } FormData.Author = $"{txtSurname.Text}, {txtName.Text}"; FormData.AuthorsID = Convert.ToInt32(authId); } else { _proxy.EditData(Convert.ToInt32(id), txtName.Text, EngServRef.ServerData.Author, EngServRef.PropertyData.Name); _proxy.EditData(Convert.ToInt32(id), txtSurname.Text, EngServRef.ServerData.Author, EngServRef.PropertyData.Surname); } Close(); } else { MessageBox.Show("This person is already taken!", "Wrong.", MessageBoxButton.OK, MessageBoxImage.Error); } }
//Add a new book. private void btnOK_Click(object sender, RoutedEventArgs e) { int?year = null; if (txtYear.Text != "") { year = Convert.ToInt32(txtYear.Text); } int edit = 0; if (txtPath.Text == "") { MessageBox.Show("Choose file!", "No file chosen", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Task.Run(new Action(() => { Dispatcher.Invoke(new Action(() => { stMain.Visibility = Visibility.Collapsed; if (name == null) { int?id = _proxy.AddBook(txtName.Text, txtDesc.Text == "" ? null : txtDesc.Text, txtPath.Text, "WolfB.png", chCopy.IsChecked == true ? false : true, mark, user, year, DateTime.Now); if (id == null) { MessageBox.Show("Something went wrong.", "Operation denied", MessageBoxButton.OK, MessageBoxImage.Stop); stMain.Visibility = Visibility.Visible; return; } edit = Convert.ToInt32(id); if (lPath.Content.ToString() != "...") { if (!_proxy.Upload(File.ReadAllBytes(lPath.Content.ToString()), $"{edit}{Path.GetExtension(lPath.Content.ToString())}", EngServRef.FilesType.BooksImages)) { MessageBox.Show("This file is too large!\nPlease choose another file.", "Unable to upload", MessageBoxButton.OK, MessageBoxImage.Stop); _proxy.RemoveItem(edit, EngServRef.ServerData.Book); stMain.Visibility = Visibility.Visible; return; } _proxy.EditData(edit, $"{edit}{Path.GetExtension(lPath.Content.ToString())}", EngServRef.ServerData.Book, EngServRef.PropertyData.Imgpath); } if (chCopy.IsChecked == true) { if (!_proxy.Upload(File.ReadAllBytes(txtPath.Text), $"{edit}{Path.GetExtension(txtPath.Text)}", EngServRef.FilesType.Books)) { MessageBox.Show("This file is too large!\nPlease choose another file.", "Unable to upload", MessageBoxButton.OK, MessageBoxImage.Stop); _proxy.RemoveItem(edit, EngServRef.ServerData.Book); stMain.Visibility = Visibility.Visible; return; } _proxy.EditData(edit, $"{edit}{Path.GetExtension(txtPath.Text)}", EngServRef.ServerData.Book, EngServRef.PropertyData.Path); } } else { edit = bookId; if (txtPath.Text != path && chCopy.IsChecked == true) { if (!_proxy.Upload(File.ReadAllBytes(txtPath.Text), $"{edit}{Path.GetExtension(txtPath.Text)}", EngServRef.FilesType.Books)) { MessageBox.Show("This file is too large!\nPlease choose another file.", "Unable to upload", MessageBoxButton.OK, MessageBoxImage.Stop); stMain.Visibility = Visibility.Visible; return; } _proxy.EditData(edit, $"{edit}{Path.GetExtension(txtPath.Text)}", EngServRef.ServerData.Book, EngServRef.PropertyData.Path); } else if (txtPath.Text == path && txtPath.Text.Contains(":") && chCopy.IsChecked == true) { if (!File.Exists(txtPath.Text)) { MessageBox.Show("This file does not exist!", "Wrong", MessageBoxButton.OK, MessageBoxImage.Stop); stMain.Visibility = Visibility.Visible; return; } if (!_proxy.Upload(File.ReadAllBytes(txtPath.Text), $"{edit}{Path.GetExtension(txtPath.Text)}", EngServRef.FilesType.Books)) { MessageBox.Show("This file is too large!\nPlease choose another file.", "Unable to upload", MessageBoxButton.OK, MessageBoxImage.Stop); stMain.Visibility = Visibility.Visible; return; } _proxy.EditData(edit, $"{edit}{Path.GetExtension(txtPath.Text)}", EngServRef.ServerData.Book, EngServRef.PropertyData.Path); } else if (chCopy.IsChecked == false) { _proxy.EditData(edit, txtPath.Text, EngServRef.ServerData.Book, EngServRef.PropertyData.Path); } if (lPath.Content.ToString() != "...") { FormData.EditBooks.Add(edit); string file = $"{edit}{Path.GetExtension(lPath.Content.ToString())}"; if (!_proxy.Upload(File.ReadAllBytes(lPath.Content.ToString()), file, EngServRef.FilesType.BooksImages)) { MessageBox.Show("This file is too large!\nPlease choose another file.", "Unable to upload", MessageBoxButton.OK, MessageBoxImage.Stop); stMain.Visibility = Visibility.Visible; return; } _proxy.EditData(edit, file, EngServRef.ServerData.Book, EngServRef.PropertyData.Imgpath); } _proxy.EditData(edit, chCopy.IsChecked != true ? "True" : null, EngServRef.ServerData.Book, EngServRef.PropertyData.IsAbsolute); _proxy.EditData(edit, txtYear.Text == "" ? null : txtYear.Text, EngServRef.ServerData.Book, EngServRef.PropertyData.Year); _proxy.RemoveFullItemData(edit, EngServRef.ServerData.Author); _proxy.RemoveFullItemData(edit, EngServRef.ServerData.BookCategory); if (txtName.Text != name) { _proxy.EditData(edit, txtName.Text, EngServRef.ServerData.Book, EngServRef.PropertyData.Name); } if (txtDesc.Text != desc) { _proxy.EditData(edit, txtDesc.Text == "" ? null : txtDesc.Text, EngServRef.ServerData.Book, EngServRef.PropertyData.Description); } } foreach (CheckBox item in lstCategory.Items) { if (item.IsChecked == true) { _proxy.AddItemDataAsync(edit, Convert.ToInt32(item.Tag), EngServRef.ServerData.BookCategory); } } foreach (CheckBox item in lstAuthors.Items) { if (item.IsChecked == true) { _proxy.AddBookAuthorAsync(edit, Convert.ToInt32(item.Tag)); } } Close(); })); })); }