예제 #1
0
        public async Task <QuizViewModel?> GetActiveQuizAsync()
        {
            var retrieveUserOperation = TableOperation.Retrieve <QuizEntity>(Active, Active);
            var result = await _quizzesTable.ExecuteAsync(retrieveUserOperation);

            return(result.Result is QuizEntity entity?QuizViewModel.FromJson(entity.Json) : null);
        }
예제 #2
0
        public async Task <QuizViewModel?> ActivateQuizAsync(string id)
        {
            var retrieveOperation = TableOperation.Retrieve <QuizEntity>(Quizzes, id);
            var result            = await _quizzesTable.ExecuteAsync(retrieveOperation);

            if (!(result.Result is QuizEntity quizEntity))
            {
                // No quiz found so clear the current active quiz
                var retrieveActiveQuizOperation = TableOperation.Retrieve <QuizEntity>(Active, Active);
                var activeQuizResult            = await _quizzesTable.ExecuteAsync(retrieveActiveQuizOperation);

                if (activeQuizResult.Result is QuizEntity activeQuiz)
                {
                    var deleteOperation = TableOperation.Delete(activeQuiz);
                    await _quizzesTable.ExecuteAsync(deleteOperation);
                }
                return(null);
            }

            var quiz = QuizViewModel.FromJson(quizEntity.Json);

            if (quiz.Questions.Any(q => q.Parameters.ClearOnActivation))
            {
                await DeleteResponsesAsync(quiz.ID);
            }

            quizEntity.PartitionKey = quizEntity.RowKey = Active;
            var upsertOperation = TableOperation.InsertOrReplace(quizEntity);
            var upsertResult    = await _quizzesTable.ExecuteAsync(upsertOperation);

            return(quiz);
        }
예제 #3
0
        public async Task GetResultsAsync(string id)
        {
            if (id == HubConstants.Active)
            {
                var activeQuiz = await _quizDataContext.GetActiveQuizAsync();

                if (activeQuiz != null)
                {
                    await Groups.AddToGroupAsync(Context.ConnectionId, HubConstants.Active);

                    var quiz = QuizViewModel.FromJson(activeQuiz.Json);
                    id = quiz.ID;

                    var counter = await _quizDataContext.GetConnectionCountAsync();

                    await Clients.Caller.SendAsync(HubConstants.ConnectedMethod, new ConnectionViewModel()
                    {
                        Counter = counter
                    });
                }
            }

            await Groups.AddToGroupAsync(this.Context.ConnectionId, id);

            var resultsBuilder = new QuizResultBuilder(_quizDataContext);
            var results        = await resultsBuilder.GetResultsAsync(id);

            await Clients.Caller.SendAsync(HubConstants.ResultsMethod, results);
        }
예제 #4
0
        public override async Task OnConnectedAsync()
        {
            var count   = _connectionStorage.Increment();
            var counter = await _quizDataContext.UpsertServerConnectionsAsync(count);

            var connection = new ConnectionViewModel()
            {
                Counter = counter
            };

            await Clients.All.Connected(connection);

            await _quizResultsHub.Clients.Group(HubConstants.Active).SendAsync(HubConstants.ConnectedMethod, connection);

            var userID     = this.Context.UserIdentifier;
            var activeQuiz = await _quizDataContext.GetActiveQuizAsync();

            if (activeQuiz != null &&
                !string.IsNullOrEmpty(userID))
            {
                var quiz             = QuizViewModel.FromJson(activeQuiz.Json);
                var userHasResponded = await _quizDataContext.UserHasResponseAsync(quiz.ID, userID);

                if (!userHasResponded)
                {
                    await Clients.Caller.Quiz(quiz);
                }
            }

            await base.OnConnectedAsync();
        }
예제 #5
0
        public async IAsyncEnumerable <QuizViewModel> GetQuizzesAsync()
        {
            var query = new TableQuery <QuizEntity>()
                        .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, Quizzes));
            var token = new TableContinuationToken();

            do
            {
                var result = await _quizzesTable.ExecuteQuerySegmentedAsync <QuizEntity>(query, token);

                foreach (var item in result)
                {
                    yield return(QuizViewModel.FromJson(item.Json));
                }
                token = result.ContinuationToken;
            } while (token != null);
        }
예제 #6
0
        public async Task <ResultViewModel> GetResultsAsync(string id)
        {
            var quizEntity = await _quizDataContext.GetQuizAsync(id);

            var quiz = quizEntity != null?
                       QuizViewModel.FromJson(quizEntity.Json) :
                           QuizViewModel.CreateBlank();

            var results = new ResultViewModel
            {
                ID    = quiz.ID,
                Title = quiz.Title
            };

            if (quizEntity != null)
            {
                var responseCount = 0;
                var responses     = new Dictionary <string, int>();
                await foreach (var response in _quizDataContext.GetQuizResponsesAsync(id))
                {
                    // Process: "q1=option1,option2;q2=optionA"
                    responseCount++;
                    var userResponses = response.Split(';');
                    foreach (var userResponse in userResponses)
                    {
                        var userResponseString = userResponse.Split('=');
                        var key     = userResponseString[0];
                        var options = userResponseString[1].Split(',');

                        foreach (var option in options)
                        {
                            var answerKey = $"{key}={option}";
                            if (responses.ContainsKey(answerKey))
                            {
                                responses[answerKey]++;
                            }
                            else
                            {
                                responses[answerKey] = 1;
                            }
                        }
                    }
                }

                foreach (var question in quiz.Questions)
                {
                    var list = new List <ResultQuestionAnswerViewModel>();
                    foreach (var option in question.Options)
                    {
                        var key   = $"{question.ID}={option.OptionId}";
                        var count = responses.ContainsKey(key) ? responses[key] : 0;

                        var row = new ResultQuestionAnswerViewModel()
                        {
                            Name  = option.OptionText,
                            Count = count
                        };

                        list.Add(row);
                    }

                    results.Results.Add(new ResultQuestionViewModel()
                    {
                        ID      = question.ID,
                        Title   = question.Title,
                        Answers = list
                    });
                }

                results.Responses = responseCount;
            }

            return(results);
        }