Exemplo n.º 1
0
        /// <summary>
        /// Records the votes sent via the IVR system.
        /// </summary>
        /// <param name="voter">
        /// An instance of the <see cref="Voter"/> class containing the
        /// 'current' voter.
        /// </param>
        /// <param name="votesToRecord">
        /// A List&lt;Vote&gt; containing the votes to save to the database.
        /// </param>
        /// <returns>
        /// A boolean value indicating whether (true) or not (false) the Votes
        /// were all recorded.
        /// </returns>
        public virtual bool RecordIvrVotes(
            Voter voter,
            List <Vote> votesToRecord)
        {
            var votesToRecordCount = 0;

            foreach (var vote in votesToRecord)
            {
                var newVote = vote;
                _votesRepository.Add(newVote);
                votesToRecordCount++;
            }

            // Mark Voting as Complete!
            voter.VoteCompleted = true;
            _votersRepository.Update(voter);

            // since all of the tables are within the single DbContext
            // (IVotingSiteAPIDbCtx/VotingSiteAPIDbCtx), it's okay to
            // use the Votes repository [in this context] in order to
            // get/use an instance of same.
            // This should write the Votes AND the Voter to the database
            // within a transaction. So, if either write fails, both should.
            var numChanges = _votesRepository.GetDbContext().SaveChanges();

            // the /+ 1/ is for the Voter
            return(numChanges == votesToRecordCount + 1);
        }