Beispiel #1
0
        public async Task <ActionResult <TestResultsDto> > CheckAnswers(TestAnswersDto answers, CancellationToken token)
        {
            var test = await testStorage.FindTest(answers.TestId, token);

            if (test == null)
            {
                return(UnprocessableEntity("test with this id does not exist"));
            }
            if (!User.OwnsResource(test))
            {
                return(Forbid());
            }

            var userAnswers = new Dictionary <Guid, IAnswer>();

            foreach (var answer in answers.Answers)
            {
                if (!userAnswers.ContainsKey(answer.Id))
                {
                    userAnswers.Add(answer.Id, answer.Answer);
                }
            }
            var verdicts = TestChecker.Check(userAnswers, test);

            var results = new TestResultsDto();

            foreach (var(key, _) in verdicts)
            {
                var exercise = test.Exercises.First(e => e.Id == key);
                results.Answers.Add(key, new ExerciseVerdictDto(verdicts[key], exercise.Answer));
                await storage.UpdateCardsAwareness(exercise.UsedCardsIds, verdicts[key]? 3 : -3, token);
            }

            return(Ok(results));
        }
Beispiel #2
0
        public static TestCommonDto GetTest(int id)
        {
            using (UserContext db = new UserContext())
            {
                var testCommonDto = new TestCommonDto();
                var findTest      = db.Test.Where(p => p.Id == id).FirstOrDefault();
                if (findTest != null)
                {
                    testCommonDto.Id   = findTest.Id;
                    testCommonDto.Name = findTest.Name;

                    var questionList = db.Question.Where(p => p.Test.Id == findTest.Id).ToList();

                    List <TestQuestionsDto> questionListDto = new List <TestQuestionsDto>();
                    foreach (var question in questionList)
                    {
                        TestQuestionsDto testQuestionsDto = new TestQuestionsDto();
                        testQuestionsDto.Id   = question.Id;
                        testQuestionsDto.Name = question.Name;

                        List <TestAnswersDto> answersListDto = new List <TestAnswersDto>();
                        var answersList = db.Answers.Where(p => p.Question.Id == question.Id).ToList();
                        foreach (var answer in answersList)
                        {
                            TestAnswersDto testAnswersDto = new TestAnswersDto();
                            testAnswersDto.Id   = answer.Id;
                            testAnswersDto.Name = answer.Name;

                            answersListDto.Add(testAnswersDto);
                        }
                        testQuestionsDto.TestAnswersDto = answersListDto;

                        questionListDto.Add(testQuestionsDto);
                    }
                    testCommonDto.TestQuestionsDto = questionListDto;
                    return(testCommonDto);
                }
                return(null);
            }
        }
Beispiel #3
0
 public IActionResult Post(TestAnswersDto dto)
 {
     _testRepository.SaveTestResult(dto);
     return(Ok());
 }