MainWindow.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 boo
  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. InsertBook(TbName.Text, TbPrice.Text, TbAuthor.Text, TbCategory.Text);
  36. }
  37. public bool InsertBook(string name, string priceStr, string author, string category)
  38. {
  39. if (!decimal.TryParse(priceStr, out decimal price))
  40. {
  41. return false;
  42. }
  43. if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(author) || string.IsNullOrWhiteSpace(category))
  44. {
  45. return false;
  46. }
  47. Books book = new Books()
  48. {
  49. Name = name,
  50. Price = price,
  51. Author = author,
  52. Category = category
  53. };
  54. _context.Books.Add(book);
  55. _context.SaveChanges();
  56. Load();
  57. return true;
  58. }
  59. private void BtnUpdate_Click(object sender, RoutedEventArgs e)
  60. {
  61. UpdateBook(TbName.Text, TbPrice.Text, TbAuthor.Text, TbCategory.Text);
  62. }
  63. public bool UpdateBook(string name, string priceStr, string author, string category)
  64. {
  65. if (BooksGrid.SelectedItem is Books selectedBook)
  66. {
  67. if (!decimal.TryParse(priceStr, out decimal price))
  68. {
  69. return false;
  70. }
  71. if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(author) || string.IsNullOrWhiteSpace(category))
  72. {
  73. return false;
  74. }
  75. selectedBook.Name = name;
  76. selectedBook.Price = price;
  77. selectedBook.Category = category;
  78. selectedBook.Author = author;
  79. _context.SaveChanges();
  80. Load();
  81. return true;
  82. }
  83. return false;
  84. }
  85. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  86. {
  87. DeleteBook();
  88. }
  89. public void DeleteBook()
  90. {
  91. if (BooksGrid.SelectedItem is Books selectedBook)
  92. {
  93. _context.Books.Remove(selectedBook);
  94. _context.SaveChanges();
  95. Load();
  96. }
  97. }
  98. private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  99. {
  100. if (BooksGrid.SelectedItem is Books selectedBook)
  101. {
  102. TbName.Text = selectedBook.Name;
  103. TbPrice.Text = selectedBook.Price.ToString();
  104. TbAuthor.Text = selectedBook.Author;
  105. TbCategory.Text = selectedBook.Category;
  106. }
  107. }
  108. }
  109. }