private Country BuildCountry(DateTime timestamp) { Country country = new Country(Countries.GetNextID()); country.Name = CountryField.Text; country.CreateDate = timestamp; country.CreateBy = Users.CurrentUser; country.UpdateTimestamp = timestamp; country.UpdateBy = Users.CurrentUser; return(country); }
private void OkButton_Click(object sender, EventArgs e) { DateTime timestamp = DateTime.Now; if (Countries.SearchBy(c => c.Name == CountryField.Text)) { Country country = Countries.GetCountryBy(c => c.Name == CountryField.Text); if (Cities.SearchBy(c => c.Name == CityField.Text && c.Country.ID == country.ID)) { City city = Cities.GetCityBy(c => c.Name == CityField.Text && c.Country.ID == country.ID);; if (Addresses.SearchBy(a => a.Address1 == Address1Field.Text && a.Address2 == Address2Field.Text && a.PostalCode == PostalCodeField.Text && a.Phone == PhoneField.Text && a.City.ID == city.ID)) { _customer.Address = Addresses.GetAddressBy(a => a.Address1 == Address1Field.Text && a.Address2 == Address2Field.Text && a.PostalCode == PostalCodeField.Text && a.Phone == PhoneField.Text && a.City.ID == city.ID); } else { Address address = BuildAddress(timestamp); address.City = city; address.City.Country = country; Addresses.Insert(address); _customer.Address = address; } _customer.CityName = city.Name; _customer.CountryName = country.Name; } else { Address address = BuildAddress(timestamp); address.City = BuildCity(timestamp); address.City.Country = country; Cities.Insert(address.City); Addresses.Insert(address); _customer.Address = address; _customer.CityName = address.City.Name; } _customer.CountryName = country.Name; } else { Address address = BuildAddress(timestamp); address.City = BuildCity(timestamp); address.City.Country = BuildCountry(timestamp); Countries.Insert(address.City.Country); Cities.Insert(address.City); Addresses.Insert(address); _customer.Address = address; _customer.CityName = address.City.Name; _customer.CountryName = address.City.Country.Name; } if (_isUpdate) { _customer.Name = CustomerNameField.Text; _customer.Active = ActiveBox.Checked; _customer.UpdateTimestamp = timestamp; _customer.UpdateBy = Users.CurrentUser; Customers.Update(_customer); } else { Customer newCustomer = new Customer(Countries.GetNextID()); newCustomer.Name = CustomerNameField.Text; newCustomer.Address = _customer.Address; newCustomer.Active = ActiveBox.Checked; newCustomer.CreateDate = timestamp; newCustomer.CreateBy = Users.CurrentUser; newCustomer.UpdateTimestamp = timestamp; newCustomer.UpdateBy = Users.CurrentUser; Customers.Insert(newCustomer); newCustomer.CityName = _customer.Address.City.Name; newCustomer.CountryName = _customer.Address.City.Country.Name; } Close(); }