Ejemplo n.º 1
0
        private static void CreateSampleTest(EfDbContext context,
                                             int num,
                                             string authorId,
                                             int viewCount,
                                             int numberOfQuestions,
                                             int numberOfAnswersPerQuestions,
                                             int numberOfResults,
                                             DateTime createdDate)
        {
            var test = new Test
            {
                UserId      = authorId,
                Title       = $"Title of the test {num}",
                Description = $"This is sample test {num}",
                Text        = $"This is sample test created using DbSeeder class. All questions, answers and results were generated " +
                              $"using this class as well.",
                ViewCount            = viewCount,
                CreationDate         = createdDate,
                LastModificationDate = createdDate
            };

            context.Tests.Add(test);
            context.SaveChanges();

            for (int i = 0; i < numberOfQuestions; i++)
            {
                var question = new Question
                {
                    TestId               = test.Id,
                    Text                 = $"Sample qeustion {i + 1}",
                    CreationDate         = createdDate,
                    LastModificationDate = createdDate
                };

                context.Questions.Add(question);
                context.SaveChanges();

                for (int j = 0; j < numberOfAnswersPerQuestions; j++)
                {
                    var a1 = context.Answers.Add(new Answer
                    {
                        QuestionId           = question.Id,
                        Text                 = $"Sample answer {j + 1}",
                        Value                = j,
                        CreationDate         = createdDate,
                        LastModificationDate = createdDate
                    });
                }
            }

            for (int i = 0; i < numberOfResults; i++)
            {
                context.Results.Add(new Result
                {
                    TestId               = test.Id,
                    Text                 = $"Sample result {i + 1}",
                    MinValue             = 0,
                    MaxValue             = numberOfQuestions + 2,
                    CreationDate         = createdDate,
                    LastModificationDate = createdDate
                });
            }

            context.SaveChanges();
        }
Ejemplo n.º 2
0
        private static async Task CreateUsers(EfDbContext context,
                                              RoleManager <IdentityRole> roleManager,
                                              UserManager <ApplicationUser> userManager)
        {
            DateTime creationDate     = new DateTime(2018, 03, 01, 12, 00, 00);
            DateTime modificationDate = DateTime.Now;

            if (!await roleManager.RoleExistsAsync(UserRoles.Administrator))
            {
                await roleManager.CreateAsync(new IdentityRole(UserRoles.Administrator));
            }
            if (!await roleManager.RoleExistsAsync(UserRoles.RegisteredUser))
            {
                await roleManager.CreateAsync(new IdentityRole(UserRoles.RegisteredUser));
            }

            var userAdmin = new ApplicationUser
            {
                SecurityStamp        = Guid.NewGuid().ToString(),
                UserName             = "******",
                Email                = "*****@*****.**",
                CreationDate         = creationDate,
                LastModificationDate = modificationDate
            };

            if (await userManager.FindByNameAsync(userAdmin.UserName) == null)
            {
                await userManager.CreateAsync(userAdmin, "Admin123+");

                await userManager.AddToRoleAsync(userAdmin, UserRoles.RegisteredUser);

                await userManager.AddToRoleAsync(userAdmin, UserRoles.Administrator);

                //remove lock
                userAdmin.EmailConfirmed = true;
                userAdmin.LockoutEnabled = false;
            }


#if DEBUG
            var normalUser = new ApplicationUser
            {
                SecurityStamp        = Guid.NewGuid().ToString(),
                UserName             = "******",
                Email                = "*****@*****.**",
                CreationDate         = creationDate,
                LastModificationDate = modificationDate
            };

            if (await userManager.FindByNameAsync(normalUser.UserName) == null)
            {
                await userManager.CreateAsync(normalUser, "User123+");

                await userManager.AddToRoleAsync(normalUser, UserRoles.RegisteredUser);

                //remove lock
                userAdmin.EmailConfirmed = true;
                userAdmin.LockoutEnabled = false;
            }
#endif

            await context.SaveChangesAsync();
        }