public async Task SeedAsync() { // Seed User if (await _userMgr.FindByNameAsync("shawnwildermuth") == null) { var user = new WilderUser() { Email = "*****@*****.**", UserName = "******", EmailConfirmed = true }; var result = await _userMgr.CreateAsync(user, "P@ssw0rd!"); // Temp Password if (!result.Succeeded) { throw new InvalidProgramException("Failed to create seed user"); } } // Seed Stories if (!_ctx.Stories.Any()) { var stories = MemoryRepository._stories; stories.ForEach(s => s.Id = 0); _ctx.Stories.AddRange(stories); await _ctx.SaveChangesAsync(); } }
public async Task InitializeAsync() { await _ctx.Database.EnsureCreatedAsync(); if (!_ctx.Stories.Any()) { // Create Data if (_environ.IsDevelopment()) { var user = new WilderUser() { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true }; await _mgr.CreateAsync(user, "P@ssw0rd!"); var story = new Story() { Body = "<p>Test Story</p>", DatePosted = DateTime.UtcNow, IsPublished = true, Permalink = Guid.NewGuid().ToString(), Title = "Welcome to My Blog" }; _ctx.Stories.Add(story); await _ctx.SaveChangesAsync(); } } }
public async Task SeedAsync() { // Seed User if (await _userMgr.FindByNameAsync("shawnwildermuth") == null) { var user = new WilderUser() { Email = "*****@*****.**", UserName = "******", EmailConfirmed = true }; var result = await _userMgr.CreateAsync(user, "P@ssw0rd!"); // Temp Password if (!result.Succeeded) { throw new InvalidProgramException("Failed to create seed user"); } } // Seed Stories if (!_ctx.Stories.Any()) { var stories = MemoryRepository._stories; stories.ForEach(s => s.Id = 0); _ctx.Stories.AddRange(stories); await _ctx.SaveChangesAsync(); } // Update Abstract and Image if (!_ctx.Stories.Any(s => !string.IsNullOrEmpty(s.Abstract))) { var stories = await _ctx.Stories.ToListAsync(); foreach (var story in stories) { // Get Image var summary = GetSummary(story); var regex = new Regex("\\<img.+src\\=(?:\"|\')(.+?)(?:\"|\')(?:.+?)\\>", RegexOptions.IgnoreCase | RegexOptions.Singleline); var result = regex.Matches(summary); if (result.Any()) { var firstResult = result.First(); var rawImg = firstResult.Groups[0].Value; var url = firstResult.Groups[1].Value; story.FeatureImageUrl = url; story.Body = story.Body.Replace(rawImg, ""); story.Abstract = summary.Replace(rawImg, ""); _logger.LogDebug($"Updating: {story.Title}"); } } if (!(await _ctx.SaveChangesAsync() > 0)) { _logger.LogError("Failed to save changes to image and abstracts"); } _logger.LogDebug($"Done Updating"); } }
public async Task SeedAsync() { // Seed User if (await _userMgr.FindByNameAsync("shawnwildermuth") == null) { var user = new WilderUser() { Email = "*****@*****.**", UserName = "******", EmailConfirmed = true }; var result = await _userMgr.CreateAsync(user, "P@ssw0rd!"); // Temp Password if (!result.Succeeded) { throw new InvalidProgramException("Failed to create seed user"); } } }