Пример #1
0
        public IResult <IEnumerable <QuestionAnswers> > GetNumberOfQuestionsByChapter(string chapterName, int numberOfQuestions)
        {
            var list   = new Dictionary <int, QuestionAnswers>();
            var result = new Result <IEnumerable <QuestionAnswers> >();

            try
            {
                using (SqlConnection connection = new SqlConnection())
                {
                    connection.ConnectionString = _connectionString;
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection  = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "GetNumberOfQuestionsByChapter";
                        command.Parameters.AddWithValue("@ChapterName", chapterName);
                        command.Parameters.AddWithValue("@numberOfQuestions", numberOfQuestions);
                        connection.Open();
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            QuestionAnswers questionAnswers = new QuestionAnswers();
                            questionAnswers.SetQuestion(reader["Question"].ToString());
                            int questionId = (int)reader["QuizId"];
                            list[questionId] = questionAnswers;
                        }
                    }
                }
                foreach (var keyValuePair in list)
                {
                    keyValuePair.Value.SetAnswers(GetAnswersForQuestion(keyValuePair.Key));
                }
                result.IsSucess = true;
                result.Data     = list.Values;
            }
            catch (Exception ex)
            {
                result.IsSucess     = false;
                result.ErrorMessage = ex.Message;
            }
            return(result);
        }
        public IResult <IEnumerable <QuestionAnswers> > GetQuestionsByChapter(string chapterName)
        {
            IResult <IEnumerable <QuestionAnswers> > result = new Result <IEnumerable <QuestionAnswers> >();

            if (_fileData == null)
            {
                throw new ArgumentNullException();
            }
            try
            {
                TextReader textReader          = new StringReader(_fileData);
                var        doc                 = XDocument.Load(textReader);
                var        questionAnswersList = doc.Descendants("Chapter")
                                                 .Where(x => x.Attribute("ChapterName").Value == chapterName)
                                                 .Elements("Quiz").OrderBy(x => Guid.NewGuid()).ToList();
                var list = new List <QuestionAnswers>();
                foreach (var questionAnswers in questionAnswersList)
                {
                    QuestionAnswers questionAnswer = new QuestionAnswers();
                    questionAnswer.SetQuestion(questionAnswers.Attribute("Question").Value);
                    List <Answer> answers = new List <Answer>();
                    var           ansList = questionAnswers.Elements("Answer")
                                            .Select(a => new Answer()
                    {
                        AnswerValue = a.Attribute("Answer").Value,
                        IsCorrect   = int.Parse(a.Attribute("IsCorrectAnswer").Value) == 1 ? true : false
                    }).ToList();
                    questionAnswer.SetAnswers(ansList.OrderBy(x => Guid.NewGuid()).Take(ansList.Count).ToList());

                    list.Add(questionAnswer);
                }
                result.Data     = list.ToList();
                result.IsSucess = true;
            }
            catch (Exception ex)
            {
                result.IsSucess     = false;
                result.ErrorMessage = ex.Message;
            }
            return(result);
        }
Пример #3
0
        private List <QuestionAnswers> LoadQuestionAnswers()
        {
            List <QuestionAnswers> questionAnswers = new List <QuestionAnswers>();
            QuestionAnswers        question1       = new QuestionAnswers();

            question1.SetQuestion("What is name of the capital of Ontrario?");
            question1.Result = true;
            question1.SetAnswers(
                new List <Answer>()
            {
                new Answer()
                {
                    IsCorrect = true, AnswerValue = "Toronto"
                },
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "Qubec"
                },
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "Minitoba"
                },
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "New Brunswick"
                }
            });

            QuestionAnswers question2 = new QuestionAnswers();

            question2.SetQuestion("When was Magna Carta siged?");
            question2.Result = true;
            question2.SetAnswers(
                new List <Answer>()
            {
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "1218"
                },
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "1261"
                },
                new Answer()
                {
                    IsCorrect = true, AnswerValue = "1215"
                },
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "1230"
                }
            });

            QuestionAnswers question3 = new QuestionAnswers();

            question3.SetQuestion("Who is the prime minister of Canada?");
            question3.Result = false;
            question3.SetAnswers(
                new List <Answer>()
            {
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "John Wich"
                },
                new Answer()
                {
                    IsCorrect = true, AnswerValue = "Justin Trudu"
                },
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "John Terry"
                },
                new Answer()
                {
                    IsCorrect = false, AnswerValue = "Total Stranger"
                }
            });

            questionAnswers.Add(question1);
            questionAnswers.Add(question2);
            questionAnswers.Add(question3);
            return(questionAnswers);
        }
Пример #4
0
 public void ShouldHaveAQuestion()
 {
     sut.SetQuestion("Whats My Name?");
     Assert.IsNotNull(sut.Question);
 }