private void UpdateUserProcess(Official tempOfficial) { Official updatedOfficial = new Official(); Address updatedAddress = new Address(); bool userWasChanged = false; bool addressWasChanged = false; bool addressExistsInDb = false; updatedOfficial.Guid = tempOfficial.Guid; updatedOfficial.Name = NameTextBox.Text; updatedOfficial.SurName = SurnameTextBox.Text; updatedOfficial.Mail = EmailTextBox.Text; updatedOfficial.Phone = PhoneTextBox.Text; updatedOfficial.CompanyNumber = LoginTextBox.Text; if (OfficialSubTypeComboBox.SelectedItem == OfficialSubTypeComboBox_Junior) { updatedOfficial.OfficialType = OfficialType.Junior; } else if (OfficialSubTypeComboBox.SelectedItem == OfficialSubTypeComboBox_Normal) { updatedOfficial.OfficialType = OfficialType.Normal; } else if (OfficialSubTypeComboBox.SelectedItem == OfficialSubTypeComboBox_Senior) { updatedOfficial.OfficialType = OfficialType.Senior; } updatedOfficial.Address = tempOfficial.Address; updatedAddress.Id = tempOfficial.Address.Id; updatedAddress.Street = StreetTextBox.Text; updatedAddress.StreetNumber = StreetNumberTextBox.Text; updatedAddress.City = CityTextBox.Text; updatedAddress.PostalCode = PostalCodeTextBox.Text; updatedAddress.Country = CountryTextBox.Text; Address addressFromDb = UsersORM.IsAddressInDatabase(updatedAddress); if (tempOfficial.Name == updatedOfficial.Name && tempOfficial.SurName == updatedOfficial.SurName && tempOfficial.Phone == updatedOfficial.Phone && tempOfficial.Mail == updatedOfficial.Mail && tempOfficial.CompanyNumber == updatedOfficial.CompanyNumber && tempOfficial.OfficialType == updatedOfficial.OfficialType) { userWasChanged = false; } else { userWasChanged = true; } if (updatedAddress.Street == tempOfficial.Address.Street && updatedAddress.StreetNumber == tempOfficial.Address.StreetNumber && updatedAddress.City == tempOfficial.Address.City && updatedAddress.PostalCode == tempOfficial.Address.PostalCode && updatedAddress.Country == tempOfficial.Address.Country) { addressWasChanged = false; } else { addressWasChanged = true; if (addressFromDb is null) { addressExistsInDb = false; } else { addressExistsInDb = true; } } if (!userWasChanged && !addressWasChanged) { return; } if (addressWasChanged) { if (addressExistsInDb) { updatedOfficial.Address = addressFromDb; } else if (!addressExistsInDb) { updatedAddress.Id = UsersORM.GetNewAddressId(); bool addressIsCreated = UsersORM.CreateAddress(updatedAddress); if (addressIsCreated) { updatedOfficial.Address = updatedAddress; } else { MessageBox.Show("Address was not created. Contact administrator.", "", MessageBoxButton.OK, MessageBoxImage.Error); return; } } } bool result = UsersORM.UpdateOfficial(updatedOfficial); if (result) { MessageBox.Show("Update of user successful.", "", MessageBoxButton.OK, MessageBoxImage.Information); if (tempOfficial.Guid == activeOfficial.Guid) { activeOfficial = updatedOfficial; } tempOfficial = updatedOfficial; } else { MessageBox.Show("Update of customer not successful. Contact administrator.", "", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void CreateNewCustomerInDB_Click(object sender, RoutedEventArgs e) { Customer newCustomer = new Customer(); Address newAddress = new Address(); if (!allUserFieldsAreNotEmpty()) { MessageBox.Show("Please fill in all user information", "", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (!allAddressFieldsAreNotEmpty()) { MessageBox.Show("Please fill in all address fields", "", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (!Validator.Validator.NameValidator(NameTextBox.Text) || !Validator.Validator.NameValidator(SurnameTextBox.Text) || !Validator.Validator.EmailValidator(EmailTextBox.Text) || !Validator.Validator.PhoneValidator(PhoneTextBox.Text) || !Validator.Validator.StreetValidator(StreetTextBox.Text) || !Validator.Validator.StreetNumberValidator(StreetNumberTextBox.Text) || !Validator.Validator.CityValidator(CityTextBox.Text) || !Validator.Validator.PostalCodeValidator(PostalCodeTextBox.Text) || !Validator.Validator.SSNValidator(LoginTextBox.Text) ) { MessageBox.Show("Incorect inputs. Please check user information.", "", MessageBoxButton.OK, MessageBoxImage.Warning); if (!Validator.Validator.NameValidator(NameTextBox.Text)) { NameTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.NameValidator(SurnameTextBox.Text)) { SurnameTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.EmailValidator(EmailTextBox.Text)) { EmailTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.PhoneValidator(PhoneTextBox.Text)) { PhoneTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.StreetValidator(StreetTextBox.Text)) { StreetTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.StreetNumberValidator(StreetNumberTextBox.Text)) { StreetNumberTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.CityValidator(CityTextBox.Text)) { CityTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.PostalCodeValidator(PostalCodeTextBox.Text)) { PostalCodeTextBox_NoIcon.Visibility = Visibility.Visible; } if (!Validator.Validator.SSNValidator(LoginTextBox.Text)) { LoginTextBox_NoIcon.Visibility = Visibility.Visible; } return; } newAddress.Id = UsersORM.GetNewAddressId(); newAddress.Street = StreetTextBox.Text; newAddress.StreetNumber = StreetNumberTextBox.Text; newAddress.City = CityTextBox.Text; newAddress.PostalCode = PostalCodeTextBox.Text; newAddress.Country = CountryTextBox.Text; bool addressIsCreated = UsersORM.CreateAddress(newAddress); if (!addressIsCreated) { MessageBox.Show("New address was not created. Contact administrator", "", MessageBoxButton.OK, MessageBoxImage.Error); } newCustomer.Guid = Guid.NewGuid(); newCustomer.Name = NameTextBox.Text; newCustomer.SurName = SurnameTextBox.Text; newCustomer.Address = newAddress; newCustomer.Mail = EmailTextBox.Text; newCustomer.Phone = PhoneTextBox.Text; newCustomer.Valid = true; newCustomer.Password = "******"; newCustomer.SSN = LoginTextBox.Text; if (CustomerSubTypeComboBox.SelectedItem == CustomerSubTypeComboBox_Person) { newCustomer.CustomerType = CustomerType.Person; } else if (CustomerSubTypeComboBox.SelectedItem == CustomerSubTypeComboBox_Company) { newCustomer.CustomerType = CustomerType.Company; } bool userIsCreated = UsersORM.CreateNewCustomer(newCustomer); if (userIsCreated) { MessageBox.Show(String.Format("New User was created: {0} {1}", newCustomer.Name, newCustomer.SurName), "", MessageBoxButton.OK, MessageBoxImage.Information); OpenDetailViewOfUser(newCustomer); } else { MessageBox.Show("New user was not created. Contact administrator", "", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void UpdateUserProcess(Customer tempCustomer) { Customer updatedCustomer = new Customer(); Address updatedAddress = new Address(); bool userWasChanged = false; bool addressWasChanged = false; bool addressExistsInDb = false; updatedCustomer.Guid = tempCustomer.Guid; updatedCustomer.Name = NameTextBox.Text; updatedCustomer.SurName = SurnameTextBox.Text; updatedCustomer.Mail = EmailTextBox.Text; updatedCustomer.Phone = PhoneTextBox.Text; updatedCustomer.SSN = LoginTextBox.Text; if (CustomerSubTypeComboBox.SelectedItem == CustomerSubTypeComboBox_Person) { updatedCustomer.CustomerType = CustomerType.Person; } else if (CustomerSubTypeComboBox.SelectedItem == CustomerSubTypeComboBox_Company) { updatedCustomer.CustomerType = CustomerType.Company; } updatedCustomer.Address = tempCustomer.Address; updatedAddress.Id = tempCustomer.Address.Id; updatedAddress.Street = StreetTextBox.Text; updatedAddress.StreetNumber = StreetNumberTextBox.Text; updatedAddress.City = CityTextBox.Text; updatedAddress.PostalCode = PostalCodeTextBox.Text; updatedAddress.Country = CountryTextBox.Text; Address addressFromDb = UsersORM.IsAddressInDatabase(updatedAddress); if (tempCustomer.Name == updatedCustomer.Name && tempCustomer.SurName == updatedCustomer.SurName && tempCustomer.Phone == updatedCustomer.Phone && tempCustomer.Mail == updatedCustomer.Mail && tempCustomer.SSN == updatedCustomer.SSN && tempCustomer.CustomerType == updatedCustomer.CustomerType) { userWasChanged = false; } else { userWasChanged = true; } if (updatedAddress.Street == tempCustomer.Address.Street && updatedAddress.StreetNumber == tempCustomer.Address.StreetNumber && updatedAddress.City == tempCustomer.Address.City && updatedAddress.PostalCode == tempCustomer.Address.PostalCode && updatedAddress.Country == tempCustomer.Address.Country) { addressWasChanged = false; } else { addressWasChanged = true; if (addressFromDb is null) { addressExistsInDb = false; } else { addressExistsInDb = true; } } if (!userWasChanged && !addressWasChanged) { return; } if (addressWasChanged) { if (addressExistsInDb) { updatedCustomer.Address = addressFromDb; } else if (!addressExistsInDb) { updatedAddress.Id = UsersORM.GetNewAddressId(); bool addressIsCreated = UsersORM.CreateAddress(updatedAddress); if (addressIsCreated) { updatedCustomer.Address = updatedAddress; } else { MessageBox.Show("Address was not created. Contact administrator.", "", MessageBoxButton.OK, MessageBoxImage.Error); return; } } } bool result = UsersORM.UpdateCustomer(updatedCustomer); if (result) { MessageBox.Show("Update successful.", "", MessageBoxButton.OK, MessageBoxImage.Information); //try //{ // if (tempCustomer.Guid == activeCustomer.Guid) // activeCustomer = updatedCustomer; //} //catch (Exception ex) //{ //} tempCustomer = updatedCustomer; } else { MessageBox.Show("Update of customer not successful. Contact administrator.", "", MessageBoxButton.OK, MessageBoxImage.Error); } }