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); }
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); }