예제 #1
0
        public void CreateQuestionAnswersTest()
        {
            using (var _Transaction = _DbContext.Database.BeginTransaction())
            {
                // Создаем вопрос.
                DAO.Question _Question = new DAO.Question()
                {
                    Text = "CreateQuestionAnswersTest?"
                };
                _QuestionManagement.CreateQuestion(_Question);
                _DbContext.SaveChanges();

                // Получаем сущность из БД - если не получить, то EF подумает, что надо создать новую.
                _Question = _DbContext.Questions.Include("Answers").Single(x => x.Text == _Question.Text);

                // Выбираем ответы для заполнения.
                DAO.Answer[] _Answers = new DAO.Answer[] {
                    _DbContext.Answers.Single(x => x.Text == "Венера"),
                    _DbContext.Answers.Single(x => x.Text == "Меркурий"),
                    _DbContext.Answers.Single(x => x.Text == "Земля")
                };
                _QuestionManagement.AddAnswer(_Question, _Answers);
                _DbContext.SaveChanges();

                Assert.True(_Question.Answers.Count() == 3);

                _Transaction.Rollback();
            }
        }
예제 #2
0
        public void SetCorrectAnswerTest()
        {
            using (var _Transaction = _DbContext.Database.BeginTransaction())
            {
                // Создаем вопрос.
                DAO.Question _Question = new DAO.Question()
                {
                    Text = "SetCorrectAnswerTest?"
                };
                _QuestionManagement.CreateQuestion(_Question);
                _DbContext.SaveChanges();

                // Получаем сущность из БД - если не получить, то EF подумает, что надо создать новую.
                _Question = _DbContext.Questions.Include("Answers").Single(x => x.Text == _Question.Text);

                // Выбираем ответы для заполнения.
                DAO.Answer[] _Answers = new DAO.Answer[] {
                    _DbContext.Answers.Single(x => x.Text == "Венера"),
                    _DbContext.Answers.Single(x => x.Text == "Меркурий"),
                    _DbContext.Answers.Single(x => x.Text == "Земля")
                };
                _QuestionManagement.AddAnswer(_Question, _Answers);
                _DbContext.SaveChanges();

                Assert.DoesNotThrow(() => _QuestionManagement.SetCorrectAnswer(_Question, _DbContext.Answers.Single(x => x.Text == "Венера")));

                var _UnCorrectAnswer = _DbContext.Answers.Single(x => x.Text == "101010");
                Assert.Throws <Exception>(() => _QuestionManagement.SetCorrectAnswer(_Question, _UnCorrectAnswer));

                _Transaction.Rollback();
            }
        }
        public string AnswerTheQuestion(DTO.InterviweeTest DtoInterviweeTest)
        {
            DTO.InterviweeTest _DtoInterviweeTest = DtoInterviweeTest;

            // Прохождение теста.
            DAO.InterviweeTests _DaoInterviweeTest = GetInterviweeTestByDtoId(ref _DtoInterviweeTest);

            // Текущий вопрос.
            DAO.Question _CurrentQuestion = m_QuestionManagement.GetQuestion(_DtoInterviweeTest.CurrentQuestion.Id);

            // Ответ на вопрос.
            DAO.Answer _Answer = m_AnswerManagement.GetAnswer(_DtoInterviweeTest.CurrentQuestion.SelectedAnswerId);
            m_Testing.AnswerToQuestion(_DaoInterviweeTest, _CurrentQuestion, _Answer);
            // Определим завершенность теста.
            m_Testing.DetermineStatusComplete(_DaoInterviweeTest);

            // Прохождение теста - обновление после ответа.
            _DaoInterviweeTest = GetInterviweeTestByDtoId(ref _DtoInterviweeTest);

            // Вопрос.
            _DtoInterviweeTest = GetNextQuestion(_DtoInterviweeTest, _DaoInterviweeTest, _CurrentQuestion);

            string _Json = Utils.JsonSerialize(_DtoInterviweeTest);

            return(_Json);
        }
        public string AddAnswerToQuestion(DTO.QuestionAnswers QuestionAnswer)
        {
            DAO.Question _Question = m_QuestionManagement.GetQuestion(QuestionAnswer.QuestionId);
            DAO.Answer   _Answer   = m_AnswerManagement.GetAnswer(QuestionAnswer.AnswerId);

            m_QuestionManagement.AddAnswer(_Question, _Answer);
            return(string.Empty);
        }
        public string SetCorrectAnswer(DTO.QuestionAnswers CorrectAnswer)
        {
            DAO.Question _Question = m_QuestionManagement.GetQuestion(CorrectAnswer.QuestionId);
            DAO.Answer   _Answer   = m_AnswerManagement.GetAnswer(CorrectAnswer.AnswerId);

            m_QuestionManagement.SetCorrectAnswer(_Question, _Answer);
            return(string.Empty);
        }
        /// <summary>
        /// Поиск ответа в БД.
        /// </summary>
        /// <param name="Answer"></param>
        /// <returns></returns>
        ///
        private bool IsExist(DAO.Answer Answer)
        {
            DAO.Answer _FinKey   = GetAnswer(Answer.Id);
            DAO.Answer _FindText = GetAnswer(Answer.Text);

            bool _IsExist = _FinKey != null || _FindText != null;

            return(_IsExist);
        }
        public void GetAnswerTest()
        {
            using (var _Transaction = _DbContext.Database.BeginTransaction())
            {
                DAO.Answer _Answer = _AnswerManagement.GetAnswer(5);

                Assert.True(_Answer.Id == 5);

                _Transaction.Rollback();
            }
        }
        /// <summary>
        /// Создать вопрос.
        /// </summary>
        /// <param name="Text">Текст вопроса.</param>
        public void CreateAnswer(DAO.Answer Answer)
        {
            if (Answer.Text.Replace(" ", "").Length <= 0)
            {
                return;
            }
            if (IsExist(Answer))
            {
                return;
            }

            _DbContext.Answers.Add(Answer);
            _DbContext.SaveChanges();
        }
        /// <summary>
        /// Установить правильный ответ на вопрос.
        /// </summary>
        public void SetCorrectAnswer(DAO.Question Question, DAO.Answer Answer)
        {
            DAO.QuestionAnswers _QuestionAnswer = _DbContext.QuestionAnswers.Find(Question.Id, Answer.Id);
            if (_QuestionAnswer == null)
            {
                throw new Exception("Правильный ответ должен содержаться в вопросе.");
            }

            DAO.QuestionAnswers _CurrentCorrectAnswer = _DbContext.QuestionAnswers.SingleOrDefault(x => x.QuestionId == Question.Id & x.IsCorrect);
            if (_CurrentCorrectAnswer != null)
            {
                _CurrentCorrectAnswer.IsCorrect = false;
            }

            _QuestionAnswer.IsCorrect = true;
            _DbContext.SaveChanges();
        }
        public string CreateAnswerToQuestion(DTO.NewAnswerToQuestion NewAnswerToQuestion)
        {
            DAO.Answer _Answer = new DAO.Answer()
            {
                Text = NewAnswerToQuestion.AnswerText
            };
            DAO.Question _Question = m_QuestionManagement.GetQuestion(NewAnswerToQuestion.QuestionId);

            m_AnswerManagement.CreateAnswer(_Answer);

            _Answer = m_AnswerManagement.GetAnswer(NewAnswerToQuestion.AnswerText);
            if (_Answer != null)
            {
                m_QuestionManagement.AddAnswer(_Question, _Answer);
            }

            return(string.Empty);
        }
        public void CreateAnswerTest()
        {
            using (var _Transaction = _DbContext.Database.BeginTransaction())
            {
                DAO.Answer _Answer = new DAO.Answer()
                {
                    Text = "wtf?"
                };

                Assert.DoesNotThrow(() => _AnswerManagement.CreateAnswer(_Answer));
                Assert.DoesNotThrow(() => _AnswerManagement.CreateAnswer(_Answer));
                _DbContext.SaveChanges();
                // Исключаем повторное добавление.
                var _Count = _DbContext.Answers.Where(x => x.Text == _Answer.Text).Count();
                Assert.True(_Count == 1);

                _Transaction.Rollback();
            }
        }
        /// <summary>
        /// Добавить вариант ответа в вопрос.
        /// </summary>
        /// <param name="Question"></param>
        /// <param name="Answer"></param>
        public void AddAnswer(DAO.Question Question, DAO.Answer Answer)
        {
            if (Question.Answers.Where(x => x.AnswerId == Answer.Id).Count() == 1)
            {
                return;
            }
            if ((Question.Answers.Count() >= 3))
            {
                throw new Exception("Вопрос может содержать не более 3 вариантов ответа.");
            }

            DAO.QuestionAnswers _QuestionAnswers = new DAO.QuestionAnswers()
            {
                AnswerId   = Answer.Id,
                QuestionId = Question.Id,
                IsCorrect  = false
            };

            _DbContext.QuestionAnswers.Add(_QuestionAnswers);
            _DbContext.SaveChanges();
        }
