コード例 #1
0
        public async Task <ErrorTestDto> ReadTestQuestions(int id, UploadFile uploadFile)
        {
            var type_test = await testRepository.GetTest(id);

            var error = new ErrorTestDto();

            if ((int)type_test.TypeOfTest == 0)
            {
                error = await ReadMultipleTestQuestions(id, uploadFile);
            }
            if ((int)type_test.TypeOfTest == 1)
            {
                error = await ReadSingleTestQuestions(id, uploadFile);
            }
            return(error);
        }
コード例 #2
0
        public async Task <ErrorTestDto> ReadMultipleTestQuestions(int id, UploadFile uploadFile)
        {
            ErrorTestDto errorTestDto      = new ErrorTestDto();
            var          files             = uploadFile.formFile;
            var          list_testQuestion = new List <TestQuestions>();
            var          list_answer       = new List <Answers>();
            var          options           = new List <string>();
            var          test = await testRepository.GetTest(id);

            CultureInfo culture1 = CultureInfo.CurrentCulture;

            using (TextReader reader = new StreamReader(files.OpenReadStream(), Encoding.UTF8))
                using (var csv = new CsvReader(reader, culture1))
                {
                    foreach (var i in csv.GetRecords <ReadMultipleTestDto>())
                    {
                        if (i.Question == "" || i.OptionA == "" ||
                            i.OptionB == "" || i.OptionC == "" || i.Complexity == "" ||
                            i.FirstAnswer == "" || i.SecondAnswer == "" ||
                            i.ThirdAnswer == "")
                        {
                            errorTestDto.FieldEmpty = 400;
                            return(errorTestDto);
                        }
                        if (test.AutomaticTime == true)
                        {
                            var testTime = 0;
                            if (i.Complexity == "1")
                            {
                                testTime += 1;
                            }
                            else if (i.Complexity == "2")
                            {
                                testTime += 3;
                            }
                            else if (i.Complexity == "3")
                            {
                                testTime += 5;
                            }
                            test.TimeOfTest += testTime;
                            testRepository.Update(test);
                            await testRepository.SaveChangesAsync();
                        }
                        var testQuestion = new TestQuestions(test.Id, test.SubjectId, i.Question, Int32.Parse(i.Complexity));
                        ///
                        list_testQuestion.Add(testQuestion);

                        var answer_one = new Answers(test.SubjectId, test.Id, testQuestion.NumberOfIdentification, i.OptionA, bool.Parse(i.FirstAnswer));
                        list_answer.Add(answer_one);

                        var answer_two = new Answers(test.SubjectId, test.Id, testQuestion.NumberOfIdentification, i.OptionB, bool.Parse(i.SecondAnswer));
                        list_answer.Add(answer_two);

                        var answer_third = new Answers(test.SubjectId, test.Id, testQuestion.NumberOfIdentification, i.OptionC, bool.Parse(i.ThirdAnswer));
                        list_answer.Add(answer_third);

                        list_answer.Add(answer_one);
                        list_answer.Add(answer_two);
                        list_answer.Add(answer_third);
                    }
                    if (list_testQuestion.Count == 0)
                    {
                        errorTestDto.FieldEmpty = 400;
                        return(errorTestDto);
                    }
                    testQuestionRepository.AddTestsQuestions(list_testQuestion);
                    answersRepository.SaveAnswers(list_answer);
                    return(errorTestDto);
                }
        }