Example #1
0
        public async Task <bool> CreateQuiz(Models.NewQuizModel newQuiz)
        {
            var quiz = new Data.Quiz
            {
                Title     = newQuiz.Title,
                Questions = new List <Data.Question>()
            };

            foreach (var question in newQuiz.NewQuestions)
            {
                var questionToAdd = new Data.Question
                {
                    QuestionString = question.Question,
                    CorrectAnswer  = new Answer {
                        AnswerText = question.CorrectAnswer
                    },
                    WrongAnswers = new WrongAnswers()
                    {
                        Answers = new List <Answer>()
                    }
                };

                foreach (var wrongAnswer in question.WrongAnswers)
                {
                    var wrongAnswerToAdd = new Answer
                    {
                        AnswerText = wrongAnswer
                    };

                    questionToAdd.WrongAnswers.Answers.Add(wrongAnswerToAdd);
                }
                ;

                quiz.Questions.Add(questionToAdd);
            }
            ;


            try
            {
                await _quizManagerContext.Quizzes.AddAsync(quiz);

                await _quizManagerContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
 private async Task AddQuizToDatabase()
 {
     _quizManagerContext.Quizzes.Add(new Quiz()
     {
         Id = 1, Title = "TestQuizTitle"
     });
     _quizManagerContext.Questions.Add(new Question()
     {
         Id = 1, QuizId = 1, Text = "TestQuestionText"
     });
     _quizManagerContext.Answers.Add(new Answer()
     {
         Id = 1, QuestionId = 1, Text = "TestAnswerText"
     });
     await _quizManagerContext.SaveChangesAsync();
 }