public async Task <bool> EditTestQuestion(int id, EditQuestionDto editQuestionDto)
        {
            var question = await testQuestionRepository.GetQuestion(id);

            question.Question = editQuestionDto.Question;
            var answers = await answersRepository.GetAnswersForQuestion(question.NumberOfIdentification);

            List <string> editAnswers = new List <string>();

            editAnswers.Add(editQuestionDto.OptionA);
            editAnswers.Add(editQuestionDto.OptionB);
            editAnswers.Add(editQuestionDto.OptionC);
            List <bool> correctAnswer = new List <bool>();

            correctAnswer.Add(editQuestionDto.isCorrectA);
            correctAnswer.Add(editQuestionDto.isCorrectB);
            correctAnswer.Add(editQuestionDto.isCorrectC);
            for (int i = 0; i < editAnswers.Count; i++)
            {
                answers[i].Option    = editAnswers[i];
                answers[i].isCorrect = correctAnswer[i];
            }
            if (question == null || editQuestionDto.Question == "")
            {
                return(false);
            }
            testQuestionRepository.Update(question);
            await testQuestionRepository.SaveChangesAsync();

            return(true);
        }
        public async Task <IActionResult> EditQuestion(string id, EditQuestionDto editQuestionDto)
        {
            var question = await testQuestionService.EditTestQuestion(Int32.Parse(id), editQuestionDto);

            if (question == false)
            {
                return(NotFound(resourceManager.GetString("Null")));
            }
            return(Ok());
        }
Esempio n. 3
0
        public Guid AddQuestion(EditQuestionDto questionDto, Guid testId)
        {
            var question = new Question()
            {
                Id          = Guid.NewGuid(),
                Description = questionDto.Description,
                TestId      = testId,
                CreatedOn   = DateTime.Now
            };

            this.questionRepo.Add(question);
            return(question.Id);
        }
        public Result ValidateEditQuestionDto(EditQuestionDto questionDto)
        {
            if (string.IsNullOrWhiteSpace(questionDto.QuestionText))
            {
                return(Result.Fail(-3));
            }

            if (!questionDto.Answers.Any())
            {
                return(Result.Fail(-4));
            }

            return(answerValidator.ValidateEditAnswerDtos(questionDto.Answers));
        }
Esempio n. 5
0
        public void EditQuestion(EditQuestionDto questionDto, Guid testId)
        {
            Guard.WhenArgument(questionDto, "questionDto").IsNull().Throw();

            if (questionDto.Id == null)
            {
                Guid questionId = this.AddQuestion(questionDto, testId);
                foreach (var answer in questionDto.Answers)
                {
                    this.answerService.AddAnswer(answer, questionId);
                }
            }
            else
            {
                var entity = questionRepo.All
                             .Where(q => q.Id.ToString().Equals(questionDto.Id))
                             .Include(q => q.Answers)
                             .FirstOrDefault();

                foreach (var answer in entity.Answers)
                {
                    if (questionDto.Answers.Any(x => x.Id == answer.Id.ToString()))
                    {
                        continue;
                    }
                    this.answerService.DeleteAnswer(answer);
                }

                foreach (var answer in questionDto.Answers)
                {
                    this.answerService.EditAnswer(answer, entity.Id);
                }

                entity.Description = questionDto.Description;
                entity.ModifiedOn  = DateTime.Now;

                this.questionRepo.Update(entity);
            }
        }
        public async Task <EditQuestionDto> GetTestQuestion(int questionId)
        {
            var testQuestion = await testQuestionRepository.GetQuestion(questionId);

            var answers = await answersRepository.GetAnswersForQuestion(testQuestion.NumberOfIdentification);

            if (testQuestion == null || answers == null)
            {
                return(null);
            }
            var editModel = new EditQuestionDto()
            {
                Question   = testQuestion.Question,
                OptionA    = answers[0].Option,
                OptionB    = answers[1].Option,
                OptionC    = answers[2].Option,
                isCorrectA = answers[0].isCorrect,
                isCorrectB = answers[1].isCorrect,
                isCorrectC = answers[2].isCorrect
            };

            return(editModel);
        }