MainWindow.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Microsoft.EntityFrameworkCore;
  2. using Microsoft.EntityFrameworkCore.SqlServer;
  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. 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 void InsertBook(string name, string priceStr, string author, string category)
  38. {
  39. if(!decimal.TryParse(priceStr, out decimal price))
  40. {
  41. return;
  42. }
  43. Book book = new()
  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. }
  54. private void BtnUpdate_Click(object sender, RoutedEventArgs e)
  55. {
  56. UpdateBook(TbName.Text, TbPrice.Text, TbAuthor.Text, TbCategory.Text);
  57. }
  58. public void UpdateBook(string name, string priceStr, string author, string category)
  59. {
  60. if (BooksGrid.SelectedItem is Book selectedBook)
  61. {
  62. if (!decimal.TryParse(priceStr, out decimal price))
  63. {
  64. return;
  65. }
  66. selectedBook.Name = name;
  67. selectedBook.Price = price;
  68. selectedBook.Category = category;
  69. selectedBook.Author = author;
  70. _context.SaveChanges();
  71. Load();
  72. }
  73. }
  74. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  75. {
  76. DeleteBook();
  77. }
  78. public void DeleteBook()
  79. {
  80. if (BooksGrid.SelectedItem is Book selectedBook)
  81. {
  82. _context.Remove(selectedBook);
  83. _context.SaveChanges();
  84. Load();
  85. }
  86. }
  87. private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  88. {
  89. SelectBook();
  90. }
  91. public void SelectBook()
  92. {
  93. if (BooksGrid.SelectedItem is Book selectedBook)
  94. {
  95. TbName.Text = selectedBook.Name;
  96. TbPrice.Text = selectedBook.Price.ToString();
  97. TbAuthor.Text = selectedBook.Author;
  98. TbCategory.Text = selectedBook.Category;
  99. }
  100. }
  101. }
  102. public class Book
  103. {
  104. public int Id { get; set; }
  105. public string Name { get; set; }
  106. public decimal Price { get; set; }
  107. public string Category { get; set; }
  108. public string Author { get; set; }
  109. }
  110. public class BookStoreContext : DbContext
  111. {
  112. public DbSet<Book> Books { get; set; }
  113. public BookStoreContext()
  114. {
  115. }
  116. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  117. {
  118. optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BookStore"].ConnectionString);
  119. }
  120. }
  121. }