Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }