using BuechermarktClient.Models; using System.ComponentModel; using System.Linq; using System.Windows; using MongoDB.Driver; using MongoDB.Bson; using System.Collections.Generic; using System.Windows.Data; using System; using System.Globalization; using System.Threading; using System.Windows.Input; using System.Windows.Controls; using System.Collections.Specialized; using System.Collections.ObjectModel; namespace BuechermarktClient { /// /// Interaktionslogik für Sell.xaml /// public partial class Sell : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private string _AddField; public string AddField { get { return _AddField; } set { string val = value.ToUpper(); if(val != _AddField) { _AddField = val; OnPropertyChanged("AddField"); } } } private int _SvSum; public int SvSum { get { return _SvSum; } set { if (value != _SvSum) { _SvSum = value; OnPropertyChanged("SvSum"); } } } private double _TotalSum; public double TotalSum { get { return _TotalSum; } set { if(value != _TotalSum) { _TotalSum = value; OnPropertyChanged("TotalSum"); } } } private readonly ObservableCollection _SellBooks; public ObservableCollection SellBooks { get { return _SellBooks; } } private void listChanged(object sender, NotifyCollectionChangedEventArgs args) { SvSum = SellBooks.Count; var pr = 0.0; foreach(var sb in SellBooks) { pr += sb.Price; } TotalSum = SvSum + pr; } public Sell() { InitializeComponent(); DataContext = this; _SellBooks = new ObservableCollection(); _SellBooks.CollectionChanged += listChanged; } private void Add_Click(object sender, RoutedEventArgs e) { while (true) { try { foreach (var s in SellBooks) { if(s.LabelId == AddField) { MessageBox.Show("Das Buch ist bereits in der Liste"); return; } } var item = MainWindow.BookCollection.FindSync(b => b.LabelId == AddField).FirstOrDefault(); if (item == null) { MessageBox.Show("Das Buch ist nicht vorhanden bitte überprüfen"); return; } var stud = MainWindow.StudentCollection.FindSync(s => s.ID == item.Student).FirstOrDefault(); if (stud == null) { MessageBox.Show("Der Schüler ist nicht vorhanden bitte überprüfen"); return; } var type = MainWindow.BookTypeCollection.FindSync(bt => bt.ID == item.BookType).FirstOrDefault(); if (type == null) { MessageBox.Show("Der Buchtyp (ObjId: " + item.BookType + ") ist nicht vorhanden bitte überprüfen"); return; } var sb = new SellBook(); sb.StudentId = stud.LabelId; sb.LabelId = item.LabelId; sb.Price = item.Price; sb.Type = type.Name; AddField = String.Empty; SellBooks.Add(sb); Dispatcher.BeginInvoke(new Action(delegate () { BooksList.ItemsSource = SellBooks; })); return; } catch (MongoExecutionTimeoutException) { Thread.Sleep(100); } } } private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e) { if (sender is ListViewItem item && item.IsSelected) { SellBooks.Remove(item.DataContext as SellBook); Dispatcher.BeginInvoke(new Action(delegate () { BooksList.ItemsSource = SellBooks; })); } } private void SellAll_Click(object sender, RoutedEventArgs e) { while (true) { try { foreach (var sb in SellBooks) { MainWindow.BookCollection.FindOneAndUpdate(b => b.LabelId == sb.LabelId, Builders.Update.Set(b => b.State, Models.BookState.Selled)); } SellBooks.Clear(); Dispatcher.BeginInvoke(new Action(delegate () { BooksList.ItemsSource = SellBooks; })); return; } catch (MongoExecutionTimeoutException) { Thread.Sleep(100); } } } } public class SellBook { public string StudentId { get; set; } public string LabelId { get; set; } public string Type { get; set; } public double Price { get; set; } } }