Example #1
0
        public void InsertAllTimeZones(string locationOfJson)
        {
            SyncTimeZones();

            jsonCountry = ReadJson(locationOfJson);
            var jArray = JArray.Parse(jsonCountry);

            allCountries = jArray.ToList();
            int len = allCountries.Count;



            for (int index = 0; index < len; index++)
            {
                var alpha2Code = GetSafeString(index, "alpha2Code");
                var countryID  = db.Countries.FirstOrDefault(n => n.Alpha2Code == alpha2Code).CountryID;

                string property = "timezones";
                if (allCountries[index][property].HasValues)
                {
                    int len2 = allCountries[index][property].Count();

                    for (int i = 0; i < len2; i++)
                    {
                        var timeZoneRel = new CountryTimezoneRelation();
                        var utc         = GetTimeZone(index, i);
                        var utcID       = GetUserTimeZoneID(utc);

                        if (utcID != -1)
                        {
                            timeZoneRel.CountryID      = countryID;
                            timeZoneRel.UserTimeZoneID = utcID;
                            db.CountryTimezoneRelations.Add(timeZoneRel);
                        }
                    }
                }
            }
            int iError = db.SaveChanges(null, "TimeZoneSave");

            if (iError == -1)
            {
                Console.WriteLine("Error. ");
            }

            Console.WriteLine("Done Saving Time Zone Database adding.");
        }
Example #2
0
        public ActionResult Edit(int countryId, int timezone, bool hasMultiple)
        {
            var country = _db.Countries.Find(countryId);

            var foundTimeZone = _db.UserTimeZones.Find(timezone);

            if (foundTimeZone != null)
            {
                var addRelation = new CountryTimezoneRelation {
                    CountryID      = country.CountryID,
                    UserTimeZoneID = foundTimeZone.UserTimeZoneID
                };
                var anyExist =
                    _db.CountryTimezoneRelations.Any(
                        n => n.UserTimeZoneID == addRelation.UserTimeZoneID && n.CountryID == addRelation.CountryID);

                if (!anyExist)
                {
                    //not exist then add
                    _db.CountryTimezoneRelations.Add(addRelation);
                    country.RelatedTimeZoneID = addRelation.UserTimeZoneID;
                }

                country.IsSingleTimeZone  = !hasMultiple;
                country.RelatedTimeZoneID = addRelation.UserTimeZoneID;
                _db.SaveChanges();
            }
            var zones = CachedQueriedData.GetTimezones(countryId);

            ViewBag.Timezone    = new SelectList(_db.UserTimeZones.ToList(), "UserTimeZoneID", "Display");
            ViewBag.CountryID   = countryId;
            ViewBag.CountryName = _db.Countries.Find(countryId).DisplayCountryName + " - " +
                                  _db.Countries.Find(countryId).Alpha2Code;

            return(View(zones));
        }