Esempio n. 1
0
        /// <summary>
        /// Creates a sample quiz and add it to the Database
        /// together with a sample set of questions, answers & results.
        /// </summary>
        /// <param name="userId">the author ID</param>
        /// <param name="id">the quiz ID</param>
        /// <param name="createdDate">the quiz CreatedDate</param>
        private static void CreateSampleQuiz(ApplicationDbContext dbContext, int num, string authorId, int viewCount,
                                             int numberOfQuestions, int numberOfAnswersPerQuestion, int numberOfResults,
                                             DateTime createdDate)
        {
            var quiz = new Quiz()
            {
                UserId      = authorId,
                Title       = String.Format("Quiz {0} Title", num),
                Description = String.Format("This is a sample description for quiz {0}.", num),
                Text        = "This is a sample quiz created by the DbSeeder class for testing purposes. " +
                              "All the questions, answers & results are autogenerated as well.",
                ViewCount        = viewCount,
                CreatedDate      = createdDate,
                LastModifiedDate = createdDate
            };

            dbContext.Quizzes.Add(quiz);

            dbContext.SaveChanges();

            for (int i = 0; i < numberOfQuestions; i++)
            {
                var question = new Question()
                {
                    QuizId = quiz.Id,
                    Text   = "This is a sample question created by the DbSeeder class for testing purposes. " +
                             "All the child answers are auto-generated as well.",
                    CreatedDate      = createdDate,
                    LastModifiedDate = createdDate
                };

                dbContext.Questions.Add(question);

                dbContext.SaveChanges();

                for (int i2 = 0; i2 < numberOfAnswersPerQuestion; i2++)
                {
                    dbContext.Answers.Add(new Answer()
                    {
                        QuestionId       = question.Id,
                        Text             = "This is a sample answer created by the DbSeeder class for testing purposes. ",
                        Value            = i2,
                        CreatedDate      = createdDate,
                        LastModifiedDate = createdDate
                    });
                }
            }

            for (int i = 0; i < numberOfResults; i++)
            {
                dbContext.Results.Add(new Result()
                {
                    QuizId   = quiz.Id,
                    Text     = "This is a sample result created by the DbSeeder class for testing purposes. ",
                    MinValue = 0,
                    // max value should be equal to answers number * max answer valueData
                    MaxValue         = numberOfAnswersPerQuestion * 2,
                    CreatedDate      = createdDate,
                    LastModifiedDate = createdDate
                });
            }
            dbContext.SaveChanges();
        }
Esempio n. 2
0
        private static void CreateSampleQuiz(
            ApplicationDbContext dbContext,
            int num,
            string authorId,
            int viewCount,
            int numberOfQuestions,
            int numberOfAnswersPerQuestion,
            int numberOfResults,
            DateTime createdDate)
        {
            var quiz = new Quiz()
            {
                UserId      = authorId,
                Title       = $"Quiz {num} Title",
                Description = $"This is a sample description for quiz {num}.",
                Text        = "This is a sample quiz created by the DbSeeder class for testing purposes. " +
                              "All the questions, answers & results are auto-generated as well.",
                ViewCount        = viewCount,
                CreatedDate      = createdDate,
                LastModifiedDate = createdDate,
            };

            dbContext.Quizzes.Add(quiz);
            dbContext.SaveChanges();

            for (int i = 0; i < numberOfQuestions; i++)
            {
                var question = new Question()
                {
                    QuizId = quiz.Id,
                    Text   = "This is a sample question created by the DbSeeder class for testing purposes. " +
                             "All the child answers are auto-generated as well.",
                    CreatedDate      = createdDate,
                    LastModifiedDate = createdDate,
                };
                dbContext.Questions.Add(question);
                dbContext.SaveChanges();

                for (int j = 0; j < numberOfAnswersPerQuestion; j++)
                {
                    var a = dbContext.Answers.Add(new Answer()
                    {
                        QuestionId       = question.Id,
                        Text             = "This is a sample answer created by the DbSeeder class for testing purposes. ",
                        Value            = j,
                        CreatedDate      = createdDate,
                        LastModifiedDate = createdDate,
                    });
                }
            }

            for (int i = 0; i < numberOfResults; i++)
            {
                dbContext.Results.Add(new Result()
                {
                    QuizId           = quiz.Id,
                    Text             = "This is a sample result created by the DbSeeder class for testing purposes. ",
                    MinValue         = 0,
                    MaxValue         = numberOfAnswersPerQuestion * 2,
                    CreatedDate      = createdDate,
                    LastModifiedDate = createdDate,
                });
            }
            dbContext.SaveChanges();
        }