Example #1
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (RedditPostContext context = new RedditPostContext(serviceProvider.GetRequiredService <DbContextOptions <RedditPostContext> >()))
            {
                //if (context.RedditPost.Any())
                //{
                //    return;   // DB has been seeded
                //}

                Configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json", true, true)
                                .Build();

                List <RedditPost> posts = GeneratePosts();

                var newIds   = posts.Select(p => p.Number).Distinct().ToArray();
                var oldIds   = context.RedditPost.Where(p => newIds.Contains(p.Number)).Select(p => p.Number).ToArray();
                var idsToAdd = posts.Where(p => !oldIds.Contains(p.Number)).ToList();

                if (idsToAdd.Count > 0)
                {
                    context.RedditPost.AddRange(idsToAdd);
                    context.SaveChanges();
                }

                //UpdateContentUrlWithinDatabase(context, posts);
            }
        }
Example #2
0
        private static void UpdateContentUrlWithinDatabase(RedditPostContext context, List <RedditPost> posts)
        {
            IQueryable <RedditPost>  postsQuery      = from m in context.RedditPost select m;
            IEnumerable <RedditPost> postsEnumerable = postsQuery.ToList().AsEnumerable();

            foreach (RedditPost fromJson in posts)
            {
                RedditPost fromDatabase = postsEnumerable.Where(post => post.Number == fromJson.Number).FirstOrDefault();

                if (fromJson.UrlContent != fromDatabase.UrlContent)
                {
                    fromDatabase.UrlContent = fromJson.UrlContent;
                    context.Update(fromDatabase);
                    context.SaveChanges();
                }
            }
        }
 public PasswordController(RedditPostContext redditPostContext, SubredditInfoContext subredditInfoContext, IConfiguration configuration) : base(redditPostContext, subredditInfoContext, configuration)
 {
 }
Example #4
0
 public RedditPostRepository(RedditPostContext redditPostContext)
 {
     this.RedditPostContext = redditPostContext;
 }
 public BaseController(RedditPostContext redditPostContext, SubredditInfoContext subredditInfoContext, IConfiguration configuration)
 {
     _redditPostContext    = redditPostContext;
     _subredditInfoContext = subredditInfoContext;
     _configuration        = configuration;
 }