Ejemplo n.º 1
0
        public async Task <IList <Country> > GetCountriesAsync()
        {
            if (_options == null)
            {
                throw new StorefrontException("the path to countries json file not set");
            }

            var cacheKey = CacheKey.With(GetType(), "GetCountries");

            return(await _memoryCache.GetOrCreateAsync(cacheKey, async (cacheEntry) =>
            {
                var result = new List <Country>();

                if (_options != null)
                {
                    var countriesJson = await File.ReadAllTextAsync(_options.FilePath);
                    var countriesDict = JsonConvert.DeserializeObject <Dictionary <string, JObject> >(countriesJson);

                    result = countriesDict
                             .Select(ParseCountry)
                             .Where(c => !string.IsNullOrEmpty(c.Code3))
                             .ToList();
                }
                return result;
            }));
        }
Ejemplo n.º 2
0
        public async Task <IList <Country> > GetCountriesAsync()
        {
            if (_options == null)
            {
                throw new StorefrontException("the path to countries json file not set");
            }

            var cacheKey = CacheKey.With(GetType(), "GetCountries");

            return(await _memoryCache.GetOrCreateAsync(cacheKey, async (cacheEntry) =>
            {
                List <Country> result = new List <Country>();

                var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                              .Select(GetRegionInfo)
                              .Where(r => r != null)
                              .ToList();

                if (_options != null)
                {
                    var countriesJson = await File.ReadAllTextAsync(_options.FilePath);
                    var countriesDict = JsonConvert.DeserializeObject <Dictionary <string, JObject> >(countriesJson);

                    result = countriesDict
                             .Select(kvp => ParseCountry(kvp, regions))
                             .Where(c => c.Code3 != null)
                             .ToList();
                }
                return result;
            }));
        }