示例#1
0
        /// <summary>
        /// Creates new test
        /// </summary>
        /// <param name="test"></param>
        /// <returns>Returns TestId of supplied test</returns>
        public async Task <int> CreateTestAsync(TestDto test)
        {
            ThrowIfTestIsInvalid(test);

            Test newTest = new Test();

            test.UpdateEntity(newTest);

            List <Question> testQuestions = new List <Question>(test.Questions.Count);

            foreach (QuestionDto questionDto in test.Questions)
            {
                ThrowIfQuestionIsInvalid(questionDto);

                // if questions for passing is zero, than it is like quiz. Whatewer you will pass is.
                // if questons count is zero,  than current test should contains them all

                // questions count in test.
                if (test.QuestionsCount == 0)
                {
                    newTest.QuestionsCount++;
                }

                Question question = new Question();
                questionDto.UpdateEntity(question);
                if (questionDto.QuestionTypeId == QuestionTypeEnum.Closed)
                {
                    List <QuestionAnswerOption> options = new List <QuestionAnswerOption>(questionDto.Options.Count);
                    foreach (QuestionAnswerOptionDto answerOption in questionDto.Options)
                    {
                        QuestionAnswerOption option = new QuestionAnswerOption();
                        answerOption.UpdateEntity(option);
                        options.Add(option);
                    }

                    question.QuestionAnswerOptions = options;
                }

                testQuestions.Add(question);
            }

            testQuestions.ForEach(q => newTest.Questions.Add(q));

            context.Tests.Add(newTest);
            await context.SaveChangesAsync();

            return(newTest.Id);
        }