Example #1
0
        public UpdateCountriesResult UpdateCountries(List <Country> newCountries)
        {
            List <Country> existedCountries = _countriesRepository.GetAll().ToList();

            UpdateCountriesResult result = new UpdateCountriesResult
            {
                Updated = existedCountries.Intersect(newCountries, new KeyEqualityComparer <Country>(o => o.EnglishName)).ToList(),
                Created = newCountries.Except(existedCountries, new KeyEqualityComparer <Country>(o => o.EnglishName)).ToList(),
                Deleted = existedCountries.Except(newCountries, new KeyEqualityComparer <Country>(o => o.EnglishName)).ToList()
            };

            result.Updated.ForEach(item =>
            {
                Country source = newCountries.Single(country => country.EnglishName == item.EnglishName);

                item.EnglishName    = source.EnglishName;
                item.ISO            = source.ISO;
                item.PhoneCode      = source.PhoneCode;
                item.ThreeCharsCode = source.ThreeCharsCode;
                item.TwoCharsCode   = source.TwoCharsCode;

                _countriesRepository.Update(item);
            });
            result.Created.ForEach(item => _countriesRepository.Add(item));
            result.Deleted.ForEach(item => _countriesRepository.Delete(item));

            return(result);
        }
Example #2
0
        public async Task SaveCountryAsync(CountryViewModel countryViewModel)
        {
            var capitalId = await SaveCapitalAsync(countryViewModel.Capital).ConfigureAwait(false);

            var regionId = await SaveRegionAsync(countryViewModel.Region).ConfigureAwait(false);

            using (var context = new CountriesContext())
            {
                var savedCountry = await countriesRepository.GetAll(context)
                                   .FirstOrDefaultAsync(country => country.NumericCode == countryViewModel.NumericCode)
                                   .ConfigureAwait(false);

                if (savedCountry == null)
                {
                    await countriesRepository
                    .AddAsync(context, MapViewModelToCountry(countryViewModel, capitalId, regionId))
                    .ConfigureAwait(false);
                }
                else
                {
                    await countriesRepository
                    .UpdateAsync(context, savedCountry.Id,
                                 country => MapViewModelToCountry(countryViewModel, capitalId, regionId))
                    .ConfigureAwait(false);
                }
            }
        }
        private IQueryable <Data.Entities.Country> DbQuery()
        {
            var cachedResponse = _responseCacheRepository.GetResposeAsync(_cackeKey).Result;

            if (string.IsNullOrEmpty(cachedResponse))
            {
                var dbCountries = _countriesRepository.GetAll();
                _responseCacheRepository.SetCacheAsync(_cackeKey, dbCountries, null);
                return(dbCountries.AsQueryable());
            }

            var cacheResult = JsonConvert.DeserializeObject <List <Data.Entities.Country> >(cachedResponse);

            return(cacheResult.AsQueryable());
        }
Example #4
0
 public async Task <List <Country> > GetAll()
 {
     return(await _countriesRepo.GetAll());
 }
        public async Task <IEnumerable <CountryModel> > GetCountries()
        {
            var countries = await _countryRepository.GetAll();

            return(_mapper.Map <IEnumerable <CountryModel> >(countries));
        }