public Location Get( string street1, string street2, string city, string state, string postalCode, string country, bool verifyLocation = true )
{
// Make sure it's not an empty address
if ( string.IsNullOrWhiteSpace( street1 ) )
{
return null;
}
// First check if a location exists with the entered values
Location existingLocation = Queryable().FirstOrDefault( t =>
( t.Street1 == street1 || ( ( street1 == null || street1 == "" ) && ( t.Street1 == null || t.Street1 == "" ) ) ) &&
( t.Street2 == street2 || ( ( street2 == null || street2 == "" ) && ( t.Street2 == null || t.Street2 == "" ) ) ) &&
( t.City == city || ( ( city == null || city == "" ) && ( t.City == null || t.City == "" ) ) ) &&
( t.State == state || ( ( state == null || state == "" ) && ( t.State == null || t.State == "" ) ) ) &&
( t.PostalCode == postalCode || ( ( postalCode == null || postalCode == "" ) && ( t.PostalCode == null || t.PostalCode == "" ) ) ) &&
( t.Country == country || ( ( country == null || country == "" ) && ( t.Country == null || t.Country == "" ) ) ) );
if ( existingLocation != null )
{
return existingLocation;
}
// If existing location wasn't found with entered values, try standardizing the values, and
// search for an existing value again
var newLocation = new Location
{
Street1 = street1.FixCase(),
Street2 = street2.FixCase(),
City = city.FixCase(),
State = state,
PostalCode = postalCode,
Country = country
};
if ( verifyLocation )
{
Verify( newLocation, false );
}
existingLocation = Queryable().FirstOrDefault( t =>
( t.Street1 == newLocation.Street1 || ( ( newLocation.Street1 == null || newLocation.Street1 == "" ) && ( t.Street1 == null || t.Street1 == "" ) ) ) &&
( t.Street2 == newLocation.Street2 || ( ( newLocation.Street2 == null || newLocation.Street2 == "" ) && ( t.Street2 == null || t.Street2 == "" ) ) ) &&
( t.City == newLocation.City || ( ( newLocation.City == null || newLocation.City == "" ) && ( t.City == null || t.City == "" ) ) ) &&
( t.State == newLocation.State || ( ( newLocation.State == null || newLocation.State == "" ) && ( t.State == null || t.State == "" ) ) ) &&
( t.PostalCode == newLocation.PostalCode || ( ( newLocation.PostalCode == null || newLocation.PostalCode == "" ) && ( t.PostalCode == null || t.PostalCode == "" ) ) ) &&
( t.Country == newLocation.Country || ( ( newLocation.Country == null || newLocation.Country == "" ) && ( t.Country == null || t.Country == "" ) ) ) );
if ( existingLocation != null )
{
return existingLocation;
}
// Create a new context/service so that save does not affect calling method's context
var rockContext = new RockContext();
var locationService = new LocationService( rockContext );
locationService.Add( newLocation );
rockContext.SaveChanges();
// refetch it from the database to make sure we get a valid .Id
return Get( newLocation.Guid );
}