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 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); } }