Exemplo n.º 1
0
        public string RefreshElections(string electionYear, string stateCode)
        {
            var jsonObj = VoteSmart.GetJsonAsDictionary("Election.getElectionByYearState",
                                                        "year=" + electionYear + "&stateId=" + stateCode);

            if (!jsonObj.ContainsKey("elections"))
            {
                return("No elections found in " + electionYear + " for " +
                       StateCache.GetStateName(stateCode));
            }

            VsElections.DeleteByElectionYearStateCode(electionYear, stateCode);

            var elections = jsonObj["elections"] as Dictionary <string, object>;

            Debug.Assert(elections != null, "elections != null");
            var election = VoteSmart.AsArrayList(elections["election"]);

            foreach (var e in election.Cast <Dictionary <string, object> >())
            {
                var electionId = Convert.ToInt32(e["electionId"]);
                var stage      = VoteSmart.AsArrayList(e["stage"]);
                foreach (var s in stage.Cast <Dictionary <string, object> >())
                {
                    var stageId = s["stageId"] as string;
                    VsElectionsCandidates.DeleteByElectionIdStageId(electionId, stageId);
                    VsElections.Insert(electionId, stageId, electionYear,
                                       stateCode, e["officeTypeId"] as string, e["special"] as string,
                                       e["name"] as string, s["name"] as string,
                                       DateTime.Parse(s["electionDate"] as string),
                                       VotePage.DefaultDbDate);
                }
            }

            if (VsElectionYearState.ElectionYearStateCodeExists(electionYear, stateCode))
            {
                VsElectionYearState.UpdateLastRefreshTime(DateTime.UtcNow, electionYear,
                                                          stateCode);
            }
            else
            {
                VsElectionYearState.Insert(electionYear, stateCode, DateTime.UtcNow);
            }

            return(string.Empty);
        }
Exemplo n.º 2
0
        public ElectionsInfo GetElections(string electionYear, string stateCode)
        {
            var info = new ElectionsInfo
            {
                LastRefreshDate =
                    VsElectionYearState.GetLastRefreshTime(electionYear, stateCode,
                                                           VotePage.DefaultDbDate).AsUtc(),
                Elections = Enumerable.Select(VsElections.GetDataByElectionYearStateCode(electionYear,
                                                                                         stateCode)
                                              .OrderBy(r => r.StageDate)
                                              .ThenBy(r => r.StageId), r => new SimpleListItem(r.ElectionId + r.StageId,
                                                                                               $"{r.ElectionName} - {r.StageName} ({r.StageDate.ToShortDateString()})"))
                            .ToList()
            };

            return(info);
        }
Exemplo n.º 3
0
        public CandidatesInfo GetCandidates(string vsElectionKey)
        {
            var electionId =
                int.Parse(vsElectionKey.Substring(0, vsElectionKey.Length - 1));
            var stageId = vsElectionKey.Substring(vsElectionKey.Length - 1, 1);
            var info    = new CandidatesInfo
            {
                LastRefreshDate =
                    VsElections.GetCandidateListRefreshTime(electionId, stageId,
                                                            VotePage.DefaultDbDate).AsUtc(),
                Candidates = Enumerable.Select(VsElectionsCandidatesView.GetData(electionId, stageId),
                                               // ReSharper disable once PossibleInvalidOperationException
                                               r => new SimpleListItem(r.CandidateId.Value.ToString(CultureInfo.InvariantCulture),
                                                                       FormatCandidateName(r)))
                             .OrderBy(i => i.Text)
                             .ToList()
            };

            return(info);
        }
Exemplo n.º 4
0
        public string RefreshCandidates(string vsElectionKey)
        {
            var electionId =
                int.Parse(vsElectionKey.Substring(0, vsElectionKey.Length - 1));
            var stageId = vsElectionKey.Substring(vsElectionKey.Length - 1, 1);

            VsElectionsCandidates.DeleteByElectionIdStageId(electionId, stageId);
            VsElections.UpdateCandidateListRefreshTime(DateTime.UtcNow, electionId, stageId);

            var jsonObj = VoteSmart.GetJsonAsDictionary("Candidates.getByElection",
                                                        "electionId=" + electionId + "&stageId=" + stageId);

            if (jsonObj.ContainsKey("candidateList"))
            {
                var candidateList = jsonObj["candidateList"] as Dictionary <string, object>;
                // ReSharper disable once PossibleNullReferenceException
                var candidateArray = VoteSmart.AsArrayList(candidateList["candidate"]);
                foreach (
                    var candidate in candidateArray.OfType <Dictionary <string, object> >())
                {
                    try
                    {
                        VsElectionsCandidates.Insert(electionId, stageId,
                                                     Convert.ToInt32(candidate["candidateId"]),
                                                     VsDecode(candidate["ballotName"]), candidate["lastName"] as string,
                                                     candidate["firstName"] as string);
                    }
                    catch (MySqlException e)
                    {
                        if (!e.Message.StartsWith("Duplicate entry",
                                                  StringComparison.OrdinalIgnoreCase))
                        {
                            throw;
                        }
                    }
                }
            }

            return(string.Empty);
        }