diff --git a/LoggingClient/LoggingClient.sln b/LoggingClient/LoggingClient.sln index 19d7900..5fd76d9 100644 --- a/LoggingClient/LoggingClient.sln +++ b/LoggingClient/LoggingClient.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29806.167 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingClient", "LoggingClient\LoggingClient.csproj", "{2622C2FC-3522-4D6F-B021-F63A243E77F1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingClientUnitTest", "LoggingClientUnitTest\LoggingClientUnitTest.csproj", "{6B9F572F-AE97-4A52-89AD-4453BC51E038}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {2622C2FC-3522-4D6F-B021-F63A243E77F1}.Debug|Any CPU.Build.0 = Debug|Any CPU {2622C2FC-3522-4D6F-B021-F63A243E77F1}.Release|Any CPU.ActiveCfg = Release|Any CPU {2622C2FC-3522-4D6F-B021-F63A243E77F1}.Release|Any CPU.Build.0 = Release|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LoggingClient/LoggingClient/App.config b/LoggingClient/LoggingClient/App.config index 2950779..0ad6cd9 100644 --- a/LoggingClient/LoggingClient/App.config +++ b/LoggingClient/LoggingClient/App.config @@ -16,6 +16,7 @@ + diff --git a/LoggingClient/LoggingClient/LoggingClient.csproj b/LoggingClient/LoggingClient/LoggingClient.csproj index 25b0884..7732333 100644 --- a/LoggingClient/LoggingClient/LoggingClient.csproj +++ b/LoggingClient/LoggingClient/LoggingClient.csproj @@ -62,6 +62,7 @@ + @@ -89,6 +90,7 @@ Inventar.tt + True True @@ -110,11 +112,16 @@ + + + + + @@ -209,5 +216,9 @@ + + + + \ No newline at end of file diff --git a/LoggingClient/LoggingClient/Model/Customer.cs b/LoggingClient/LoggingClient/Model/Customer.cs index 3c547e4..2118d2a 100644 --- a/LoggingClient/LoggingClient/Model/Customer.cs +++ b/LoggingClient/LoggingClient/Model/Customer.cs @@ -9,9 +9,6 @@ namespace LoggingClient.Model { - using System; - using System.Collections.Generic; - public partial class Customer { public long customer_id { get; set; } diff --git a/LoggingClient/LoggingClient/Model/CustomerLinq.cs b/LoggingClient/LoggingClient/Model/CustomerLinq.cs new file mode 100644 index 0000000..5fa4088 --- /dev/null +++ b/LoggingClient/LoggingClient/Model/CustomerLinq.cs @@ -0,0 +1,42 @@ +using System.Data.Linq.Mapping; + +namespace LoggingClient.Model +{ + [Table(Name = "inventarisierungsloesung.Customer")] + public class CustomerLinq + { + [Column(IsPrimaryKey = true, Name = "CustomerId")] + public long customer_id { get; set; } + [Column(Name = "FirstName")] + public string firstname { get; set; } + [Column(Name = "LastName")] + public string lastname { get; set; } + [Column(Name = "CustomerNumber")] + public string customernumber { get; set; } + [Column(Name = "KundenKontoFK")] + public long kundenkonto_fk { get; set; } + [Column(Name = "PhoneNumber")] + public string tel { get; set; } + [Column(Name = "EMail")] + public string email { get; set; } + [Column(Name = "URL")] + public string url { get; set; } + [Column(Name = "Password")] + public string password { get; set; } + + public CustomerLinq() { } + + public CustomerLinq(long Id, string FirstName, string LastName, string CustomerNumber, long Customer_fk, string PhoneNumber, string EMail, string URL, string Password) + { + this.customer_id = Id; + this.firstname = FirstName; + this.lastname = LastName; + this.customernumber = CustomerNumber; + this.kundenkonto_fk = Customer_fk; + this.tel = PhoneNumber; + this.email = EMail; + this.url = URL; + this.password = Password; + } + } +} diff --git a/LoggingClient/LoggingClient/Repository/CustomerRepository.cs b/LoggingClient/LoggingClient/Repository/CustomerRepository.cs index 83ae162..c2c6a78 100644 --- a/LoggingClient/LoggingClient/Repository/CustomerRepository.cs +++ b/LoggingClient/LoggingClient/Repository/CustomerRepository.cs @@ -22,7 +22,6 @@ namespace LoggingClient.Repository public override string PrimaryKeyFromTable => "customer_id"; public List Customers { get; set; } - public Customer _Customers { get; set; } public override void Add(Customer entity) { diff --git a/LoggingClient/LoggingClient/Repository/CustomerRepositoryEF.cs b/LoggingClient/LoggingClient/Repository/CustomerRepositoryEF.cs index e93e68d..a78b8f6 100644 --- a/LoggingClient/LoggingClient/Repository/CustomerRepositoryEF.cs +++ b/LoggingClient/LoggingClient/Repository/CustomerRepositoryEF.cs @@ -1,6 +1,7 @@ using LoggingClient.Model; using System; using System.Collections.Generic; +using System.Data.Entity.Migrations; using System.Linq; namespace LoggingClient.Repository @@ -13,6 +14,8 @@ namespace LoggingClient.Repository } public List Customers { get; set; } + public string TableName => throw new NotImplementedException(); + public void Add(Customer newCustomerEntry) { using (var context = new inventarisierungsloesungEntities()) @@ -33,6 +36,16 @@ namespace LoggingClient.Repository } } + public long Count(string whereCondition, Dictionary parameterValues) + { + throw new NotImplementedException(); + } + + public long Count() + { + throw new NotImplementedException(); + } + public void Delete(Customer entity) { using (var context = new inventarisierungsloesungEntities()) @@ -63,7 +76,11 @@ namespace LoggingClient.Repository public void Update(Customer entity) { - throw new NotImplementedException(); + using (var context = new inventarisierungsloesungEntities()) + { + context.Customer.AddOrUpdate(entity); + context.SaveChanges(); + } } } } diff --git a/LoggingClient/LoggingClient/Repository/CustomerRepositoryLinq.cs b/LoggingClient/LoggingClient/Repository/CustomerRepositoryLinq.cs new file mode 100644 index 0000000..f539c5c --- /dev/null +++ b/LoggingClient/LoggingClient/Repository/CustomerRepositoryLinq.cs @@ -0,0 +1,105 @@ +using LoggingClient.Model; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; + +namespace LoggingClient.Repository +{ + public class CustomerRepositoryLinq : IRepositoryBase + { + public CustomerRepositoryLinq() { } + + public string TableName => throw new NotImplementedException(); + + public void Add(Customer entity) + { + using (var context = new inventarisierungsloesungEntities()) + { + var customer = new Customer() + { + firstname = entity.firstname, + lastname = entity.lastname, + customernumber = entity.customernumber, + kundenkonto_fk = entity.kundenkonto_fk, + tel = entity.tel, + email = entity.email, + url = entity.url, + password = entity.password + }; + context.Customer.Add(customer); + context.SaveChanges(); + } + } + + public void CallStoredProcedure(Customer entity) + { + throw new NotImplementedException(); + } + + public long Count(string whereCondition, Dictionary parameterValues) + { + throw new NotImplementedException(); + } + + public long Count() + { + throw new NotImplementedException(); + } + + public void Delete(Customer entity) + { + using (var context = new inventarisierungsloesungEntities()) + { + var x = (from c in context.Customer + where (c.customer_id == entity.customer_id) + select c).SingleOrDefault(); + context.Customer.Remove(x); + context.SaveChanges(); + } + } + + public List GetAll(string whereCondition, Dictionary parameterValues) + { + throw new NotImplementedException(); + } + + public List GetAll() + { + using (var context = new inventarisierungsloesungEntities()) + { + return context.Customer.ToList(); + } + } + + public Customer GetSingle

(P pkValue) + { + throw new NotImplementedException(); + } + + public void Update(Customer entity) + { + using (var context = new inventarisierungsloesungEntities()) + { + var x = (from c in context.Customer + where (c.customer_id == entity.customer_id) + select c).SingleOrDefault(); + + x.firstname = entity.firstname; + x.lastname = entity.lastname; + x.customernumber = entity.customernumber; + x.kundenkonto_fk = entity.kundenkonto_fk; + x.tel = entity.tel; + x.email = entity.email; + x.url = entity.url; + x.password = entity.password; + context.SaveChanges(); + } + } + + List IRepositoryBase.GetAll() + { + throw new NotImplementedException(); + } + } +} diff --git a/LoggingClient/LoggingClient/Repository/LocationRepository.cs b/LoggingClient/LoggingClient/Repository/LocationRepository.cs index 7d153d1..55f1635 100644 --- a/LoggingClient/LoggingClient/Repository/LocationRepository.cs +++ b/LoggingClient/LoggingClient/Repository/LocationRepository.cs @@ -23,7 +23,6 @@ namespace LoggingClient.Repository { try { - location.AddressId = 2; using (var conn = new MySqlConnection(ConnectionString)) { conn.Open(); diff --git a/LoggingClient/LoggingClient/Validators/CustomerNumberValidationRule.cs b/LoggingClient/LoggingClient/Validators/CustomerNumberValidationRule.cs new file mode 100644 index 0000000..33b87f2 --- /dev/null +++ b/LoggingClient/LoggingClient/Validators/CustomerNumberValidationRule.cs @@ -0,0 +1,32 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using System.Windows.Controls; + +namespace LoggingClient.Validators +{ + public class CustomerNumberValidationRule : ValidationRule + { + public string ErrorMessage { get; set; } + + ///

+ /// Regex for Address Number Validation. + /// Beginning with CU following a 5 digit number. + /// + /// + /// + /// + public override ValidationResult Validate(object value, CultureInfo cultureInfo) + { + Regex regex = new Regex(@"^CU[0-9]{5}$"); + Match match = regex.Match(value.ToString()); + if (match == Match.Empty) + { + return new ValidationResult(false, ErrorMessage); + } + else + { + return ValidationResult.ValidResult; + } + } + } +} diff --git a/LoggingClient/LoggingClient/Validators/EmailValidationRule.cs b/LoggingClient/LoggingClient/Validators/EmailValidationRule.cs new file mode 100644 index 0000000..017fc06 --- /dev/null +++ b/LoggingClient/LoggingClient/Validators/EmailValidationRule.cs @@ -0,0 +1,31 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using System.Windows.Controls; + +namespace LoggingClient.Validators +{ + public class EmailValidationRule : ValidationRule + { + public string ErrorMessage { get; set; } + /// + /// Checks an Email Address if it's valid or not + /// Regex doesn't check if the top + subdomains are valid. + /// + /// + /// + /// + public override ValidationResult Validate(object value, CultureInfo cultureInfo) + { + Regex regex = new Regex(@"\A[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z"); + Match match = regex.Match(value.ToString()); + if (match == Match.Empty) + { + return new ValidationResult(false, ErrorMessage); + } + else + { + return ValidationResult.ValidResult; + } + } + } +} diff --git a/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs b/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs index 487f8e2..212ce20 100644 --- a/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs +++ b/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs @@ -10,8 +10,8 @@ namespace LoggingClient.Validators { public class IntRangeValidationRule : ValidationRule { - public int MinimumLength { get; set; } - public int MaximumLength { get; set; } + public int MinimumLength { get; set; } = 1; + public int MaximumLength { get; set; } = 8; public string ErrorMessage { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) diff --git a/LoggingClient/LoggingClient/Validators/PasswordValidationRule.cs b/LoggingClient/LoggingClient/Validators/PasswordValidationRule.cs new file mode 100644 index 0000000..4ea68c0 --- /dev/null +++ b/LoggingClient/LoggingClient/Validators/PasswordValidationRule.cs @@ -0,0 +1,31 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using System.Windows.Controls; + +namespace LoggingClient.Validators +{ + public class PasswordValidationRule : ValidationRule + { + public string ErrorMessage { get; set; } + /// + /// Regex for password validation + /// 8 - 15 characters. At least 1 upper, 1 lowercase, 1 number and 1 special character. + /// + /// + /// + /// + public override ValidationResult Validate(object value, CultureInfo cultureInfo) + { + Regex regex = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$"); + Match match = regex.Match(value.ToString()); + if (match == Match.Empty) + { + return new ValidationResult(false, ErrorMessage); + } + else + { + return ValidationResult.ValidResult; + } + } + } +} diff --git a/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs b/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs index 2e285ff..48874ba 100644 --- a/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs +++ b/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs @@ -10,8 +10,8 @@ namespace LoggingClient.Validators { public class StringRangeValidationRule : ValidationRule { - public int MinimumLength { get; set; } - public int MaximumLength { get; set; } + public int MinimumLength { get; set; } = 1; + public int MaximumLength { get; set; } = 50; public string ErrorMessage { get; set; } diff --git a/LoggingClient/LoggingClient/Validators/UrlValidationRule.cs b/LoggingClient/LoggingClient/Validators/UrlValidationRule.cs new file mode 100644 index 0000000..2172096 --- /dev/null +++ b/LoggingClient/LoggingClient/Validators/UrlValidationRule.cs @@ -0,0 +1,32 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using System.Windows.Controls; + +namespace LoggingClient.Validators +{ + public class UrlValidationRule : ValidationRule + { + public string ErrorMessage { get; set; } + + /// + /// Regex for url Validation. + /// Doesn't check if the url exists. + /// + /// + /// + /// + public override ValidationResult Validate(object value, CultureInfo cultureInfo) + { + Regex regex = new Regex(@"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$"); + Match match = regex.Match(value.ToString()); + if (match == Match.Empty) + { + return new ValidationResult(false, ErrorMessage); + } + else + { + return ValidationResult.ValidResult; + } + } + } +} diff --git a/LoggingClient/LoggingClient/ViewModel/CustomerViewModel.cs b/LoggingClient/LoggingClient/ViewModel/CustomerViewModel.cs index 106408d..a474d28 100644 --- a/LoggingClient/LoggingClient/ViewModel/CustomerViewModel.cs +++ b/LoggingClient/LoggingClient/ViewModel/CustomerViewModel.cs @@ -33,7 +33,7 @@ namespace LoggingClient.ViewModel } } - public Customer NewCustomerEntry { get; set; } + public Customer SelectedItem { get; set; } private long _customerId; public long customer_id @@ -58,6 +58,8 @@ namespace LoggingClient.ViewModel } private bool _LinqIsChecked; + private Customer mySelectedItem; + public bool LinqIsChecked { get { return _LinqIsChecked; } @@ -67,17 +69,26 @@ namespace LoggingClient.ViewModel OnPropertyChanged(nameof(LinqIsChecked)); } } - public Customer SelectedItem { get; set; } + public CustomerViewModel() { TxtConnectionString = "Server=localhost;Database=inventarisierungsloesung;Uid=root;Pwd=MySQLPassword1234!;"; Customers = new List(); - NewCustomerEntry = new Customer(); + _dupChecker = new DuplicateChecker(); } - public Customer MySelectedItem { get; set; } + public Customer MySelectedItem + { + get => mySelectedItem; + set + { + mySelectedItem = value; + this.OnPropertyChanged("MySelectedItem"); + + } + } public event PropertyChangedEventHandler PropertyChanged; public string TxtConnectionString @@ -161,6 +172,8 @@ namespace LoggingClient.ViewModel { var customerModelRepositoryEF = new CustomerRepositoryEF(); Customers = customerModelRepositoryEF.GetAll(); + + this.MySelectedItem = new Customer(); } catch (Exception ex) { @@ -169,7 +182,15 @@ namespace LoggingClient.ViewModel } if (_LinqIsChecked) { - //TODO + try + { + var customerModelRepositoryLinq = new CustomerRepositoryLinq(); + Customers = customerModelRepositoryLinq.GetAll().ToList(); + } + catch (Exception ex) + { + MessageBox.Show("Error occurred: " + ex.Message); + } } if (!_EfIsChecked && !_LinqIsChecked) { @@ -191,24 +212,47 @@ namespace LoggingClient.ViewModel try { var customerModelRepositoryEF = new CustomerRepositoryEF(); - customerModelRepositoryEF.Add(NewCustomerEntry); + customerModelRepositoryEF.Add(MySelectedItem); Customers = customerModelRepositoryEF.GetAll(); } catch (Exception ex) { - MessageBox.Show("Error occurred: " + ex.Message); + if (MySelectedItem.kundenkonto_fk == 0) + { + MessageBox.Show("Error occurred: CustAcck ID darf nicht 0 sein"); + } + else + { + MessageBox.Show("Error occurred: " + ex.Message); + } } } if (_LinqIsChecked) { - // TODO + try + { + var customerModelRepositoryLinq = new CustomerRepositoryLinq(); + customerModelRepositoryLinq.Add(MySelectedItem); + Customers = customerModelRepositoryLinq.GetAll(); + } + catch (Exception ex) + { + if (MySelectedItem.kundenkonto_fk == 0) + { + MessageBox.Show("Error occurred: CustAcck ID darf nicht 0 sein"); + } + else + { + MessageBox.Show("Error occurred: " + ex.Message); + } + } } if (!_EfIsChecked && !_LinqIsChecked) { try { var customerModelRepository = new CustomerRepository(TxtConnectionString); - customerModelRepository.Add(this.NewCustomerEntry); + customerModelRepository.Add(MySelectedItem); this.Customers = customerModelRepository.GetAll().ToList(); } catch (Exception ex) @@ -232,12 +276,25 @@ namespace LoggingClient.ViewModel MessageBox.Show("Error occurred: " + ex.Message); } } + if (_LinqIsChecked) + { + try + { + var customerModelRepositoryLinq = new CustomerRepositoryLinq(); + customerModelRepositoryLinq.Delete(MySelectedItem); + Customers = customerModelRepositoryLinq.GetAll().ToList(); + } + catch (Exception ex) + { + MessageBox.Show("Error occurred: " + ex.Message); + } + } if (!_EfIsChecked && !_LinqIsChecked) { try { var customerModelRepository = new CustomerRepository(TxtConnectionString); - customerModelRepository.Delete(this.NewCustomerEntry); + customerModelRepository.Delete(MySelectedItem); this.Customers = customerModelRepository.GetAll().ToList(); } catch (Exception ex) @@ -248,15 +305,45 @@ namespace LoggingClient.ViewModel } private void UpdateData() { - try + if (_EfIsChecked) { - var customerModelRepository = new CustomerRepository(TxtConnectionString); - customerModelRepository.Update(this.NewCustomerEntry); - this.Customers = customerModelRepository.GetAll().ToList(); + try + { + var customerModelRepositoryEF = new CustomerRepositoryEF(); + customerModelRepositoryEF.Update(MySelectedItem); + Customers = customerModelRepositoryEF.GetAll(); + } + catch (Exception ex) + { + MessageBox.Show("Error occurred: " + ex.Message); + } } - catch (Exception ex) + if (_LinqIsChecked) { - MessageBox.Show("Error occurred: " + ex.Message); + try + { + var customerModelRepositoryLinq = new CustomerRepositoryLinq(); + customerModelRepositoryLinq.Update(MySelectedItem); + Customers = customerModelRepositoryLinq.GetAll().ToList(); + } + catch (Exception ex) + { + MessageBox.Show("Error occurred: " + ex.Message); + } + } + if (!_EfIsChecked && !_LinqIsChecked) + { + + try + { + var customerModelRepository = new CustomerRepository(TxtConnectionString); + customerModelRepository.Update(MySelectedItem); + this.Customers = customerModelRepository.GetAll().ToList(); + } + catch (Exception ex) + { + MessageBox.Show("Error occurred: " + ex.Message); + } } } diff --git a/LoggingClient/LoggingClient/ViewModel/LocationViewModel.cs b/LoggingClient/LoggingClient/ViewModel/LocationViewModel.cs index d542656..870f35a 100644 --- a/LoggingClient/LoggingClient/ViewModel/LocationViewModel.cs +++ b/LoggingClient/LoggingClient/ViewModel/LocationViewModel.cs @@ -29,15 +29,13 @@ namespace LoggingClient.ViewModel } } private List _locations; - public Location NewLocationModelEntry { get; set; } + public Location SelectedItem { get; set; } public List> LocationTree { get; set; } public LocationViewModel() { TxtConnectionString = "Server=localhost;Database=inventarisierungsloesung;Uid=root;Pwd=MySQLPassword1234!;"; Locations = new List(); - NewLocationModelEntry = new Location(); - } public Location MySelectedItem { get; set; } public event PropertyChangedEventHandler PropertyChanged; @@ -126,7 +124,7 @@ namespace LoggingClient.ViewModel try { var locationModelRepository = new LocationRepository(TxtConnectionString); - locationModelRepository.Add(this.NewLocationModelEntry); + locationModelRepository.Add(SelectedItem); this.Locations = locationModelRepository.GetAll(); } catch (Exception ex) @@ -139,7 +137,7 @@ namespace LoggingClient.ViewModel try { var locationModelRepository = new LocationRepository(TxtConnectionString); - locationModelRepository.Delete(this.NewLocationModelEntry); + locationModelRepository.Delete(SelectedItem); this.Locations = locationModelRepository.GetAll(); } catch (Exception ex) @@ -152,7 +150,7 @@ namespace LoggingClient.ViewModel try { var locationModelRepository = new LocationRepository(TxtConnectionString); - locationModelRepository.Update(this.NewLocationModelEntry); + locationModelRepository.Update(SelectedItem); this.Locations = locationModelRepository.GetAll(); } catch (Exception ex) diff --git a/LoggingClient/LoggingClient/Views/CustomerView.xaml b/LoggingClient/LoggingClient/Views/CustomerView.xaml index dfc9405..da0c89b 100644 --- a/LoggingClient/LoggingClient/Views/CustomerView.xaml +++ b/LoggingClient/LoggingClient/Views/CustomerView.xaml @@ -15,7 +15,7 @@ - + @@ -35,7 +35,7 @@