buechermarkt/BuechermarktClient/BooksEdit.xaml.cs
2017-05-21 16:23:59 +02:00

267 lines
6.7 KiB
C#

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;
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)
{
if(value == null) return null;
if (!double.TryParse((string)value, out double outD)) return null;
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
{
return MainWindow.StudentCollection.Find(s => s.ID == Book.Student).FirstOrDefault();
}
set
{
if (value != null && value.ID != Book.Student)
{
Book.Student = value.ID;
OnPropertyChanged("Student");
} else if(value == null)
{
Book.Student = new MongoDB.Bson.ObjectId(string.Empty);
}
}
}
public string StudentLabelId
{
get
{
return Student?.LabelId;
}
set
{
var st = Student;
if (st == null || value != st.LabelId)
{
Student = MainWindow.StudentCollection.Find(s => s.LabelId.Equals(value)).FirstOrDefault();
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
{
return MainWindow.BookTypeCollection.Find(b => b.ID == Book.BookType).FirstOrDefault();
}
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)
{
BookType = MainWindow.BookTypeCollection.Find(b => b.ISBN == value).FirstOrDefault();
OnPropertyChanged("BookTypeISBN");
}
}
}
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),
new ComboS("Verloren", BookState.Missing),
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)
{
MainWindow.BookCollection.InsertOne(Book);
var oldStudent = Student;
Book = new Book();
Student = oldStudent;
Book.State = BookState.InStock;
OnPropertyChanged(string.Empty);
}
else
{
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();
}
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
if (Book != null && Book.ID != null)
{
MainWindow.StudentCollection.DeleteOne(bt => bt.ID == Student.ID);
}
Close();
}
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;
}
}
}
}