コード例 #1
0
        /// <summary>
        /// Performs the vote recording.
        /// </summary>
        /// <param name="recordVotesInput">The record votes input.</param>
        /// <returns>
        /// Currently set as an 'object', which is a place-holder for an
        /// object that will contain the status of this 'Voting.'
        /// <para>
        /// The object returned does contain an integer <c>VotingSuccessful</c>
        /// property, which should be a 0 if all goes well.
        /// </para>
        /// </returns>
        public IvrVotingStatusModel OrchestrateVoteRecording(RecordVotesInputModel recordVotesInput)
        {
            var results = new IvrVotingStatusModel();
            var voterId = recordVotesInput.VoterId;

            try
            {
                var voter = _votersRepository.GetById(voterId);
                if (voter == null)
                {
                    // This should never happen because otherwise, how would
                    // this person have even been able to login?!
                    // If it has happened, then most likely there is a
                    // database issue of some kind.
                    results.VotingSuccessful = -1;
                    results.Exception        = new VoterNotFoundException(
                        $"The Voter with the Id of {voterId} was not found in the system.");

                    return(results);
                }

                // pull any votes cast already
                var votesCastForThisElection = _votesRepository
                                               .GetMany(vote => vote.VoterId.Equals(voterId)).ToList();

                if (votesCastForThisElection.Any())
                {
                    // make sure the votesCastForThisElection haven't already
                    // been cast. If so, return a value indicating that the
                    // voter's already voted.
                    results.VotingSuccessful = -2;

                    return(results);
                }

                IEnumerable <Vote> mappedVotesToCast = MapCastVotesToVotes(recordVotesInput);

                try
                {
                    var votesRecordedSuccessfully = RecordIvrVotes(voter, mappedVotesToCast.ToList());

                    results.VotingSuccessful = votesRecordedSuccessfully ? 0 : -4;
                }
                catch (OptimisticConcurrencyException oce)
                {
                    Console.WriteLine(oce);

                    results.VotingSuccessful = -3;
                    results.Exception        = oce;
                }
                catch (Exception oEx)
                {
                    Console.WriteLine(oEx);

                    results.VotingSuccessful = -3;
                    results.Exception        = oEx;
                }
            }
            catch (Exception oEx)
            {
                Console.WriteLine(oEx);

                results.VotingSuccessful = -3;
                results.Exception        = oEx;
            }

            return(results);
        }