-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElection.cs
140 lines (127 loc) · 5.21 KB
/
Election.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace StarVoteServer
{
// This class is used internally by the service and includes some information not shared with client
public class Election : ElectionBallot
{
public string TimeZone { get; set; }
public List<string> AuthorizedVoters { get; set; }
public static Election DefaultValue()
{
var settings = new ElectionSettings
{
StartTime = DateTime.UtcNow,
EndTime = DateTime.UtcNow + new TimeSpan(7, 0, 0, 0, 0),
FaqUrl = "",
AdminEmail = "[email protected]",
SupportEmail = "[email protected]",
AuditEmail = "[email protected]",
EmailVerification = true,
VoterAuthorization = false,
BallotUpdates = false,
PublicResults = true,
RandomizeCandidates = false
};
var races = new List<Race> {
new Race {
Caption = "Race 1",
Candidates = new List<string> { "Candidate 1", "Candidate 2", "Candidate 3" }
},
new Race {
Caption = "Race 2",
Candidates = new List<string> { "A", "B", "C", "D", "E" }
}
};
return new Election
{
Title = "My Election",
Settings = settings,
Races = races,
AuthorizedVoters = null
};
}
public static async Task<ElectionBallot> ReadBallot(GoogleFunctions.GoogleService service)
{
var election = await Election.ReadElection(service).ConfigureAwait(false);
var ballot = new ElectionBallot { Title = election.Title, Races = election.Races, Settings = election.Settings};
return ballot;
}
public static async Task<Election> ReadElection(GoogleFunctions.GoogleService service)
{
// First, validate that we can access the document.
var info = await service.GetSheetInfo().ConfigureAwait(false);
SheetInfo settingsSheet = null;
SheetInfo votersSheet = null;
List<SheetInfo> raceSheets = new List<SheetInfo>();
foreach (var sheet in info.Sheets)
{
// Ignore sheets that don't start with StarSymbol;
if (!sheet.Title.StartsWith(GoogleFunctions.GoogleService.StarSymbol))
continue;
var title = sheet.Title.Substring(1).Trim(); // Get the rest of title after star
if ("Settings".Equals(title, StringComparison.OrdinalIgnoreCase) || sheet.SheetId == 0)
{
settingsSheet = sheet;
}
else if ("Voters".Equals(title, StringComparison.OrdinalIgnoreCase) || sheet.SheetId == 1)
{
votersSheet = sheet;
}
else
{
raceSheets.Add(sheet);
}
}
if (settingsSheet == null)
{
throw new ApplicationException($"The document is missing a {GoogleFunctions.GoogleService.StarSymbol}Settings tab");
}
if (votersSheet == null)
{
throw new ApplicationException($"The document is missing a {GoogleFunctions.GoogleService.StarSymbol}Voters tab");
}
if (raceSheets.Count == 0)
{
throw new ApplicationException($@"The document does not have any races defined.
For each race, there should be a tab with the name of the race preceeded by {GoogleFunctions.GoogleService.StarSymbol}.
For example, ""{GoogleFunctions.GoogleService.StarSymbol}Best Pianist""");
}
var election = await service.GetElection(settingsSheet, votersSheet, raceSheets, info.TimeZone);
election.Title = info.Title;
election.TimeZone = info.TimeZone;
return election;
}
}
// This is the base class containing the fields that may be shared with the client
// describing the election ballot
public class ElectionBallot
{
public string Title { get; set; }
public ElectionSettings Settings { get; set; }
public List<Race> Races { get; set; }
}
public class ElectionSettings
{
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public string FaqUrl { get; set; }
public string AdminEmail { get; set; }
public string SupportEmail { get; set; }
public string AuditEmail { get; set; }
public bool? EmailVerification { get; set; }
public bool? VoterAuthorization { get; set; }
public bool? BallotUpdates { get; set; }
public bool? RandomizeCandidates { get; set; }
public bool? PublicResults { get; set; }
}
public class Race
{
public string Caption { get; set; }
public List<string> Candidates { get; set; }
public int? SheetId { get; set; }
}
}