Ejemplo n.º 1
0
        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");
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }
Ejemplo n.º 3
0
 public async Task <bool> SaveAllAsync()
 {
     return(await _ctx.SaveChangesAsync() > 0);
 }