예제 #1
0
        public ActionResult Vote(FormCollection formCollection)
        {
            string errorMessage = "";

            try
            {
                var elections  = _electionManager.RetrieveElectionsByActive(true);
                var delegateID = formCollection[1].ToString();
                if (formCollection.Count != elections.Count + 2)
                {
                    errorMessage = "Please vote for all elections";
                    return(RedirectToAction("Vote", new { delegateID = delegateID, errorMessage = errorMessage }));
                }
                else
                {
                    var oldDelegate = _delegateManager.RetrieveDelegateByID(Int32.Parse(delegateID));

                    var newDelegate = new ElectionDelegate
                    {
                        DelegateID           = oldDelegate.DelegateID,
                        Active               = oldDelegate.Active,
                        FirstName            = oldDelegate.FirstName,
                        LastName             = oldDelegate.LastName,
                        Pin                  = oldDelegate.Pin,
                        HasVotedForGrants    = oldDelegate.HasVotedForGrants,
                        HasVotedForElections = true // mark that this delegate has voted for elections
                    };

                    // gives a vote to the designated candidate
                    for (int i = 2; i < formCollection.Count; i++)
                    {
                        var candidate = _candidateManager.RetrieveCandidateByCandidateID(Int32.Parse(formCollection[i]));
                        candidate.Votes += 1;
                        _candidateManager.EditCandidateVotes(candidate);
                    }

                    _delegateManager.EditDelegate(oldDelegate, newDelegate);
                    return(RedirectToAction("VotingConfirmation"));
                }
            }
            catch (Exception)
            {
                return(View());
            }
        }