Exemplo n.º 1
0
        public async Task <ResponseOutput> SumbitTest(TestSumbitModel testRequest)
        {
            var answers = testRequest.Answers;
            var test    = _testRepo.Entities.FirstOrDefault(_ => _.Id == testRequest.Id);

            if (test != null && testRequest.Answers != null)
            {
                var numberCorrect = answers.Where(_ => _.IsCorrect == true).ToList().Count;
                var userId        = answers[0].UserId;

                var guidId = Guid.NewGuid();

                // save test result
                var testResult = new TestResults
                {
                    TestId        = testRequest.Id,
                    UserId        = userId,
                    CorrectAnswer = numberCorrect,
                    ExamedAt      = DateTime.Now,
                    GuidId        = guidId.ToString(),
                    TotalQuestion = answers.Count,
                    TotalTime     = testRequest.TotalTime,
                };

                _testResultRepo.Insert(testResult);
                UnitOfWork.SaveChanges();

                // Save answer
                var answersDB = new List <Answers>();
                foreach (var answer in answers)
                {
                    var answerDB = new Answers
                    {
                        UserId       = answer.UserId,
                        TopicId      = answer.TopicId,
                        IsCorrect    = answer.IsCorrect,
                        QtionId      = answer.QuestionId,
                        UpdateDay    = DateTime.Now,
                        TestResultId = testResult.Id,
                        Answer       = answer.UserAnswer
                    };
                    answersDB.Add(answerDB);
                }

                _answerRepo.InsertRange(answersDB);
                UnitOfWork.SaveChanges();


                await CalculateProficient(answers, testResult.GuidId, userId);

                var topicIds = GetTopics(answers);
                await CalculateProficientTop(topicIds, testResult.Id, userId);

                return(new ResponseOutput(true, "", guidId.ToString()));
            }

            return(new ResponseOutput(false, "array is emtpy."));
        }
Exemplo n.º 2
0
 public async Task <ResponseOutput> SumbitTest([FromBody] TestSumbitModel input)
 {
     return(await _service.SumbitTest(input));
 }