buechermarkt/BuechermarktClient/Sell.xaml.cs

207 lines
5.7 KiB
C#
Raw Normal View History

2017-09-27 13:57:10 +00:00
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
{
/// <summary>
/// Interaktionslogik für Sell.xaml
/// </summary>
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)
2017-09-27 13:57:10 +00:00
{
_AddField = val;
2017-09-27 13:57:10 +00:00
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<SellBook> _SellBooks;
public ObservableCollection<SellBook> 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<SellBook>();
_SellBooks.CollectionChanged += listChanged;
}
private void Add_Click(object sender, RoutedEventArgs e)
{
while (true)
{
try
{
foreach (var s in SellBooks)
{
if(s.LabelId == AddField)
2017-09-27 13:57:10 +00:00
{
MessageBox.Show("Das Buch ist bereits in der Liste");
return;
}
}
var item = MainWindow.BookCollection.FindSync<Models.Book>(b => b.LabelId == AddField).FirstOrDefault();
2017-09-27 13:57:10 +00:00
if (item == null)
{
MessageBox.Show("Das Buch ist nicht vorhanden bitte überprüfen");
return;
}
var stud = MainWindow.StudentCollection.FindSync<Models.Student>(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<Models.BookType>(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<Models.Book>(b => b.LabelId == sb.LabelId, Builders<Book>.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; }
}
}