using System.Linq; using System.Windows; using System.Windows.Controls; namespace BookStore { public partial class MainWindow : Window { BookStoreContext _context = new BookStoreContext(); public MainWindow() { InitializeComponent(); Load(); } public void Load() { BooksGrid.ItemsSource = _context.Books.ToList(); } private void BtnInsert_Click(object sender, RoutedEventArgs e) { BIC(); } public void BIC() { if (!decimal.TryParse(TbPrice.Text, out decimal price)) { return; } Book book = new Book() { Name = TbName.Text, Price = price, Author = TbAuthor.Text, Category = TbCategory.Text }; _context.Books.Add(book); _context.SaveChanges(); Load(); } private void BtnUpdate_Click(object sender, RoutedEventArgs e) { BUC(); } public void BUC() { if (BooksGrid.SelectedItem is Book selectedBook) { if (!decimal.TryParse(TbPrice.Text, out decimal price)) { return; } selectedBook.Name = TbName.Text; selectedBook.Price = price; selectedBook.Author = TbAuthor.Text; selectedBook.Category = TbCategory.Text; _context.SaveChanges(); Load(); } } private void BtnDelete_Click(object sender, RoutedEventArgs e) { BDC(); } public void BDC() { if (BooksGrid.SelectedItem is Book selectedBook) { _context.Books.Remove(selectedBook); _context.SaveChanges(); TbClear(); Load(); } } public void TbClear() { TbName.Text = ""; TbPrice.Text = ""; TbAuthor.Text = ""; TbCategory.Text = ""; } private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { BGSC(); } public void BGSC() { if (BooksGrid.SelectedItem is Book selectedBook) { TbName.Text = selectedBook.Name; TbPrice.Text = selectedBook.Price.ToString(); TbAuthor.Text = selectedBook.Author; TbCategory.Text = selectedBook.Category; } } } }