예제 #13
0
        /// <summary>
        /// Ответить на вопрос.
        /// </summary>
        /// <param name="InterviweeTestId">Прохождение теста.</param>
        /// <param name="QuestionId">Вопрос.</param>
        /// <param name="AnswerId">Ответ.</param>
        public void AnswerToQuestion(DAO.InterviweeTests InterviweeTest, DAO.Question Question, DAO.Answer Answer)
        {
            // Если ответ на такой вопрос уже был - выходим.
            var ExistAnswer = _DbContext.TestingResults.Where(x =>
                                                              x.InterviweeTestsId == InterviweeTest.Id
                                                              & x.QuestionId == Question.Id).Count();

            if (ExistAnswer > 0)
            {
                return;
            }

            // Определить ответ.
            DAO.TestingResult _TestingResult = new DAO.TestingResult()
            {
                InterviweeTestsId = InterviweeTest.Id,
                QuestionId        = Question.Id,
                AnswerId          = Answer.Id,
                IsCorrect         = _DbContext.QuestionAnswers.Where(x => x.QuestionId == Question.Id & x.IsCorrect).FirstOrDefault()?.AnswerId == Answer.Id
            };

            // Добавить в результаты теста.
            _DbContext.TestingResults.Add(_TestingResult);
            _DbContext.SaveChanges();
        }