diff --git a/BuechermarktClient/Backup.cs b/BuechermarktClient/Backup.cs new file mode 100644 index 0000000..f8c9502 --- /dev/null +++ b/BuechermarktClient/Backup.cs @@ -0,0 +1,67 @@ +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.IO.Compression; + +namespace BuechermarktClient +{ + public class Backup + { + public static void MakeBackup() { + string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); + string backupfolder = folder + "\\backups"; + if (!Directory.Exists(backupfolder)){ + Directory.CreateDirectory(backupfolder); + } + string tbackupfoldername = "" + (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; + string tbackupfolder = backupfolder + "\\" + tbackupfoldername; + Directory.CreateDirectory(tbackupfolder); + + var bt = MainWindow.BookTypeCollection.Find(e => true).ToList(); + var json = JsonConvert.SerializeObject(bt); + StringToFile(tbackupfolder + "\\BookTypes.json", json); + + var b = MainWindow.BookCollection.Find(e => true).ToList(); + json = JsonConvert.SerializeObject(b); + StringToFile(tbackupfolder + "\\Book.json", json); + + var s = MainWindow.StudentCollection.Find(e => true).ToList(); + json = JsonConvert.SerializeObject(s); + StringToFile(tbackupfolder + "\\Students.json", json); + + ZipFile.CreateFromDirectory(tbackupfolder, tbackupfolder + ".zip"); + DeleteDirectory(tbackupfolder); + } + + private static void StringToFile(string filepath, string json) + { + //File.Create(filepath); + File.WriteAllText(filepath, json); + } + + public static void DeleteDirectory(string target_dir) + { + string[] files = Directory.GetFiles(target_dir); + string[] dirs = Directory.GetDirectories(target_dir); + + foreach (string file in files) + { + File.SetAttributes(file, FileAttributes.Normal); + File.Delete(file); + } + + foreach (string dir in dirs) + { + DeleteDirectory(dir); + } + + Directory.Delete(target_dir, false); + } + } +} \ No newline at end of file diff --git a/BuechermarktClient/BookTypes.xaml b/BuechermarktClient/BookTypes.xaml index 7a6a4ea..26f615a 100644 --- a/BuechermarktClient/BookTypes.xaml +++ b/BuechermarktClient/BookTypes.xaml @@ -8,10 +8,19 @@ Title="BookTypes" Height="300" Width="300"> + - + + + + + + + + + diff --git a/BuechermarktClient/BookTypes.xaml.cs b/BuechermarktClient/BookTypes.xaml.cs index df2a5f9..9fc0851 100644 --- a/BuechermarktClient/BookTypes.xaml.cs +++ b/BuechermarktClient/BookTypes.xaml.cs @@ -1,19 +1,12 @@ 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.Shapes; using BuechermarktClient.Models; using System.Threading; using MongoDB.Driver; +using System.Collections.Generic; namespace BuechermarktClient { @@ -25,8 +18,32 @@ namespace BuechermarktClient public Thread RefreshThread = null; private bool ThreadRunning = true; - public BookTypes() + public bool Chooser = false; + + private string _SearchField; + public string SearchField { + get + { + return _SearchField; + } + set + { + if(value != _SearchField) + { + _SearchField = value; + LoadList(); + } + } + } + + public BookType SelectedItem { get; private set; } + + public BookTypes() : this(false) { } + + public BookTypes(bool select) { + DataContext = this; + Chooser = select; InitializeComponent(); RefreshThread = new Thread(RefreshThreadS); RefreshThread.Start(); @@ -51,8 +68,21 @@ namespace BuechermarktClient public void LoadList() { var list = MainWindow.BookTypeCollection.FindSync((b)=>true).ToList(); + var show = new List(); + if(SearchField != null && SearchField != String.Empty) + { + foreach(var e in list) + { + if (e.ISBN.ToLower().Contains(SearchField.ToLower()) || e.Name.ToLower().Contains(SearchField.ToLower())) + { + show.Add(e); + } + } + } else { + show = list; + } Dispatcher.BeginInvoke(new Action(delegate (){ - BookTypesList.ItemsSource = list; + BookTypesList.ItemsSource = show; })); } @@ -67,13 +97,22 @@ namespace BuechermarktClient private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e) { - var item = sender as ListViewItem; - if (item != null && item.IsSelected) + if (sender is ListViewItem item && item.IsSelected) { - var editWindow = new BookTypesEdit(item.DataContext as BookType) { - Owner = this - }; - editWindow.Show(); + SelectedItem = item.DataContext as BookType; + if (Chooser) + { + DialogResult = true; + Close(); + } + else + { + var editWindow = new BookTypesEdit(item.DataContext as BookType) + { + Owner = this + }; + editWindow.Show(); + } } } } diff --git a/BuechermarktClient/Books.xaml b/BuechermarktClient/Books.xaml new file mode 100644 index 0000000..7b96a98 --- /dev/null +++ b/BuechermarktClient/Books.xaml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BuechermarktClient/Books.xaml.cs b/BuechermarktClient/Books.xaml.cs new file mode 100644 index 0000000..8900229 --- /dev/null +++ b/BuechermarktClient/Books.xaml.cs @@ -0,0 +1,120 @@ +using System; +using System.Linq; +using System.Threading; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using MongoDB.Driver; +using System.Windows.Data; +using System.Globalization; +using MongoDB.Bson; + +namespace BuechermarktClient +{ + [ValueConversion(typeof(object), typeof(string))] + public class EnumConverter : 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) + { + throw new NotImplementedException(); + } + } + + [ValueConversion(typeof(object), typeof(string))] + public class StudentConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + var i = MainWindow.StudentCollection.Find(s => s.ID == (ObjectId)value).FirstOrDefault(); + if (i == null) return value; + return i.Lastname + ", " + i.Forname; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } + + [ValueConversion(typeof(object), typeof(string))] + public class BookTypeConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + var i = MainWindow.BookTypeCollection.Find(bt => bt.ID == (ObjectId)value).FirstOrDefault(); + if (i == null) return value; + return i.Name; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } + + /// + /// Interaktionslogik für Books.xaml + /// + public partial class Books : Window + { + public Thread RefreshThread = null; + private bool ThreadRunning = true; + + public Books() + { + InitializeComponent(); + RefreshThread = new Thread(RefreshThreadS); + RefreshThread.Start(); + RefreshThread.IsBackground = true; + Closing += Book_Closing; + } + + private void Book_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + ThreadRunning = false; + } + + public void RefreshThreadS() + { + while (ThreadRunning) + { + LoadList(); + Thread.Sleep(1000); + } + } + + public void LoadList() + { + var list = MainWindow.BookCollection.FindSync((b) => true).ToList(); + Dispatcher.BeginInvoke(new Action(delegate () { + BooksList.ItemsSource = list; + })); + } + + private void AddNew_Click(object sender, RoutedEventArgs e) + { + var editWindow = new BooksEdit(null) + { + Owner = this + }; + editWindow.Show(); + } + + private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e) + { + if (sender is ListViewItem item && item.IsSelected) + { + var editWindow = new BooksEdit(item.DataContext as Models.Book) + { + Owner = this + }; + editWindow.Show(); + } + } + } +} diff --git a/BuechermarktClient/BooksEdit.xaml b/BuechermarktClient/BooksEdit.xaml new file mode 100644 index 0000000..f58e16d --- /dev/null +++ b/BuechermarktClient/BooksEdit.xaml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BuechermarktClient/BooksEdit.xaml.cs b/BuechermarktClient/BooksEdit.xaml.cs new file mode 100644 index 0000000..7bc09ab --- /dev/null +++ b/BuechermarktClient/BooksEdit.xaml.cs @@ -0,0 +1,266 @@ +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; + } + } + + /// + /// Interaktionslogik für BookEdit.xaml + /// + 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("0"); + } + } + } + + 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 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 + { + new ComboS("Im Lager", BookState.InStock), + new ComboS("Verkauft", BookState.Selled), + new ComboS("Verlohren", 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.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; + } + } + } +} diff --git a/BuechermarktClient/BuechermarktClient.csproj b/BuechermarktClient/BuechermarktClient.csproj index 26bf0f7..dbca5a8 100644 --- a/BuechermarktClient/BuechermarktClient.csproj +++ b/BuechermarktClient/BuechermarktClient.csproj @@ -46,11 +46,9 @@ ..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll - - ..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll - + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll @@ -72,6 +70,14 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -96,9 +102,17 @@ App.xaml Code + + + Books.xaml + + + BooksEdit.xaml + BookTypes.xaml + Students.xaml diff --git a/BuechermarktClient/MainWindow.xaml b/BuechermarktClient/MainWindow.xaml index 3875efb..8f9d238 100644 --- a/BuechermarktClient/MainWindow.xaml +++ b/BuechermarktClient/MainWindow.xaml @@ -10,8 +10,10 @@ +