Exemplo n.º 1
0
        private void VoteButton_Clicked(object sender, EventArgs e)
        {
            try
            {
                //Check if all fields valid, then check if pesel is not blocked, then check if pesel didn't already vote
                if (ValidateFields())
                {
                    //Disable login form
                    SetLoginFormEnable(false);

                    //Download blocked pesels from server and check if it contains provided pesel
                    _peselWorker         = new BackgroundWorker();
                    _peselWorker.DoWork += (s, ea) =>
                    {
                        try
                        {
                            List <string> errorList     = new List <string>();
                            string        providedPesel = "";

                            //Show loading overlay
                            this.Dispatcher.Invoke(() =>
                            {
                                _mainWindow.LoadingLabel.Foreground     = ColorHelper.HexToColorBrush("#1c9ad2");
                                _mainWindow.LoadingLabel.Content        = "Checking your data...";
                                _mainWindow.LoadingContainer.Visibility = Visibility.Visible;
                                providedPesel = PeselTextBox.Text;
                            });


                            using (System.Net.WebClient client = new System.Net.WebClient())
                            {
                                string jsonData =
                                    client.DownloadString(new Uri("http://webtask.future-processing.com:8069/blocked"));

                                BlockedRoot root =
                                    JsonConvert.DeserializeObject <BlockedRoot>(jsonData);

                                List <string> blockedPesels = root.Disallowed.Person.Select(obj => obj.Pesel).ToList();

                                if (blockedPesels.Contains(providedPesel))
                                {
                                    //Blocked pesels list contains provided pesel, invoke event to report disallowed try to database
                                    errorList.Add("You have lost your voting privileges.");
                                    Dispatcher.Invoke(() =>
                                    {
                                        LoginDisallowed?.Invoke(this,
                                                                new LoginEventArgs()
                                        {
                                            FirstName = FirstNameTextBox.Text,
                                            LastName  = LastNameTextBox.Text,
                                            Pesel     = PeselTextBox.Text
                                        });
                                    });
                                }
                                else
                                {
                                    //Check if pesel already voted
                                    if (DBHelper.CheckPeselAlreadyVoted(providedPesel))
                                    {
                                        errorList.Add("You have already voted.");
                                    }
                                }
                                ea.Result = errorList;
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(
                                "An error occurred while checking your data, please contact person responsible for managing the system",
                                "Error occured", MessageBoxButton.OK);
                            _mainWindow.Logout();
                        }
                    };

                    //If pesel is blocked or already voted, lock program for 10 seconds
                    _peselWorker.RunWorkerCompleted += (s, ea) =>
                    {
                        List <string> errors = (List <string>)ea.Result;

                        if (errors.Count > 0)
                        {
                            //Show warning overlay
                            _mainWindow.LoadingLabel.Foreground           = ColorHelper.HexToColorBrush("#d11b1b");
                            _mainWindow.LoadingLabel.Content              = errors.First();
                            _mainWindow.LoadingAdditionalLabel.Content    = "Returning to login page in 10 seconds";
                            _mainWindow.LoadingAdditionalLabel.Visibility = Visibility.Visible;

                            //Count 10 seconds
                            int counter = 9;

                            Timer timer = new Timer();
                            timer.Interval = 1000;
                            timer.Elapsed += (s2, ea2) =>
                            {
                                Dispatcher.Invoke(() =>
                                {
                                    _mainWindow.LoadingAdditionalLabel.Content =
                                        $"Returning to login page in {counter} seconds";

                                    if (counter == 0)
                                    {
                                        //10 seconds elapsed, stop timer, hide warning overlay, clear and unlock login form
                                        timer.Stop();
                                        _mainWindow.LoadingContainer.Visibility       = Visibility.Collapsed;
                                        _mainWindow.LoadingAdditionalLabel.Visibility = Visibility.Collapsed;
                                        ClearLoginForm();

                                        SetLoginFormEnable(true);

                                        FirstNameTextBox.FocusOnMe();
                                    }
                                });

                                counter--;
                            };

                            timer.Start();
                        }
                        else
                        {
                            //Login successfull
                            LoginSuccessful?.Invoke(this,
                                                    new LoginEventArgs()
                            {
                                FirstName = FirstNameTextBox.Text,
                                LastName  = LastNameTextBox.Text,
                                Pesel     = PeselTextBox.Text
                            });
                            _mainWindow.LoadingContainer.Visibility = Visibility.Collapsed;
                        }
                    };

                    _peselWorker.RunWorkerAsync();
                }
            }
            catch (Exception)
            {
                MessageBox.Show(
                    "An error occurred while checking your data, please contact person responsible for managing the system",
                    "Error occured", MessageBoxButton.OK);
                _mainWindow.Logout();
            }
        }
Exemplo n.º 2
0
        private void _votingWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                using (System.Net.WebClient client = new System.Net.WebClient())
                {
                    string jsonData =
                        client.DownloadString(new Uri("http://webtask.future-processing.com:8069/blocked"));

                    BlockedRoot root =
                        JsonConvert.DeserializeObject <BlockedRoot>(jsonData);

                    List <string> blockedPesels = root.Disallowed.Person.Select(obj => obj.Pesel).ToList();

                    if (blockedPesels.Contains(_mainWindow.LoginInfo.Pesel))
                    {
                        //Blocked pesels list contains provided pesel, invoke event to report disallowed try to database
                        Dispatcher.Invoke(() =>
                        {
                            LoginDisallowed?.Invoke(this, _mainWindow.LoginInfo);
                        });

                        //Show warning overlay
                        _mainWindow.LoadingLabel.Foreground           = ColorHelper.HexToColorBrush("#d11b1b");
                        _mainWindow.LoadingLabel.Content              = "You have lost your voting privilages";
                        _mainWindow.LoadingAdditionalLabel.Content    = "Returning to login page in 10 seconds";
                        _mainWindow.LoadingAdditionalLabel.Visibility = Visibility.Visible;

                        //Count 10 seconds
                        int counter = 9;

                        Timer timer = new Timer();
                        timer.Interval = 1000;
                        timer.Elapsed += (s2, ea2) =>
                        {
                            Dispatcher.Invoke(() =>
                            {
                                _mainWindow.LoadingAdditionalLabel.Content =
                                    $"Returning to login page in {counter} seconds";

                                if (counter == 0)
                                {
                                    //10 seconds elapsed, stop timer, hide warning overlay, clear and unlock login form
                                    timer.Stop();
                                    _mainWindow.LoadingContainer.Visibility       = Visibility.Collapsed;
                                    _mainWindow.LoadingAdditionalLabel.Visibility = Visibility.Collapsed;

                                    _mainWindow.Logout();
                                }
                            });

                            counter--;
                        };

                        timer.Start();
                    }
                    else
                    {
                        //Check if vote is valid or invalid, cast appropriate vote
                        if (_candidates.Count == 1)
                        {
                            //Vote valid, cast vote
                            DBHelper.AddCorrectVote(_candidates.First().Name, _candidates.First().Party,
                                                    _mainWindow.LoginInfo.FirstName,
                                                    _mainWindow.LoginInfo.LastName, _mainWindow.LoginInfo.Pesel, DateTime.Now);
                        }
                        else
                        {
                            //Vote invalid, cast vote
                            DBHelper.AddSpoiledVote(_mainWindow.LoginInfo.FirstName,
                                                    _mainWindow.LoginInfo.LastName, _mainWindow.LoginInfo.Pesel, DateTime.Now);
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show(
                    "An error occurred, please contact person responsible for managing the system",
                    "Error occured", MessageBoxButton.OK);
                _mainWindow.Logout();
            }
        }