public ActionResult AddCountry(CountryViewModel viewModel)
        {
            var country = new CountryRecord
            {
                Code = viewModel.Code,
                Name = viewModel.Name
            };

            if (viewModel.SelectedCultures != null)
            {
                foreach (var cultureId in viewModel.SelectedCultures)
                {
                    var culture = _cultureRepository.Get(cultureId);

                    var countryCulture = new LinkCountryCultureRecord
                    {
                        CountryRecord = country,
                        CultureRecord = culture
                    };

                    country.CountryCultures.Add(countryCulture);
                }
            }

            var defaultCulture = _cultureRepository.Get(viewModel.DefaultCultureId);

            var defaultCountryCulture = country.CountryCultures
                                        .FirstOrDefault(c => c.CultureRecord == defaultCulture);

            if (defaultCountryCulture == null)
            {
                defaultCountryCulture = new LinkCountryCultureRecord
                {
                    CountryRecord = country,
                    CultureRecord = defaultCulture
                };

                country.CountryCultures.Add(defaultCountryCulture);
            }

            country.DefaultCulture = defaultCountryCulture;

            _countryRepository.Create(country);

            _orchardServices.Notifier.Information(T("Country has been added."));
            return(RedirectToAction("Index"));
        }
        public Task UpdateGraphData(SourceData data, CancellationToken stoppingToken)
        {
            var countries  = new List <Country>();
            var sourceData = JsonConvert.DeserializeObject <SourceData>(sourceDataJson);
            var geoIds     = sourceData.Records.Select(o => o.GeoId.ToLowerInvariant()).Distinct();

            foreach (var geoId in geoIds)
            {
                var firstMatch = sourceData.Records.FirstOrDefault(o => o.GeoId.Equals(geoId, StringComparison.InvariantCultureIgnoreCase));
                var country    = new Country
                {
                    GeoId                   = firstMatch.GeoId,
                    CasesBucket             = buckets.FirstOrDefault(oo => oo.Name == firstMatch.CasesBucket),
                    DeathsBucket            = buckets.FirstOrDefault(oo => oo.Name == firstMatch.DeathsBucket),
                    CountriesAndTerritories = firstMatch.CountriesAndTerritories,
                    FocusCountry            = firstMatch.FocusCountry,
                    PopData2018             = firstMatch.PopData2018?.Length > 0 ? Convert.ToInt32(firstMatch.PopData2018) : 0,
                };
                countries.Add(country);
                var records = sourceData.Records.Where(o => o.GeoId.Equals(firstMatch.GeoId, StringComparison.InvariantCultureIgnoreCase));
                foreach (var record in records)
                {
                    var countryRecord = new CountryRecord
                    {
                        GeoId          = firstMatch.GeoId,
                        Date           = record.Date,
                        Day            = Convert.ToInt32(record.Day),
                        Month          = Convert.ToInt32(record.Month),
                        Year           = Convert.ToInt32(record.Year),
                        Cases          = record.CasesNumber,
                        Deaths         = record.DeathsNumber,
                        CasesToDate    = record.CasesToDate,
                        DeathsToDate   = record.DeathsToDate,
                        DaysWithCases  = record.DaysWithCases,
                        DaysWithDeaths = record.DaysWithDeaths,
                    };
                    country.Records.Add(countryRecord);
                }
            }
            this.countries = countries;
            return(Task.CompletedTask);
        }
Example #3
0
    public static void ParseJSONIntoObjects()
    {
        if (CovidJSONRawData != null)
        {
            JObject       json = JObject.Parse(CovidJSONRawData);
            List <JToken> data = json.Children().ToList();

            foreach (JProperty item in data)
            {
                item.CreateReader();
                switch (item.Name)
                {
                case "records":
                    List <JToken> issues = item.Children().Children().ToList();
                    foreach (JObject issue in issues)
                    {
                        CountryRecord newRecord = new CountryRecord();
                        issue.CreateReader();
                        List <JToken> values = issue.Children().ToList();

                        string countryName = "";
                        string geoId       = "";
                        long   population  = -1;

                        foreach (JProperty value in values)
                        {
                            value.CreateReader();
                            switch (value.Name)
                            {
                            case "countryterritoryCode":
                                newRecord.CountryCode = value.Value.ToString();
                                break;

                            case "day":
                                newRecord.Day = (int)value.Value;
                                break;

                            case "month":
                                newRecord.Month = (int)value.Value;
                                break;

                            case "year":
                                newRecord.Year = (int)value.Value;
                                break;

                            case "cases":
                                newRecord.Cases = (int)value.Value;
                                break;

                            case "deaths":
                                newRecord.Deaths = (int)value.Value;
                                break;

                            case "countriesAndTerritories":
                                countryName = value.Value.ToString();
                                break;

                            case "geoId":
                                geoId = value.Value.ToString();
                                break;

                            default:
                                if (value.Name.Substring(0, 7).Equals("popData"))
                                {
                                    if (!value.Value.ToString().Equals(""))
                                    {
                                        population = (long)value.Value;
                                    }
                                }
                                break;
                            }
                        }
                        Country tempCountry;

                        int index = countries.FindIndex(x => x.CountryCode == newRecord.CountryCode);

                        if (index >= 0)
                        {
                            tempCountry = countries.ElementAt(index);
                            tempCountry.CountryRecords.Add(newRecord);
                        }
                        else
                        {
                            tempCountry = new Country
                            {
                                CountryCode = newRecord.CountryCode,
                                Name        = countryName.Replace(underscore, space),
                                GeoID       = geoId,
                                Population  = population
                            };
                            tempCountry.CountryRecords.Add(newRecord);
                            countries.Add(tempCountry);
                        }
                        countryRecords.Add(newRecord);
                    }
                    break;
                }
            }
        }
    }