Example #1
0
        public async Task <SaveLocationReturnValues> SaveCityStateCountry(ChauffeurRoute routeDetail, ValueRetailVillage village)
        {
            var locationData = await _googleMapApi.GetLatLongFromAddress(routeDetail.LocationHeader);

            try
            {
                if (!locationData.Success)
                {
                    throw new ArgumentNullException(locationData.Result.CityName, "Failed to get Location Data");
                }

                var countryCodeAndCurrency = await _countryAlphaCode.GetCountryCodeByName(locationData.Result.CountryName);

                var country = _countryRepository.GetByName(locationData.Result.CountryName);
                if (country == null)
                {
                    country = _countryRepository.Save(new Country
                    {
                        Name              = locationData.Result.CountryName,
                        IsoAlphaTwoCode   = countryCodeAndCurrency.Result.IsoAlphaTwoCode.ToUpper(),
                        IsoAlphaThreeCode = countryCodeAndCurrency.Result.IsoAlphaThreeCode.ToUpper(),
                        IsEnabled         = true,
                    });
                }

                var state = _stateRepository.GetByNameAndCountryId(locationData.Result.StateName, country.Id);
                if (state == null)
                {
                    state = _stateRepository.Save(new State
                    {
                        Name      = locationData.Result.StateName,
                        CountryId = country.Id,
                        IsEnabled = true,
                    });
                }

                var city = _cityRepository.GetByNameAndStateId(locationData.Result.CityName, state.Id);
                if (city == null)
                {
                    city = _cityRepository.Save(new City
                    {
                        Name      = locationData.Result.CityName,
                        StateId   = state.Id,
                        IsEnabled = true,
                    });
                }

                var currencyType = _currencyTypeRepository.GetByCurrencyCode(village.CurrencyCode.ToUpper());
                if (currencyType == null)
                {
                    currencyType = _currencyTypeRepository.Save(new CurrencyType
                    {
                        Code       = village.CurrencyCode.ToUpper(),
                        Name       = village.CurrencyCode.ToUpper(),
                        CountryId  = country.Id,
                        ModifiedBy = Guid.NewGuid(),
                        IsEnabled  = true
                    });
                }
                var values = new SaveLocationReturnValues
                {
                    cityId     = city.Id,
                    currencyId = currencyType.Id,
                    lat        = locationData.Result.lat,
                    lng        = locationData.Result.lng
                };

                return(values);
            }
            catch (Exception ex)
            {
                _logger.Log(LogCategory.Error, ex);
                return(new SaveLocationReturnValues());
            }
        }