Exemplo n.º 1
0
        public async Task Initialize()
        {
            var emptyFilter = new BsonDocument();
            var players     = await GetAllAsync <Player>(emptyFilter);

            if (!players.Any())
            {
                players = _modelFactory.CreatePlayers(100);
                await SaveAsync(players);
            }

            var categories = await GetAllAsync <Category>(emptyFilter);

            if (!categories.Any())
            {
                categories = _modelFactory.CreateCategories(10);
                await SaveAsync(categories);
            }

            foreach (var category in categories)
            {
                var categoryFilter    = new BsonDocument(nameof(Question.Category), category.Name);
                var categoryQuestions = await GetAllAsync <Question>(categoryFilter);

                if (!categoryQuestions.Any())
                {
                    var questionIds = new List <ObjectId>();
                    for (int i = 0; i < 50; i++)
                    {
                        var newQuestionWithAnswers = _modelFactory.CreateQuestionWithAnswers(i, category.Name);
                        await SaveAsync(newQuestionWithAnswers.answers);

                        newQuestionWithAnswers.question.Answers = newQuestionWithAnswers.answers.Select(answer => answer.Id);
                        await SaveAsync(newQuestionWithAnswers.question);

                        questionIds.Add(newQuestionWithAnswers.question.Id);
                    }

                    category.Questions = questionIds;
                    await UpdateAsync(category);
                }
            }
        }