/// <summary> /// Initializes database and seeds some data /// </summary> /// <param name="context"></param> /// <param name="hashService"></param> /// <returns></returns> public static async Task Initialize(this CodingTaskContext context, IHashService hashService, IClock clock) { await context.Database.EnsureCreatedAsync(); await initializeUsers(context, hashService); await initializeMatches(context, clock); }
private static async Task <bool> SeedMatches(this CodingTaskContext context) { var currentMatches = await context.Matches.ToListAsync(); //expired matches for (int count = 1; count <= 3; count++) { if (!currentMatches.Any(u => u.MatchId == count)) { context.Matches.Add(new Models.Match { ExpiryDate = DateTime.Now.AddMinutes(0 - (count * 5)), MatchWinnerUserId = null }); } } //active matches for (int count = 1; count <= 10; count++) { if (!currentMatches.Any(u => u.MatchId == count)) { context.Matches.Add(new Models.Match { ExpiryDate = DateTime.Now.AddMinutes(count * 5), MatchWinnerUserId = null }); } } await context.SaveChangesAsync(); return(true); }
public static async Task Initialize(this CodingTaskContext context, IHashService hashService) { await context.Database.EnsureCreatedAsync(); await SeedUsers(context, hashService); await SeedMatches(context); }
private static async Task <bool> SeedUsers(this CodingTaskContext context, IHashService hashService) { var currentUsers = await context.Users.ToListAsync(); bool anyNewUser = false; if (!currentUsers.Any(u => u.UserName == "User1")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password1") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User2")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password2") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User3")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password3") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User4")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password4") }); anyNewUser = true; } if (anyNewUser) { await context.SaveChangesAsync(); } return(true); }
public static async Task Initialize(this CodingTaskContext context, IHashService hashService) { await context.Database.EnsureCreatedAsync(); var currentUsers = await context.Users.ToListAsync(); bool anyNewUser = false; if (!currentUsers.Any(u => u.UserName == "User1")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password1") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User2")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password2") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User3")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password3") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User4")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password4") }); anyNewUser = true; } if (anyNewUser) { await context.SaveChangesAsync(); } }
// seeds some sample users // todo: remove this in production private static async Task initializeUsers(CodingTaskContext context, IHashService hashService) { var currentUsers = await context.Users.ToListAsync(); bool anyNewUser = false; if (currentUsers.All(u => u.UserName != "User1")) { await context.Users.AddAsync(new Models.User { UserName = "******", Password = await hashService.HashText("Password1") }); anyNewUser = true; } if (currentUsers.All(u => u.UserName != "User2")) { await context.Users.AddAsync(new Models.User { UserName = "******", Password = await hashService.HashText("Password2") }); anyNewUser = true; } if (currentUsers.All(u => u.UserName != "User3")) { await context.Users.AddAsync(new Models.User { UserName = "******", Password = await hashService.HashText("Password3") }); anyNewUser = true; } if (currentUsers.All(u => u.UserName != "User4")) { await context.Users.AddAsync(new Models.User { UserName = "******", Password = await hashService.HashText("Password4") }); anyNewUser = true; } if (anyNewUser) { await context.SaveChangesAsync(); } }
// seeds some sample matches (each 10 seconds there will be a match) // todo: remove this in production private static async Task initializeMatches(CodingTaskContext context, IClock clock) { if (await context.Matches.AnyAsync()) { return; } var now = clock.Now(); for (var i = 0; i < 1000; i++) { await context.Matches.AddRangeAsync(new Match { Id = Guid.NewGuid(), ExpiresTimestamp = now.AddSeconds((i + 1) * 10).ToUnixTime() }); } await context.SaveChangesAsync(); }
public static async Task Initialize(this CodingTaskContext context, IHashService hashService, IUnitOfWork uow) { await context.Database.EnsureCreatedAsync(); var currentUsers = await context.Users.ToListAsync(); bool anyNewUser = false; if (!currentUsers.Any(u => u.UserName == "User1")) { context.Users.Add(new User { UserName = "******", Password = await hashService.HashText("Password1") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User2")) { context.Users.Add(new User { UserName = "******", Password = await hashService.HashText("Password2") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User3")) { context.Users.Add(new User { UserName = "******", Password = await hashService.HashText("Password3") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User4")) { context.Users.Add(new User { UserName = "******", Password = await hashService.HashText("Password4") }); anyNewUser = true; } if (await context.Matches.AnyAsync()) { return; } for (var i = 0; i < 200; i++) { await context.Matches.AddRangeAsync(new Match { Id = Guid.NewGuid(), ExpTimestamp = (int)(DateTime.Now.ToUniversalTime().AddSeconds(i *DateTime.Now.Millisecond) - new DateTime(1970, 1, 1)).TotalSeconds }); } if (anyNewUser) { await uow.Commit(); } }
public static async Task Initialize(this CodingTaskContext context, IHashService hashService) { await context.Database.EnsureCreatedAsync(); var currentUsers = await context.Users.ToListAsync(); bool anyNewUser = false; if (!currentUsers.Any(u => u.UserName == "User1")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password1") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User2")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password2") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User3")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password3") }); anyNewUser = true; } if (!currentUsers.Any(u => u.UserName == "User4")) { context.Users.Add(new Models.User { UserName = "******", Password = await hashService.HashText("Password4") }); anyNewUser = true; } bool newMatchesAdded = false; if (context.Matches.Any()) { DateTime startDate = DateTime.UtcNow; for (int i = 0; i < 3; i++) { await context.Matches.AddAsync(new Models.Match() { StartDate = startDate, EndDate = startDate.AddMinutes(10), Status = i == 0 ? Enums.MatchStatus.Active : Enums.MatchStatus.NotStarted }); } newMatchesAdded = true; } if (anyNewUser || newMatchesAdded) { await context.SaveChangesAsync(); } }