public IActionResult Checkquiz([FromBody] UserAnswersDto userAnswers)
        {
            try
            {
                if (userAnswers == null)
                {
                    return(Json(new { Result = false, Message = "ERROR - Dane nie zostały przesłane." }));
                }

                var quiz = _context.Quiz.Where(q => q.Id == userAnswers.QuizId).FirstOrDefault();
                if (quiz == null)
                {
                    return(Json(new { Result = false, Message = "ERROR - Quiz o podanym Id nie istnieje." }));
                }

                int Points      = 0;
                var questionsDB = _context.Question.Where(q => q.QuizId == quiz.Id);
                foreach (var item in userAnswers.QuestionAnswers)
                {
                    var  questionDB = questionsDB.Where(q => q.Id == item.QuestionId).FirstOrDefault();
                    bool CorrectAnswerOnQuestion = true;
                    if (questionDB != null)
                    {
                        var qAnswersDB = _context.Answer.Where(q => q.QuestionId == questionDB.Id);
                        foreach (var itemAnswer in qAnswersDB)
                        {
                            var x = item.SelectedAnswersId.Where(q => q == itemAnswer.Id);
                            if (((x == null || x.Count() == 0) && itemAnswer.Correct) ||
                                (x != null && x.Count() > 0 && !itemAnswer.Correct))
                            {
                                CorrectAnswerOnQuestion = false;
                                break;
                            }
                        }
                        if (CorrectAnswerOnQuestion)
                        {
                            Points += 1;
                        }
                    }
                    else
                    {
                        return(Json(new { Result = false, Message = "ERROR - Pytanie o podanym Id nie istnieje." }));
                    }
                }

                float score = (float)Points / (float)questionsDB.Count();

                if (score > 0)
                {
                    score *= 100;
                }

                return(Json(new { Result = true, score = score }));
            }
            catch (Exception e)
            {
                return(Json(new { Result = false, Message = "Nieznany bład", ErrorMessage = e.Message }));
            }
        }
        public async Task <ActionResult <TestResultDto> > GetTestResult(int testId, [FromBody] UserAnswersDto userAnswersDto)
        {
            if (userAnswersDto != null && ModelState.IsValid)
            {
                var testResult = await _testService.GetTestResult(testId, userAnswersDto);

                return(Ok(testResult));
            }

            return(BadRequest("Failed to update test"));
        }
        public async Task <TestResultDto> GetTestResult(int testId, UserAnswersDto userAnswersDto)
        {
            var testResult = ComputeResults(testId, userAnswersDto);

            testResult.TestId    = testId;
            testResult.StartedAt = userAnswersDto.StartedAt;
            testResult.EndedAt   = userAnswersDto.EndedAt;
            testResult.UserName  = userAnswersDto.UserName;

            await _testResultRepository.AddAsync(testResult);

            return(_mapper.Map <TestResultDto>(testResult));
        }
        private TestResult ComputeResults(int testId, UserAnswersDto userAnswersDto)
        {
            var testResult = new TestResult();

            var test          = GetTestById(testId);
            var testQuestions = test.Questions.ToList();

            var userQApairs = userAnswersDto.QApairs.ToList();

            foreach (var qapair in userQApairs)
            {
                if (qapair.AnswerId == 0)
                {
                    testResult.SkippedCount++;
                }
                else
                {
                    var testQuestion = testQuestions.FirstOrDefault(q => q.Id == qapair.QuestionId);
                    if (testQuestion == null)
                    {
                        throw new ArgumentException("No question with this id");
                    }

                    var answer = testQuestion.QuestionAnswers.FirstOrDefault(qa => qa.Id == qapair.AnswerId);
                    if (answer == null)
                    {
                        throw new ArgumentException("No answer with this id");
                    }

                    if (answer.IsCorrect)
                    {
                        testResult.CorrectCount++;
                        testResult.Points += testQuestion.Points;
                    }
                    else
                    {
                        testResult.IncorrectCount++;
                    }
                }
            }

            return(testResult);
        }