/// <summary> /// Maps the collection (List<VoteToRecord>) of cast votes to /// a collection of <see cref="Vote"/> entities. (IEnumerable<Vote>) /// </summary> /// <param name="recordVotesInput">The record votes input.</param> /// <returns></returns> public virtual IEnumerable <Vote> MapCastVotesToVotes( RecordVotesInputModel recordVotesInput) { var voterId = recordVotesInput.VoterId; ////public class VoteToRecord //// public int CandidateID { get; set; } //// public string VoteDate { get; set; } // first, convert a VoteToRecord to a Vote entity, or, in this // case, the collection. var mapperConfig = new MapperConfiguration(cfg => { cfg.CreateMap <VoteToRecord, Vote>() .BeforeMap((src, dest) => { dest.VoterId = voterId; dest.CandidateId = src.CandidateID; dest.Candidate = _candidatesRepository.GetById(src.CandidateID); dest.Voter = _votersRepository.GetById(voterId); }) .ForMember( dest => dest.Id, opt => opt.MapFrom(src => src.CandidateID)); }); var iMapper = mapperConfig.CreateMapper(); var votesToRecord = iMapper.Map <List <VoteToRecord>, IEnumerable <Vote> >(recordVotesInput.VotesToRecord); return(votesToRecord); }
public IHttpActionResult RecordVotes( [FromBody] RecordVotesInputModel votesToRecord) { IvrVotingStatusModel result; try { result = _votesServices.OrchestrateVoteRecording(votesToRecord); } catch (Exception oEx) { return(InternalServerError(oEx)); } return(Ok(result)); }
/// <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); }