public async Task <IActionResult> CloseElection([FromBody] UpdateElection election)
        {
            await _electionService.CloseElectionAsync(election.Id);

            return(Ok());
        }
示例#2
0
        private async void btnOpenOrCloseElection_Click(object sender, EventArgs e)
        {
            if (_canOpenNewElection)
            {
                this.Opacity = 0.5;

                var form = new ElectionForm
                {
                    Owner = this
                };
                form.Tag = this.Tag;
                form.ShowDialog();

                if (!form.IsCancelled)
                {
                    await LoadElectionAsync();

                    await _window.LoadElectionAsync();

                    await _window.LoadCandidatesAsync();

                    await _window.LoadResultsAsync();

                    await _window.LoadVotersAsync();
                }

                this.Opacity = 1;
            }
            else
            {
                if (_currentElection.TookPlaceOn > DateTime.Today)
                {
                    MessageBox.Show("Cannot close a future election", "Future election", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                var votersCount = await _voterService.CountVotersAsync(_currentElection.Id);

                var votedCount = await _voterService.CountVotedVotersAsync(_currentElection.Id);

                var notVotedCount = votersCount - votedCount;

                if (votedCount == 0)
                {
                    MessageBox.Show("Cannot close the election without votes", "No votes yet", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                var message = string.Empty;

                if (notVotedCount > 0)
                {
                    message = $"You can view the result from the server '{ _currentElection.ServerTag }' once the { _currentElection.Title } is closed.\n\n" +
                              $"All { "voter".ToQuantity(notVotedCount) } who have not voted yet CANNOT BE ALLOWED to vote anymore.\n\n" +
                              $"Also, you WILL NO LONGER add, edit or delete voters.\n\n" +
                              $"Do you want to close the election and view the results?";
                }
                else
                {
                    message = $"You can view the result from the server '{ _currentElection.ServerTag }' once the { _currentElection.Title } is closed.\n\n" +
                              $"All voters have voted.\n\n" +
                              $"You WILL NO LONGER add, edit or delete voters once you close the election.\n\n" +
                              $"Do you want to close the election and view the results?";
                }

                var result = MessageBox.Show(message, "Close election", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);

                if (result == DialogResult.Yes)
                {
                    try
                    {
                        await _electionService.CloseElectionAsync(_currentElection.Id);

                        await _window.LoadElectionAsync();

                        await _window.LoadResultsAsync();

                        MessageBox.Show($"The { _currentElection.Title } is officially closed.", "Election closed", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        Close();
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex);

                        MessageBox.Show(ex.GetBaseException().Message, "PROGRAM ERROR: " + ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
            }
        }