private Address GetCompleteAddressforValidatable(Address addressToSanitize) { State state; if (addressToSanitize.StateId < 1) { if (!string.IsNullOrEmpty(addressToSanitize.State)) { state = _stateRepository.GetState(addressToSanitize.State); } else { state = _stateRepository.GetStatebyCode(addressToSanitize.StateCode); } addressToSanitize.StateId = state.Id; } else { state = _stateRepository.GetState(addressToSanitize.StateId); } if (addressToSanitize.CityId < 1) { var city = _cityRepository.GetCityByStateAndName(addressToSanitize.StateId, addressToSanitize.City); if (city == null) { throw new InvalidAddressException(state.Name, addressToSanitize.City, addressToSanitize.ZipCode.Zip); } addressToSanitize.CityId = city.Id; } if (addressToSanitize.ZipCode.Id < 1) { var zip = _zipCodeRepository.GetZipCodeForCity(addressToSanitize.CityId).Where(z => z.Zip == addressToSanitize.ZipCode.Zip).SingleOrDefault(); if (zip != null) { addressToSanitize.ZipCode.Id = zip.Id; } else { throw new InvalidAddressException(state.Name, addressToSanitize.City, addressToSanitize.ZipCode.Zip); } } return(addressToSanitize); }
public State GetState(string stateName, string cityName, string zipCode) { try { var state = _stateRepository.GetState(stateName); if (state != null) { // TODO: Ideally it should not be the case, there can be a single city with given name for a given state. // But due to the data problem we are returning list of cities. var cities = _cityRepository.GetCityForState(state.Id, cityName); if (cities != null && !cities.IsEmpty()) { var city = cities.Where( c => c.ZipCodes != null && !c.ZipCodes.IsEmpty() && c.ZipCodes.Select(zc => zc.Zip).Contains(zipCode)).FirstOrDefault(); if (city != null) { var zip = _zipCodeRepository.GetZipCodeForCity(city.Id).Where(z => z.Zip == zipCode).SingleOrDefault(); if (zip != null) { city.ZipCodes = new List <ZipCode> { zip }; state.Cities = new List <City> { city }; return(state); } } } } } // TODO: This is done because there is problems with the data in the system, // We dont always get right data, so for now consuming it. // TODO: We really need to handle this state, city,zip combination uniquness more elegantly. catch { } return(null); }