MainWindow.xaml.cs 3.1 KB

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