コード例 #1
0
        public OperationResultVo PollVote(Guid currentUserId, Guid pollOptionId)
        {
            try
            {
                int  pointsEarned = 0;
                Poll poll         = pollDomainService.GetPollByOptionId(pollOptionId);

                if (poll == null)
                {
                    return(new OperationResultVo("Unable to identify the poll you are voting for."));
                }

                bool alreadyVoted = poll.Votes.Any(x => x.PollOptionId == pollOptionId && x.UserId == currentUserId);

                if (alreadyVoted)
                {
                    return(new OperationResultVo("You already voted on this option."));
                }

                IEnumerable <PollVote> userVotesOnThisPoll = poll.Votes.Where(x => x.UserId == currentUserId);

                if (poll.MultipleChoice || !userVotesOnThisPoll.Any())
                {
                    pollDomainService.AddVote(currentUserId, poll.Id, pollOptionId);
                }
                else
                {
                    PollVote oldVote = userVotesOnThisPoll.FirstOrDefault();
                    if (oldVote != null)
                    {
                        pollDomainService.ReplaceVote(currentUserId, poll.Id, oldVote.PollOptionId, pollOptionId);
                    }
                }

                if (!userVotesOnThisPoll.Any())
                {
                    pointsEarned = gamificationDomainService.ProcessAction(currentUserId, PlatformAction.PollVote);
                }

                unitOfWork.Commit();

                PollResultsViewModel resultVm = CalculateNewResult(poll);

                return(new OperationResultVo <PollResultsViewModel>(resultVm, pointsEarned));
            }
            catch (Exception ex)
            {
                return(new OperationResultVo(ex.Message));
            }
        }