/// <summary> /// Gets work address from API Address. /// </summary> /// <param name="org"></param> /// <returns>WorkAddress</returns> private WorkAddress GetWorkAddress(APIOrgUnit apiOrgunit) { var launderer = new CachedAddressLaunderer(_cachedRepo, _actualLaunderer, _coordinates); if (apiOrgunit.Address?.Street == null) { return(null); } var splitStreetAddress = SplitAddressOnNumber(apiOrgunit.Address.Street); var addressToLaunder = new Address { StreetName = splitStreetAddress.ElementAt(0), StreetNumber = splitStreetAddress.Count > 1 ? splitStreetAddress.ElementAt(1) : "1", ZipCode = apiOrgunit.Address.PostalCode, Town = apiOrgunit.Address.City, Description = apiOrgunit.Name }; addressToLaunder = launderer.Launder(addressToLaunder); var launderedAddress = new WorkAddress() { StreetName = addressToLaunder.StreetName, StreetNumber = addressToLaunder.StreetNumber, ZipCode = addressToLaunder.ZipCode, Town = addressToLaunder.Town, Latitude = addressToLaunder.Latitude ?? "", Longitude = addressToLaunder.Longitude ?? "", Description = apiOrgunit.Name }; var existingOrg = _orgUnitRepo.AsNoTracking().FirstOrDefault(x => x.OrgId.Equals(apiOrgunit.Id)); // If the address hasn't changed then set the Id to be the same as the existing one. // That way a new address won't be created in the database. // If the address is not the same as the existing one, // Then the Id will be 0, and a new address will be created in the database. if (existingOrg != null && existingOrg.Address != null && existingOrg.Address.StreetName == launderedAddress.StreetName && existingOrg.Address.StreetNumber == launderedAddress.StreetNumber && existingOrg.Address.ZipCode == launderedAddress.ZipCode && existingOrg.Address.Town == launderedAddress.Town && existingOrg.Address.Latitude == launderedAddress.Latitude && existingOrg.Address.Longitude == launderedAddress.Longitude && existingOrg.Address.Description == launderedAddress.Description) { launderedAddress.Id = existingOrg.AddressId; } return(launderedAddress); }
private OrgUnit mapAPIOrgUnit(APIOrgUnit apiOrgUnit, ref OrgUnit orgUnit) { orgUnit.OrgId = int.Parse(apiOrgUnit.Id); if (!String.IsNullOrEmpty(apiOrgUnit.ParentId)) { orgUnit.ParentId = _orgUnitRepo.AsNoTracking().Where(o => o.OrgId.ToString() == apiOrgUnit.ParentId).Select(o => o.Id).First(); } orgUnit.ShortDescription = apiOrgUnit.Name; orgUnit.LongDescription = apiOrgUnit.Name; var workAddress = GetWorkAddress(apiOrgUnit); if (workAddress != orgUnit.Address) { orgUnit.Address = workAddress; } return(orgUnit); }