示例#1
0
        public async Task <VoteResponse> Vote(string PostId, string UserId)
        {
            var post = await _postRepository.GetAsync(PostId);

            if (post == null)
            {
                return(new VoteResponse("Post not found."));
            }

            var vote = await _voteRepository.FindInstance(PostId, UserId);

            if (vote != null)
            {
                return(new VoteResponse("You already voted."));
            }

            var Vote = new Vote {
                PostId = PostId, UserId = UserId
            };

            try
            {
                await _voteRepository.AddAsync(Vote);

                post.Rating += 1;
                await _unitOfWork.CompleteAsync();

                return(new VoteResponse(Vote));
            }
            catch (Exception ex)
            {
                return(new VoteResponse($"An error occurred when saving the vote: {ex.Message}"));
            }
        }
        public async Task <bool> CastVote(int candidateId, int categoryId, int voterId)
        {
            bool isVoteCasted = false;
            var  voter        = await VoterRepository.GetByIdAsync(voterId);

            var candidate = await CandidateRepository.GetByIdAsync(candidateId);

            var category = await CategoryRepository.GetByIdAsync(categoryId);

            if (voter != null && candidate != null && category != null)
            {
                var vote = await VoteRepository.FindAsync(s => s.CategoryId == categoryId && s.VoterId == voterId);

                // Check to verify if voter has already casted vote for category
                if (!vote.Any())
                {
                    var newVote = new Vote {
                        VoterId = voterId, CategoryId = categoryId, CandidateId = candidateId
                    };
                    var data = await VoteRepository.AddAsync(newVote);

                    isVoteCasted = true;
                }
            }

            return(isVoteCasted);
        }
示例#3
0
        public async Task <IActionResult> Index()
        {
            try
            {
                string body = Request.Form["Body"];
                string from = Request.Form["From"];
                _logger.LogWarning(body + " | " + body.Substring(0, 2));
                var responseMessage = new MessagingResponse();
                if (!_voteService.IsSpecialCommand(body))
                {
                    if (await _voteService.PhoneNumberHasVotedToday(from))
                    {
                        textResponse = responseMessage.Message($"You have already voted today. Please vote again tomorrow. {messageEnding}").ToString();
                    }
                    else
                    {
                        bool isValidMessage = await _voteService.ValidateVoteMessageAsync(body);

                        if (!isValidMessage)
                        {
                            textResponse = responseMessage.Message($"This is an invalid vote. Please check the entry ID and try again. {messageEnding}").ToString();
                        }
                        else
                        {
                            Camera camera = await _voteService.GetCameraByCameraName(body.Substring(0, 2).ToUpper());

                            VoteEntry voteEntry = VoteEntry.CreateEntry(body, from, camera);
                            _repository.AddAsync(voteEntry);
                            _repository.SaveAsync();

                            textResponse = responseMessage.Message($"Your vote is in!!. You may only vote once per day. {messageEnding}").ToString();
                        }
                    }
                }
                else
                {
                }
                return(Content(textResponse, "application/xml"));
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex.Message);
                throw;
            }
        }