123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Bookstore;
- using MongoDB.Bson;
- using MongoDB.Driver;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace BookStore
- {
- public partial class MainWindow : Window
- {
- private BookStoreContext _context;
- public MainWindow()
- {
- InitializeComponent();
- _context = new BookStoreContext();
- Load();
- }
- private void Load()
- {
- BooksGrid.ItemsSource = _context.GetCollection<Book>("Book").Find(new BsonDocument()).ToList();
- }
- private void BtnUpdate_Click(object sender, RoutedEventArgs e)
- {
- 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.GetCollection<Book>("Book").ReplaceOne(x => x.Id == selectedBook.Id, selectedBook);
- Load();
- }
- }
- private void BookGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (BooksGrid.SelectedItem is Book selectedBook)
- {
- TbName.Text = selectedBook.Name;
- TbPrice.Text = selectedBook.Price.ToString();
- TbAuthor.Text = selectedBook.Author;
- TbCategory.Text = selectedBook.Category;
- }
- }
- private void BtnDelete_Click(object sender, RoutedEventArgs e)
- {
- if (BooksGrid.SelectedItem is Book selectedBook)
- {
- _context.GetCollection<Book>("Book").DeleteOne(x => x.Id == selectedBook.Id);
- Load();
- }
- }
- private void BtnInsert_Click(object sender, RoutedEventArgs e)
- {
- if (!decimal.TryParse(TbPrice.Text, out decimal price))
- {
- return;
- }
- Book book = new()
- {
- Name = TbName.Text,
- Price = price,
- Author = TbAuthor.Text,
- Category = TbCategory.Text,
- };
- _context.GetCollection<Book>("Book").InsertOne(book);
- Load();
- }
- }
- }
|