MainWindow.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Microsoft.EntityFrameworkCore;
  2. using Stripe;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace BookStore
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. private BookStoreContext _context;
  26. public MainWindow()
  27. {
  28. InitializeComponent();
  29. _context = new BookStoreContext();
  30. Load();
  31. }
  32. private void Load()
  33. {
  34. BooksGrid.ItemsSource = _context.Books.ToList();
  35. }
  36. private void BtnInsert_Click(object sender, RoutedEventArgs e)
  37. {
  38. if (!decimal.TryParse(TbPrice.Text, out decimal price))
  39. {
  40. return;
  41. }
  42. Book book = new()
  43. {
  44. Name = TbName.Text,
  45. Price = price,
  46. Autor = TbAutor.Text,
  47. Category = TbCategory.Text,
  48. };
  49. _context.Books.Add(book);
  50. _context.SaveChanges();
  51. Load();
  52. }
  53. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  54. {
  55. if (BooksGrid.SelectedItem is Book selectedBook)
  56. {
  57. _context.Books.Remove(selectedBook);
  58. _context.SaveChanges();
  59. Load();
  60. }
  61. }
  62. private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  63. {
  64. if (BooksGrid.SelectedItem is Book selectedBook)
  65. {
  66. TbName.Text = selectedBook.Name;
  67. TbPrice.Text = selectedBook.Price.ToString();
  68. TbAutor.Text = selectedBook.Autor;
  69. TbCategory.Text = selectedBook.Category;
  70. }
  71. }
  72. private void BtnUpdate_Click(object sender, RoutedEventArgs e)
  73. {
  74. if (BooksGrid.SelectedItem is Book selectedBook)
  75. {
  76. if (!decimal.TryParse(TbPrice.Text, out decimal price))
  77. {
  78. return;
  79. }
  80. selectedBook.Name = TbName.Text;
  81. selectedBook.Price = price;
  82. selectedBook.Autor = TbAutor.Text;
  83. selectedBook.Category = TbCategory.Text;
  84. _context.SaveChanges();
  85. Load();
  86. }
  87. }
  88. //private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  89. //{
  90. //}
  91. }
  92. }