Exemplo n.º 1
0
            public async Task <UserExamResultDTOEnvelope> Handle(Request request, CancellationToken cancellationToken)
            {
                var userId = currentUserAccessor.GetCurrentUserId();

                var userExam = await this.context.UserExams
                               .Include(e => e.Exam).Where(e => e.Id == request.UserExamId && e.UserId == userId && e.Exam.IsPublished == true).FirstOrDefaultAsync();

                if (userExam == null)
                {
                    throw new Exceptions.UserExamNotFoundException();
                }

                if (!userExam.EndedAt.HasValue)
                {
                    await UserExam.UserExamHelper.EndUserExamIfTimeExpired(context, cancellationToken, userExam);
                }
                if (!userExam.EndedAt.HasValue)
                {
                    throw new Exceptions.UserExamNotFoundException();
                }
                var userExamResult = await context.UserExamResults.FindAsync(userExam.Id);

                if (userExamResult != null)
                {
                    return(MakeEnvelope(userExamResult));
                }
                userExamResult = new Domain.UserExamResult()
                {
                    UserExamId = userExam.Id
                };

                userExamResult.QuestionCount = context.Questions.Where(e => e.ExamId == userExam.ExamId).Count();

                var userExamQuestionAnswers = context.UserExamQuestions.Where(e => e.UserExamId == userExam.Id)
                                              .Include(e => e.UserExamQuestionAnswers).ToArray();

                foreach (var userExamQuestionAnswer in userExamQuestionAnswers)
                {
                    userExamQuestionAnswer.HasRightAnswer = IsRight(userExamQuestionAnswer);
                }

                userExamResult.AnsweredQuestionCount    = userExamQuestionAnswers.Length;
                userExamResult.NotAnsweredQuestionCount = userExamResult.QuestionCount - userExamResult.AnsweredQuestionCount;
                userExamResult.RightAnswerCount         = userExamQuestionAnswers.Where(e => e.HasRightAnswer == true).Count();
                userExamResult.WrongAnswerCount         = userExamQuestionAnswers.Where(e => e.HasRightAnswer == false).Count();
                userExamResult.IsPassed = (float)userExamResult.RightAnswerCount / (float)userExamResult.QuestionCount * 100
                                          >= userExam.Exam.PassPercentage;

                await context.UserExamResults.AddAsync(userExamResult);

                await context.SaveChangesAsync();

                context.Entry(userExamResult).Reload();
                return(MakeEnvelope(userExamResult));
            }
Exemplo n.º 2
0
            private UserExamResultDTOEnvelope MakeEnvelope(Domain.UserExamResult userExamResult)
            {
                var userExamResultDto = mapper.Map <Domain.UserExamResult, UserExamResultDTO>(userExamResult);

                return(new UserExamResultDTOEnvelope(userExamResultDto));
            }