Ejemplo n.º 1
0
        public Task Handle(UpdateCountryEvent countryEvent)
        {
            var pais = GetCountryFromDb(countryEvent.CountryName, countryEvent.CountryIso2);

            if (countryEvent.CountryName == null || countryEvent.CountryIso2 == null)
            {
                pais = GetCountryFromDbList(countryEvent.CountryName, countryEvent.CountryIso2);
                countryEvent.CountryName = pais.Nome;
                countryEvent.CountryIso2 = pais.SiglaPais2Digitos;
            }

            if (pais == null)
            {
                pais = new Pais();
                pais.SiglaPais2Digitos = countryEvent.CountryIso2;
                pais.Nome = countryEvent.CountryName;
            }

            var isCountryModified = false;
            // TravelBriefingHandler
            var travelBriefingCountry = TravelBriefingHandler.GetCountry(countryEvent.CountryName);

            // Avoid invalid default value
            if (travelBriefingCountry != null && pais.SiglaPais2Digitos == travelBriefingCountry.Names.Iso2)
            {
                isCountryModified = true;
                TravelBriefingHandler.Treat(travelBriefingCountry, pais, out pais);
            }
            var countryIoCountryCapital = CountryIoHandler.GetCountryCapital(countryEvent.CountryIso2);

            if (!string.IsNullOrEmpty(countryIoCountryCapital))
            {
                isCountryModified = true;
                CountryIoHandler.Treat(countryIoCountryCapital, pais, out pais);
            }
            var restCountryInfo = RestCountriesHandler.GetCountryInfo(countryEvent.CountryName);

            if (restCountryInfo != null)
            {
                isCountryModified = true;
                RestCountriesHandler.Treat(restCountryInfo, pais, out pais);
            }

            if (isCountryModified)
            {
                CreateOrUpdateCountry(pais);
            }
            else
            {
                RemovedInvalidCountry(countryEvent.CountryIso2);
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        private async Task SeedCountries()
        {
            var countriesList = _context.ListaPaises.ToList();

            if (countriesList.Count <= 0)
            {
                countriesList = await CountryIoHandler.GetCountriesList();

                foreach (var countryInList in countriesList)
                {
                    _context.ListaPaises.Add(new CountryBase
                    {
                        CountryName = countryInList.CountryName.Trim(),
                        CountryIso2 = countryInList.CountryIso2.Trim()
                    });
                }
                _context.SaveChanges();
            }

            var threadList = new List <Thread>();

            foreach (var countryInList in countriesList)
            {
                if (CheckIfShouldUpdateCountry(countryInList))
                {
                    threadList.Add(new Thread(() => SeedCountry(countryInList)));
                }
            }
            var tList = new Thread(() =>
            {
                foreach (var thread in threadList)
                {
                    thread.Start();
                    // Sleep to avoid DDOS Attack to API
                    Thread.Sleep(DatabaseConfig.SeedCountriesInterval * 1000);
                }
            });

            tList.Start();
        }