private void DeleteThisBook_Click(object sender, RoutedEventArgs e) { var getBook = (RememberUtility.Model.Books)listviewBook.SelectedItem; if (MessageBox.Show($"Found '{getBook.bookName}'." + $" Do you wanna delete '{getBook.bookName}'", "Confirm delete", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { booksUtil.DeleteBook(getBook.bookName); lblExportBookResult.Foreground = Brushes.Green; lblExportBookResult.Content = "Deleted"; // Count second var stopwatch = new Stopwatch(); stopwatch.Restart(); // Count this object loading listviewBook.ItemsSource = booksUtil.GetListBooks(); Show_ms.Content = stopwatch.Elapsed.TotalMilliseconds; // Count again countObjectList.Foreground = Brushes.ForestGreen; countObjectList.Content = booksUtil.GetListBooks().Count; } else { lblExportBookResult.Foreground = Brushes.Red; lblExportBookResult.Content = "Canceled"; } }
private void Delete_Click(object sender, RoutedEventArgs e) { var book = new RememberUtility.Model.Books(); // txtBookName if (txtBookName.Text != "") { book.BookName = txtBookName.Text; } else { AddBookResult.Foreground = Brushes.Red; AddBookResult.Content = "Book name cannot be null. Please choose a type"; } if (txtBookName.Text == "") { AddBookResult.Foreground = Brushes.Red; AddBookResult.Content = "You must input book name"; } else // has input { var findBook = booksUtil.FindBookBy(txtBookName.Text); if (findBook != null) { if (book.BookName.ToLower() == findBook.BookName.ToLower()) { if (MessageBox.Show($"Found '{txtBookName.Text}'." + $" Do you wanna to delete '{txtBookName.Text}'?", "Confirm delete", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { booksUtil.DeleteBook(book.BookName); AddBookResult.Foreground = Brushes.Green; AddBookResult.Content = "Delete Book Successful"; txtBookName.Text = string.Empty; txtAuthor.Text = string.Empty; lstListCategory.Text = string.Empty; } else { AddBookResult.Foreground = Brushes.YellowGreen; AddBookResult.Content = $"Delete operate was canceled."; } } } // input and not found else { AddBookResult.Foreground = Brushes.Red; AddBookResult.Content = $" Cannot find '{txtBookName.Text}'. Delete failed."; } } }
public ActionResult DeleteConfirmed(string id) { var book = _booksUtil.FindBookByBookId(id); if (book == null) { return(View("Error")); } _booksUtil.DeleteBook(book.BookName); return(RedirectToAction("Index", "Books")); }
private static void Main() { Console.InputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8; LoggerUtil.HandleLogPath(); var bookUtil = new BooksUtil(); int choose; int tempChoose; HandleRandom.ChooseColorForString("--- Welcome to Web Bookmark ---", ConsoleColor.Yellow); Console.WriteLine("1. Get list books"); Console.WriteLine("2. Add book"); Console.WriteLine("3. Find book"); Console.WriteLine("4. Delete book"); Console.WriteLine("5. Upate book"); Console.WriteLine("6. Save book to Excel"); Console.WriteLine("7. Save book Model to db"); HandleRandom.ChooseColorForString("0. Exit", ConsoleColor.Red); Console.Write("Choose: "); while (int.TryParse(Console.ReadLine(), out tempChoose)) { choose = tempChoose; switch (choose) { case 1: foreach (var itemBook in bookUtil.GetListBooks()) { Console.WriteLine("Book Id: " + itemBook.BookId); HandleRandom.ChooseColorForString($"Book name: {itemBook.BookName}", ConsoleColor.Green); Console.WriteLine("Author: " + itemBook.Author); Console.WriteLine("Category: " + itemBook.Category); Console.WriteLine("-----------------------------"); } break; case 2: // add book Console.Write("Book Name: "); var bookName = Console.ReadLine(); Console.Write("Author: "); var author = Console.ReadLine(); Console.WriteLine("Types of Category"); string category; var count = 1; var listConstantValue = typeof(CategoriesBookConstant).GetAllPublicConstantValues <string>(); listConstantValue.Sort(); foreach (var propertyInfo in listConstantValue) { Console.WriteLine(count++ + ". " + propertyInfo); } Console.WriteLine("Please choose a category: "); category = Console.ReadLine(); if (category != null) { if (int.Parse(category) > 0 && int.Parse(category) <= listConstantValue.Count) { category = listConstantValue[int.Parse(category) - 1]; } } bookUtil.AddBook(new Books() { BookId = HandleRandom.RandomString(8), BookName = bookName, Author = author, Category = category }); HandleRandom.ChooseColorForString("Adding successful", ConsoleColor.Blue); break; case 3: // find book Console.Write("Book name: "); var findBookName = Console.ReadLine(); var result = bookUtil.FindBookBy(findBookName); if (result != null) { Console.WriteLine("Info Book---"); Console.WriteLine("Id: " + result.BookId); Console.WriteLine("Book Name: " + result.BookName); Console.WriteLine("Author: " + result.Author); Console.WriteLine("Category: " + result.Category); Console.WriteLine("-----------------------------"); break; } HandleRandom.ChooseColorForString("There are no book you find", ConsoleColor.Blue); break; case 4: Console.Write("Book name to delete: "); var findBookToDelete = Console.ReadLine(); Console.WriteLine($"Are you sure wants to delete '{findBookToDelete}'"); Console.Write("Y/n? "); var yesNoQuestion = Console.ReadLine(); if (yesNoQuestion.ToLower() == "y") { if (bookUtil.DeleteBook(findBookToDelete)) { HandleRandom.ChooseColorForString("Deleted successful", ConsoleColor.Blue); break; } HandleRandom.ChooseColorForString("Nothing book name to delete", ConsoleColor.DarkRed); break; } break; case 5: // update book Console.Write("Find book to UPDATE: "); var placeHoldBookName = Console.ReadLine(); if (bookUtil.FindBookBy(placeHoldBookName) != null) { var currentBookResult = bookUtil.FindBookBy(placeHoldBookName); HandleRandom.ChooseColorForString("Found book", ConsoleColor.Blue); Console.WriteLine("Book Info---"); Console.WriteLine("Id: " + currentBookResult.BookId); Console.WriteLine("Book Name: " + currentBookResult.BookName); Console.WriteLine("Author: " + currentBookResult.Author); Console.WriteLine("Category: " + currentBookResult.Category); Console.WriteLine("-----------------------------"); Console.Write("Book name to update: "); var bookNameToUpdate = Console.ReadLine(); Console.Write("Author to update: "); var authorToUpdate = Console.ReadLine(); Console.WriteLine("Types of Category"); var counts = 1; var listConstantValues = typeof(CategoriesBookConstant).GetAllPublicConstantValues <string>(); listConstantValues.Sort(); foreach (var propertyInfo in listConstantValues) { Console.WriteLine(counts++ + ". " + propertyInfo); } Console.Write("Please choose a category to update: "); var categoryToUpdate = Console.ReadLine(); if (categoryToUpdate != null) { if (int.Parse(categoryToUpdate) > 0 && int.Parse(categoryToUpdate) <= listConstantValues.Count) { categoryToUpdate = listConstantValues[int.Parse(categoryToUpdate) - 1]; } } if (bookUtil.UpdateBook(currentBookResult.BookName, bookNameToUpdate, authorToUpdate, categoryToUpdate)) { HandleRandom.ChooseColorForString("Update book success", ConsoleColor.Blue); break; } HandleRandom.ChooseColorForString("Update book Failed", ConsoleColor.DarkRed); } else { HandleRandom.ChooseColorForString("Nothing found book", ConsoleColor.DarkRed); } break; case 6: // save book to excel var saveFile = new SaveFileDialog(); Console.Write("Table Name: "); var tableName = Console.ReadLine(); var filePath = ""; if (saveFile.ShowDialog() == DialogResult.OK) { filePath = Path.GetFullPath(saveFile.FileName); } bookUtil.SaveBookToExcel(filePath, tableName); break; case 7: break; } if (choose == 0) { HandleRandom.ChooseColorForString("There is no option you chose", ConsoleColor.Blue); Thread.Sleep(2000); Environment.Exit(0); } else { Console.WriteLine("1. Get list books"); Console.WriteLine("2. Add book"); Console.WriteLine("3. Find book"); Console.WriteLine("4. Delete book"); Console.WriteLine("5. Upate book"); Console.WriteLine("6. Save book to Excel"); Console.WriteLine("0. Exit"); Console.Write("Choose: "); } } }