Exemplo n.º 1
0
            public async Task <UserExamDTOEnvelope> Handle(Request request, CancellationToken cancellationToken)
            {
                var userId = this.currentUserAccessor.GetCurrentUserId();
                var exam   = context.Exams.Where(e => e.Id == request.ExamId && e.IsPublished == true).FirstOrDefault();

                if (exam == null)
                {
                    throw new Exceptions.ExamNotFoundException();
                }
                if (exam.IsPrivate && exam.UserId != userId)
                {
                    throw new Exceptions.ExamNotFoundException();
                }

                var userExam = new Domain.UserExam();

                userExam.ExamId       = request.ExamId;
                userExam.UserId       = userId;
                userExam.StartedtedAt = DateTime.UtcNow;

                await this.context.UserExams.AddAsync(userExam, cancellationToken);

                await context.SaveChangesAsync(cancellationToken);

                context.Entry(userExam).Reload();
                var userExamDto = mapper.Map <Domain.UserExam, UserExamDTO>(userExam);

                return(new UserExamDTOEnvelope(userExamDto));
            }
Exemplo n.º 2
0
            public async Task <UserExamDTOEnvelope> Handle(Request request, CancellationToken cancellationToken)
            {
                var userId = this.currentUserAccessor.GetCurrentUserId();

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

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

                if (userExam.Exam.IsPrivate && userExam.Exam.UserId != userId)
                {
                    throw new Exceptions.PrivateUserExamEditException();
                }

                if (userExam.EndedAt.HasValue)
                {
                    throw new Exceptions.UserExamAlreadyEndedException();
                }


                userExam.EndedAt = DateTime.UtcNow;
                await context.SaveChangesAsync(cancellationToken);

                context.Entry(userExam).Reload();
                var userExamDto = mapper.Map <Domain.UserExam, UserExamDTO>(userExam);

                return(new UserExamDTOEnvelope(userExamDto));
            }
Exemplo n.º 3
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.º 4
0
            public async Task <QuestionDTOEnvelope> Handle(Request request, CancellationToken cancellationToken)
            {
                var userId = currentUserAccessor.GetCurrentUserId();
                var exam   = context.Exams.NotPublishedByIdAndUserId(request.ExamId, userId).FirstOrDefault();

                if (exam == null)
                {
                    throw new Exceptions.ExamNotFoundException();
                }

                var question = mapper.Map <QuestionData, Domain.Question>(request.Question);

                question.ExamId = exam.Id;
                question.Exam   = exam;
                var utcNow = DateTime.UtcNow;

                question.CreatedAt = utcNow;
                question.UpdatedAt = utcNow;

                question.AnswerOptions.ToList().ForEach(ao =>
                {
                    ao.Question  = question;
                    ao.CreatedAt = utcNow;
                    ao.UpdatedAt = utcNow;
                });
                await this.context.Questions.AddAsync(question);

                await context.SaveChangesAsync();

                context.Entry(question).Reload();
                foreach (var answerOption in question.AnswerOptions)
                {
                    context.Entry(answerOption).Reload();
                }
                var questionDto = mapper.Map <Domain.Question, QuestionDTO>(question);

                return(new QuestionDTOEnvelope(questionDto));
            }
Exemplo n.º 5
0
            public async Task <QuestionAnswerDTOEnvelope> Handle(Request request, CancellationToken cancellationToken)
            {
                var requestQA       = request.UserExamQuestionAnswer;
                var answerOptionIds = requestQA.AnswerOptionIds.ToArray();
                var userId          = this.currentUserAccessor.GetCurrentUserId();

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

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

                if (userExam.EndedAt.HasValue)
                {
                    throw new UserExamAlreadyEndedException();
                }

                if (await UserExam.UserExamHelper.EndUserExamIfTimeExpired(context, cancellationToken, userExam))
                {
                    throw new UserExamAlreadyEndedException();
                }

                var question = await context.Questions.Where(e => e.Id == requestQA.QuestionId && e.ExamId == userExam.ExamId).FirstOrDefaultAsync();

                if (question == null)
                {
                    throw new QuestionNotFoundException();
                }

                if (question.QuestionTypeId == SeedData.QuestionTypes.Radio.Id && answerOptionIds.Count() > 1)
                {
                    throw new AnswerToRadioQuestionFormatException();
                }

                //answer options validation
                foreach (var answerOptionId in answerOptionIds)
                {
                    var answerOption = await this.context.AnswerOptions.Include(e => e.Question).Where(e => e.Id == answerOptionId).FirstOrDefaultAsync(cancellationToken);

                    if (answerOption == null)
                    {
                        throw new AnswerOptionNotFoundException(answerOptionId);
                    }
                    ;

                    if (answerOption.Question.ExamId != userExam.ExamId)
                    {
                        throw new InvalidAnswerOptionExamException(answerOptionId);
                    }
                }

                var userExamQuestion = await this.context.UserExamQuestions
                                       .Where(e => e.QuestionId == requestQA.QuestionId &&
                                              e.UserExamId == requestQA.UserExamId).Include(e => e.UserExamQuestionAnswers)
                                       .FirstOrDefaultAsync(cancellationToken);

                var isCreate = userExamQuestion == null;
                var utcNow   = DateTime.UtcNow;

                userExamQuestion = userExamQuestion ?? new UserExamQuestion()
                {
                    UserExamId = requestQA.UserExamId,
                    QuestionId = requestQA.QuestionId,
                    CreatedAt  = utcNow,
                    UpdatedAt  = utcNow
                };

                var isUnchanged = !isCreate && userExamQuestion.UserExamQuestionAnswers
                                  .Select(e => e.AnswerOptionId).OrderBy(e => e).ToArray()
                                  .SequenceEqual(answerOptionIds.OrderBy(e => e).ToArray());

                if (isUnchanged)
                {
                    return(MakeEnvelope(userExamQuestion));
                }

                if (isCreate)
                {
                    var answers = answerOptionIds.Select(answerOptionId => new UserExamQuestionAnswr()
                    {
                        UserExamId     = requestQA.UserExamId,
                        QuestionId     = requestQA.QuestionId,
                        AnswerOptionId = answerOptionId,
                        CreatedAt      = utcNow,
                        UpdatedAt      = utcNow
                    }).ToList();
                    await this.context.UserExamQuestions.AddAsync(userExamQuestion);

                    await this.context.UserExamQuestionAnswers.AddRangeAsync(answers);
                }
                else
                {
                    var answersToDelete = userExamQuestion.UserExamQuestionAnswers.Where(qa => !answerOptionIds.Any(id => id == qa.AnswerOptionId)).ToArray();
                    var answersToAdd    = answerOptionIds.Where(id => !userExamQuestion.UserExamQuestionAnswers.Any(qa => qa.AnswerOptionId == id))
                                          .Select(answerOptionId => new UserExamQuestionAnswr()
                    {
                        UserExamId     = requestQA.UserExamId,
                        QuestionId     = requestQA.QuestionId,
                        AnswerOptionId = answerOptionId,
                        CreatedAt      = utcNow,
                        UpdatedAt      = utcNow
                    });
                    userExamQuestion.UpdatedAt = utcNow;
                    this.context.UserExamQuestionAnswers.RemoveRange(answersToDelete);
                    await this.context.UserExamQuestionAnswers.AddRangeAsync(answersToAdd);
                }
                await context.SaveChangesAsync(cancellationToken);

                context.Entry(userExamQuestion).Reload();
                foreach (var questionAnswer in userExamQuestion.UserExamQuestionAnswers)
                {
                    context.Entry(questionAnswer).Reload();
                }
                return(MakeEnvelope(userExamQuestion));
            }