MainWindow.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Linq;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace BookStoreFramework
  5. {
  6. /// <summary>
  7. /// Логика взаимодействия для MainWindow.xaml
  8. /// </summary>
  9. public partial class MainWindow : Window
  10. {
  11. private BookStoreContext _context;
  12. public MainWindow()
  13. {
  14. InitializeComponent();
  15. _context = new BookStoreContext();
  16. Load();
  17. }
  18. private void Load()
  19. {
  20. BooksGrid.ItemsSource = _context.Books.ToList();
  21. }
  22. private void BtnInsert_Click(object sender, RoutedEventArgs e)
  23. {
  24. InsertBook(TbName.Text, TbPrice.Text, TbAuthor.Text, TbCategory.Text);
  25. }
  26. public bool InsertBook(string name, string priceStr, string author, string category)
  27. {
  28. if (!decimal.TryParse(priceStr, out decimal price))
  29. {
  30. return false;
  31. }
  32. if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(author) || string.IsNullOrWhiteSpace(category))
  33. {
  34. return false;
  35. }
  36. Books book = new Books()
  37. {
  38. Name = name,
  39. Price = price,
  40. Author = author,
  41. Category = category
  42. };
  43. _context.Books.Add(book);
  44. _context.SaveChanges();
  45. Load();
  46. return true;
  47. }
  48. private void BtnUpdate_Click(object sender, RoutedEventArgs e)
  49. {
  50. UpdateBook(TbName.Text, TbPrice.Text, TbAuthor.Text, TbCategory.Text);
  51. }
  52. public bool UpdateBook(string name, string priceStr, string author, string category)
  53. {
  54. if (BooksGrid.SelectedItem is Books selectedBook)
  55. {
  56. if (!decimal.TryParse(priceStr, out decimal price))
  57. {
  58. return false;
  59. }
  60. if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(author) || string.IsNullOrWhiteSpace(category))
  61. {
  62. return false;
  63. }
  64. selectedBook.Name = name;
  65. selectedBook.Price = price;
  66. selectedBook.Category = category;
  67. selectedBook.Author = author;
  68. _context.SaveChanges();
  69. Load();
  70. return true;
  71. }
  72. return false;
  73. }
  74. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  75. {
  76. DeleteBook();
  77. }
  78. public void DeleteBook()
  79. {
  80. if (BooksGrid.SelectedItem is Books selectedBook)
  81. {
  82. _context.Books.Remove(selectedBook);
  83. _context.SaveChanges();
  84. Load();
  85. }
  86. }
  87. private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  88. {
  89. if (BooksGrid.SelectedItem is Books selectedBook)
  90. {
  91. TbName.Text = selectedBook.Name;
  92. TbPrice.Text = selectedBook.Price.ToString();
  93. TbAuthor.Text = selectedBook.Author;
  94. TbCategory.Text = selectedBook.Category;
  95. }
  96. }
  97. }
  98. }