From 129e0679cd58db9538607a5f58c3fb3474b73c88 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Thu, 18 May 2017 12:57:50 +0200 Subject: [PATCH] Basic Implemetation of all needed function to take Books and create Students etc. Missing is the automatic Server Identification and Signing Stuff --- BuechermarktClient/Backup.cs | 67 + BuechermarktClient/BookTypes.xaml | 11 +- BuechermarktClient/BookTypes.xaml.cs | 71 +- BuechermarktClient/Books.xaml | 39 + BuechermarktClient/Books.xaml.cs | 120 + BuechermarktClient/BooksEdit.xaml | 82 + BuechermarktClient/BooksEdit.xaml.cs | 266 ++ BuechermarktClient/BuechermarktClient.csproj | 20 +- BuechermarktClient/MainWindow.xaml | 2 + BuechermarktClient/MainWindow.xaml.cs | 45 +- BuechermarktClient/Models/Book.cs | 5 +- BuechermarktClient/SigningService.cs | 91 + BuechermarktClient/Students.xaml | 11 +- BuechermarktClient/Students.xaml.cs | 68 +- BuechermarktClient/StudentsEdit.xaml | 2 +- BuechermarktClient/StudentsEdit.xaml.cs | 10 +- BuechermarktClient/bin/Debug/RestSharp.xml | 3095 ------------- BuechermarktClient/obj/Debug/Book.g.i.cs | 121 + BuechermarktClient/obj/Debug/BookEdit.g.i.cs | 75 + BuechermarktClient/obj/Debug/BookTypes.baml | Bin 1670 -> 1982 bytes BuechermarktClient/obj/Debug/BookTypes.g.cs | 8 +- BuechermarktClient/obj/Debug/BookTypes.g.i.cs | 8 +- BuechermarktClient/obj/Debug/Books.baml | Bin 0 -> 2280 bytes BuechermarktClient/obj/Debug/Books.g.cs | 121 + BuechermarktClient/obj/Debug/Books.g.i.cs | 121 + BuechermarktClient/obj/Debug/BooksEdit.baml | Bin 0 -> 4170 bytes BuechermarktClient/obj/Debug/BooksEdit.g.cs | 137 + BuechermarktClient/obj/Debug/BooksEdit.g.i.cs | 137 + ...hermarktClient.csproj.FileListAbsolute.txt | 42 +- .../obj/Debug/BuechermarktClient.g.resources | Bin 9425 -> 16777 bytes .../Debug/BuechermarktClient_Content.g.i.cs | 13 + .../BuechermarktClient_MarkupCompile.i.lref | 4 + .../BuechermarktClient_MarkupCompile.lref | 6 +- .../Debug/GeneratedInternalTypeHelper.g.cs | 62 + .../Debug/GeneratedInternalTypeHelper.g.i.cs | 62 + BuechermarktClient/obj/Debug/MainWindow.baml | Bin 1474 -> 1659 bytes BuechermarktClient/obj/Debug/MainWindow.g.cs | 14 +- .../obj/Debug/MainWindow.g.i.cs | 14 +- BuechermarktClient/obj/Debug/Students.baml | Bin 1999 -> 2319 bytes BuechermarktClient/obj/Debug/Students.g.cs | 12 +- BuechermarktClient/obj/Debug/Students.g.i.cs | 12 +- .../obj/Debug/StudentsEdit.baml | Bin 2193 -> 2194 bytes .../obj/Debug/StudentsEdit.g.cs | 2 +- .../obj/Debug/StudentsEdit.g.i.cs | 2 +- BuechermarktClient/packages.config | 1 - .../RestSharp.105.2.3/RestSharp.105.2.3.nupkg | Bin 1152099 -> 0 bytes .../lib/MonoAndroid10/RestSharp.xml | 3020 ------------- .../lib/MonoTouch10/RestSharp.xml | 3020 ------------- .../lib/Xamarin.iOS10/RestSharp.xml | 3020 ------------- .../RestSharp.105.2.3/lib/net35/RestSharp.xml | 2858 ------------ .../lib/net4-client/RestSharp.xml | 3095 ------------- .../RestSharp.105.2.3/lib/net4/RestSharp.xml | 3095 ------------- .../RestSharp.105.2.3/lib/net45/RestSharp.xml | 3095 ------------- .../lib/net451/RestSharp.xml | 3095 ------------- .../lib/net452/RestSharp.xml | 3095 ------------- .../RestSharp.105.2.3/lib/net46/RestSharp.xml | 3095 ------------- .../RestSharp.105.2.3/lib/sl5/RestSharp.xml | 2649 ----------- .../lib/windowsphone8/RestSharp.xml | 3866 ----------------- .../lib/windowsphone81/RestSharp.xml | 3866 ----------------- packages/RestSharp.105.2.3/readme.txt | 20 - 60 files changed, 1799 insertions(+), 44069 deletions(-) create mode 100644 BuechermarktClient/Backup.cs create mode 100644 BuechermarktClient/Books.xaml create mode 100644 BuechermarktClient/Books.xaml.cs create mode 100644 BuechermarktClient/BooksEdit.xaml create mode 100644 BuechermarktClient/BooksEdit.xaml.cs create mode 100644 BuechermarktClient/SigningService.cs delete mode 100644 BuechermarktClient/bin/Debug/RestSharp.xml create mode 100644 BuechermarktClient/obj/Debug/Book.g.i.cs create mode 100644 BuechermarktClient/obj/Debug/BookEdit.g.i.cs create mode 100644 BuechermarktClient/obj/Debug/Books.baml create mode 100644 BuechermarktClient/obj/Debug/Books.g.cs create mode 100644 BuechermarktClient/obj/Debug/Books.g.i.cs create mode 100644 BuechermarktClient/obj/Debug/BooksEdit.baml create mode 100644 BuechermarktClient/obj/Debug/BooksEdit.g.cs create mode 100644 BuechermarktClient/obj/Debug/BooksEdit.g.i.cs create mode 100644 BuechermarktClient/obj/Debug/BuechermarktClient_Content.g.i.cs create mode 100644 BuechermarktClient/obj/Debug/BuechermarktClient_MarkupCompile.i.lref create mode 100644 BuechermarktClient/obj/Debug/GeneratedInternalTypeHelper.g.cs create mode 100644 BuechermarktClient/obj/Debug/GeneratedInternalTypeHelper.g.i.cs delete mode 100644 packages/RestSharp.105.2.3/RestSharp.105.2.3.nupkg delete mode 100644 packages/RestSharp.105.2.3/lib/MonoAndroid10/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/MonoTouch10/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/Xamarin.iOS10/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/net35/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/net4-client/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/net4/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/net45/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/net451/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/net452/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/net46/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/sl5/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/windowsphone8/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/lib/windowsphone81/RestSharp.xml delete mode 100644 packages/RestSharp.105.2.3/readme.txt 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 @@ +