private async Task AddWatchedUserToDb(IUser user)
        {
            using IServiceScope scope = _scopeFactory.CreateScope();
            ApplicationDbContext db = scope.GetDatabase();

            if (db.WatchedUsers != null)
            {
                await db.WatchedUsers.AddAsync(new WatchedUser(user));
            }

            await db.SaveChangesAsync();
        }
        private async Task RemoveWatchedUserFromDb(IUser user)
        {
            using IServiceScope scope = _scopeFactory.CreateScope();
            ApplicationDbContext db = scope.GetDatabase();

            if (db.WatchedUsers != null)
            {
                WatchedUser watchedUser = await db.WatchedUsers.FirstOrDefaultAsync(u => u.Name == user.Name);

                db.WatchedUsers.Remove(watchedUser);
            }

            await db.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task InitializeAsync()
        {
            using (IServiceScope scope = _scopeFactory.CreateScope())
            {
                ApplicationDbContext db = scope.GetDatabase();

                await db.Database.EnsureCreatedAsync();

                await db.Users
                .ToAsyncEnumerable()
                .ForEachAwaitAsync(Login);

                if (db.OperatedPosts != null)
                {
                    await db.OperatedPosts
                    .ToAsyncEnumerable()
                    .ForEachAsync(
                        post => _postOperationValidator.UserOperatedOnPost(post.PostId, post.UserId));
                }

                if (db.WatchedUsers != null)
                {
                    var watchedUsers = await db.WatchedUsers.ToListAsync();

                    _postsChecker
                    .WatchedUsers
                    .AddRange(watchedUsers);
                }
            }

            _postOperationValidator
            .OnUserOperatedOnPost
            .SubscribeAsync(OnUserOperatedOnPost);

            _postsWatcher.StartWatch();
        }