public async Task <ActionResult> NextQuestion(int id)
        {
            try {
                Poll updatedPoll = await _pollRepository.SetNextQuestionAsync(id);

                if (updatedPoll != null)
                {
                    PollResult message = PollHelper.GetPollResults(updatedPoll);

                    await _hubContext.Clients.All.SendAsync(MessageTypes.LOAD_QUESTION, message);

                    _logger.LogInformation(LoggingEvents.PollSetNextQuestion, "Poll {id} next question updated", id);
                    return(Ok(updatedPoll));
                }
                else
                {
                    return(StatusCode(500, "Error occurred updating the database"));
                }
            }
            catch (PollNotFoundException e) {
                ApiStatusMessage a = ApiStatusMessage.CreateFromException(e);
                return(BadRequest(a));
            }
            catch (PollAtLastQuestionException e) {
                ApiStatusMessage a = ApiStatusMessage.CreateFromException(e);
                return(BadRequest(a));
            }
            catch (Exception e) {
                _logger.LogError(LoggingEvents.PollSetNextQuestion, "Error setting next question for poll {id}: Exception {ex}", id, e.Message);
                return(StatusCode(500, e.Message));
            }
        }
        /// <summary>
        /// Register the vote by updating the database
        /// and sending a message to all clients connected
        /// to the hub to inform interested parties
        /// </summary>
        /// <param name="pollId"></param>
        /// <param name="question"></param>
        /// <param name="answer"></param>
        private async Task RegisterVote(int pollId, int question, int answer)
        {
            // Register the vote in the database
            Poll updatedPoll = await _pollRepository.VoteAsync(pollId, question, answer);

            if (updatedPoll != null)
            {
                // then notify connected clients of the poll status using SignalR
                PollResult message = PollHelper.GetPollResults(updatedPoll);
                await _hubContext.Clients.All.SendAsync(MessageTypes.VOTE_RECEIVED, message);

                _logger.LogInformation(LoggingEvents.PollVoteRegistered, "Poll {id} vote registered", pollId);
            }
        }
        public async Task <ActionResult> GetPollResults(int id)
        {
            var poll = await _pollRepository.GetPollAsync(id) ?? new Poll();

            if (poll == null)
            {
                return(NotFound());
            }

            PollResult result = PollHelper.GetPollResults(poll);

            _logger.LogInformation(LoggingEvents.GetPollResults, "Get poll results for {id}", id);

            return(Ok(result));
        }
        public async Task <ActionResult> ResetPoll(int id)
        {
            try
            {
                Poll poll = await _pollRepository.GetPollAsync(id);

                if (poll != null)
                {
                    // Go through each question and reset the vote counts
                    foreach (Question question in poll.Questions)
                    {
                        foreach (Answer answer in question.Answers)
                        {
                            answer.VoteCount = 0;
                        }
                    }

                    poll.CurrentQuestion = 1;

                    // Update the poll then notify connected clients of the poll status using SignalR
                    poll = await _pollRepository.UpdatePollAsync(poll);

                    PollResult message = PollHelper.GetPollResults(poll);

                    await _hubContext.Clients.All.SendAsync(MessageTypes.RESET_POLL, message);

                    _logger.LogInformation(LoggingEvents.ResetPoll, "Poll {id} has been reset", id);
                    return(Ok(poll));
                }
                else
                {
                    return(StatusCode(500, "Error occurred updating the database"));
                }
            }
            catch (PollNotFoundException e)
            {
                ApiStatusMessage a = ApiStatusMessage.CreateFromException(e);
                return(BadRequest(a));
            }
            catch (Exception e)
            {
                _logger.LogError(LoggingEvents.PollSetNextQuestion, "Error resetting poll {id}: Exception {ex}", id, e.Message);
                return(StatusCode(500, e.Message));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Update the database poll record with the selected vote
        /// and notify any connected clients
        /// </summary>
        /// <param name="pollId"></param>
        /// <param name="question"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public async Task <VoteResult> Vote(int pollId, int question, int answer)
        {
            VoteResult result = new VoteResult();

            if (question < 1 || answer < 1)
            {
                result.StatusCode = 400;
                result.Errors.Add("Question and Answer index values must start at 1");
                return(result);
            }

            try {
                // Register the vote in the database
                Poll updatedPoll = await _pollRepository.VoteAsync(pollId, question, answer);

                if (updatedPoll != null)
                {
                    // then notify other connected clients of the poll status using SignalR
                    PollResult message = PollHelper.GetPollResults(updatedPoll);
                    await SendMessageToOthers(MessageTypes.VOTE_RECEIVED, message);

                    result.StatusCode = 200;
                }
                else
                {
                    result.StatusCode = 400;
                    result.Errors.Add("Poll does not exist or is closed");
                }
            }
            catch (Exception e) {
                result.StatusCode = 500;
                result.Errors.Add(e.Message);
            }

            return(result);
        }