2017-05-18 10:57:50 +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;
|
2017-06-27 19:54:13 +00:00
|
|
|
|
using System.Threading;
|
2017-05-18 10:57:50 +00:00
|
|
|
|
|
|
|
|
|
namespace BuechermarktClient
|
|
|
|
|
{
|
|
|
|
|
[ValueConversion(typeof(object), typeof(string))]
|
|
|
|
|
public class StringConverter : IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
return value?.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
var style = NumberStyles.AllowDecimalPoint;
|
|
|
|
|
var c = CultureInfo.CreateSpecificCulture("de-DE");
|
|
|
|
|
if (value == null) return null;
|
|
|
|
|
if (!double.TryParse((string)value, style, c, out double outD)) return null;
|
2017-05-18 10:57:50 +00:00
|
|
|
|
return outD;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaktionslogik für BookEdit.xaml
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class BooksEdit : Window, INotifyPropertyChanged
|
|
|
|
|
{
|
|
|
|
|
private bool New = false;
|
|
|
|
|
private Book Book = null;
|
|
|
|
|
|
|
|
|
|
public string LabelId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Book.LabelId;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(value != Book.LabelId)
|
|
|
|
|
{
|
|
|
|
|
Book.LabelId = value;
|
|
|
|
|
OnPropertyChanged("LabelId");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public Student Student
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return MainWindow.StudentCollection.Find(s => s.ID == Book.Student).FirstOrDefault();
|
|
|
|
|
} catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-18 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != null && value.ID != Book.Student)
|
|
|
|
|
{
|
|
|
|
|
Book.Student = value.ID;
|
|
|
|
|
OnPropertyChanged("Student");
|
|
|
|
|
} else if(value == null)
|
|
|
|
|
{
|
2017-05-21 14:23:59 +00:00
|
|
|
|
Book.Student = new MongoDB.Bson.ObjectId(string.Empty);
|
2017-05-18 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string StudentLabelId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Student?.LabelId;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var st = Student;
|
|
|
|
|
if (st == null || value != st.LabelId)
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
var f = true;
|
|
|
|
|
while (f)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Student = MainWindow.StudentCollection.Find(s => s.LabelId.Equals(value)).FirstOrDefault();
|
|
|
|
|
} catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-18 10:57:50 +00:00
|
|
|
|
OnPropertyChanged("StudentLabelId");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ComboS
|
|
|
|
|
{
|
|
|
|
|
public ComboS(string name,BookState state)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Sate = state;
|
|
|
|
|
}
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public BookState Sate { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ComboS> StatesList;
|
|
|
|
|
|
|
|
|
|
public ComboS ComboState
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return StatesList.FirstOrDefault(s => s.Sate == Book.State);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(value.Sate != Book.State)
|
|
|
|
|
{
|
|
|
|
|
Book.State = value.Sate;
|
|
|
|
|
OnPropertyChanged("ComboState");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double Price
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Book.Price;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(value != Book.Price)
|
|
|
|
|
{
|
|
|
|
|
Book.Price = value;
|
|
|
|
|
OnPropertyChanged("Price");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BookType BookType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return MainWindow.BookTypeCollection.Find(b => b.ID == Book.BookType).FirstOrDefault();
|
|
|
|
|
} catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-18 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != null && value.ID != Book.BookType)
|
|
|
|
|
{
|
|
|
|
|
Book.BookType = value.ID;
|
|
|
|
|
OnPropertyChanged("BookType");
|
|
|
|
|
}
|
|
|
|
|
else if (value == null)
|
|
|
|
|
{
|
|
|
|
|
Book.BookType = new ObjectId("0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string BookTypeISBN
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return BookType?.ISBN;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var bt = BookType;
|
|
|
|
|
if(bt == null || value != bt.ISBN)
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
BookType = MainWindow.BookTypeCollection.Find(b => b.ISBN == value).FirstOrDefault();
|
|
|
|
|
OnPropertyChanged("BookTypeISBN");
|
|
|
|
|
return;
|
|
|
|
|
} catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-18 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
protected void OnPropertyChanged(string propertyName)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BooksEdit(Book book)
|
|
|
|
|
{
|
|
|
|
|
StatesList = new List<ComboS>
|
|
|
|
|
{
|
|
|
|
|
new ComboS("Im Lager", BookState.InStock),
|
|
|
|
|
new ComboS("Verkauft", BookState.Selled),
|
2017-05-21 14:23:59 +00:00
|
|
|
|
new ComboS("Verloren", BookState.Missing),
|
2017-05-18 10:57:50 +00:00
|
|
|
|
new ComboS("Zurück an den Schüler", BookState.BackToStudent)
|
|
|
|
|
};
|
|
|
|
|
DataContext = this;
|
|
|
|
|
if (book == null)
|
|
|
|
|
{
|
|
|
|
|
Book = new Book();
|
|
|
|
|
New = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Book = book;
|
|
|
|
|
}
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
StateComboBox.ItemsSource = StatesList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Save_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (New)
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
MainWindow.BookCollection.InsertOne(Book);
|
|
|
|
|
var oldStudent = Student;
|
|
|
|
|
Book = new Book();
|
|
|
|
|
Student = oldStudent;
|
|
|
|
|
Book.State = BookState.InStock;
|
|
|
|
|
OnPropertyChanged(string.Empty);
|
|
|
|
|
return;
|
|
|
|
|
} catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-18 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
MainWindow.BookCollection.FindOneAndUpdate(b => b.ID == Book.ID, Builders<Book>.Update
|
|
|
|
|
.Set(b => b.Student, Book.Student)
|
|
|
|
|
.Set(b => b.BookType, Book.BookType)
|
|
|
|
|
.Set(b => b.Price, Book.Price)
|
|
|
|
|
.Set(b => b.State, Book.State)
|
|
|
|
|
.Set(b => b.LabelId, Book.LabelId));
|
|
|
|
|
Close();
|
|
|
|
|
return;
|
|
|
|
|
} catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-18 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Delete_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (Book != null && Book.ID != null)
|
|
|
|
|
{
|
2017-06-27 19:54:13 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
MainWindow.StudentCollection.DeleteOne(bt => bt.ID == Student.ID);
|
|
|
|
|
Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
Close();
|
2017-05-18 10:57:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SelectBookType_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var btd = new BookTypes(true);
|
|
|
|
|
btd.ShowDialog();
|
|
|
|
|
if (btd.DialogResult == true)
|
|
|
|
|
{
|
|
|
|
|
BookTypeISBN = btd.SelectedItem.ISBN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BookTypeDetails_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var bte = new BookTypesEdit(BookType);
|
|
|
|
|
bte.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StudentDetails_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var se = new StudentsEdit(Student);
|
|
|
|
|
se.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SelectStudent_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var sd = new Students(true);
|
|
|
|
|
sd.ShowDialog();
|
|
|
|
|
if (sd.DialogResult == true)
|
|
|
|
|
{
|
|
|
|
|
StudentLabelId = sd.SelectedItem.LabelId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|