Пример #1
0
        static async Task Main(string[] args)
        {
            var medium = new Medium();
            var pack   = await medium.GetPack(MEDIUM_USERS);

            var contextOptions = new DbContextOptionsBuilder <BlogContext>()
                                 .UseNpgsql("Server=localhost;Database=blog;Username=blogadmin;Password=blogadmin")
                                 .Options;
            var blogContext = new BlogContext(contextOptions);

            var usersRepository = new UserRepository(blogContext);
            var users           = pack.Users.Where(u => usersRepository.IsUsernameUniq(u.Username)).ToList();

            users.ForEach(usersRepository.Add);
            usersRepository.Commit();
            Console.WriteLine($"{users.Count} new users added");

            var storiesRepository = new StoryRepository(blogContext);
            var stories           = pack.Stories.Where(s =>
                                                       storiesRepository.GetSingle(os => os.Title == s.Title && os.PublishTime == s.PublishTime) == null
                                                       ).ToList();

            stories.ForEach(storiesRepository.Add);
            storiesRepository.Commit();
            Console.WriteLine($"{stories.Count} new stories added");

            var likeRepository = new LikeRepository(blogContext);
            var likes          = GenerateLikes(new Pack {
                Users = pack.Users, Stories = stories
            });

            likes.ForEach(likeRepository.Add);
            likeRepository.Commit();
            Console.WriteLine($"{likes.Count} new likes added");
        }