Example #1
0
        private void SaveFeedsPortion(IEnumerable <FeedRow> feeds, DateTime aggregatedDate)
        {
            using var tx = FeedDbContext.Database.BeginTransaction();

            foreach (var f in feeds)
            {
                if (0 >= f.Users.Count)
                {
                    continue;
                }

                var feedAggregate = new FeedAggregate
                {
                    Id            = f.Id,
                    Tenant        = f.Tenant,
                    Product       = f.ProductId,
                    Module        = f.ModuleId,
                    Author        = f.AuthorId,
                    ModifiedBy    = f.ModifiedById,
                    GroupId       = f.GroupId,
                    CreatedDate   = f.CreatedDate,
                    ModifiedDate  = f.ModifiedDate,
                    Json          = f.Json,
                    Keywords      = f.Keywords,
                    AggregateDate = aggregatedDate
                };

                if (f.ClearRightsBeforeInsert)
                {
                    var fu = FeedDbContext.FeedUsers.Where(r => r.FeedId == f.Id).FirstOrDefault();
                    if (fu != null)
                    {
                        FeedDbContext.FeedUsers.Remove(fu);
                    }
                }

                FeedDbContext.AddOrUpdate(r => r.FeedAggregates, feedAggregate);

                foreach (var u in f.Users)
                {
                    var feedUser = new FeedUsers
                    {
                        FeedId = f.Id,
                        UserId = u
                    };

                    FeedDbContext.AddOrUpdate(r => r.FeedUsers, feedUser);
                }
            }

            FeedDbContext.SaveChanges();

            tx.Commit();
        }
        public async Task <string> Handle(AddFeedCommand request, CancellationToken cancellationToken)
        {
            var isNewFeed = false;
            var feed      = await _feedCommandRepository.Get(request.UserId, request.Title, cancellationToken);

            var evts = new List <FeedDataSourceSubscribedEvent>();

            if (feed == null)
            {
                feed      = FeedAggregate.Create(request.UserId, request.Title);
                isNewFeed = true;
            }

            if (request.DatasourceIds != null)
            {
                var datasources = await _dataSourceService.Get(request.DatasourceIds, cancellationToken);

                var missingDatasources = request.DatasourceIds.Where(d => !datasources.Any(ds => ds.Id == d));
                if (missingDatasources.Any())
                {
                    throw new NewsAggregatorException(string.Format(Global.UnknownDataSource, string.Join(",", missingDatasources)));
                }

                foreach (var datasource in datasources)
                {
                    evts.Add(feed.SubscribeDataSource(request.UserId, datasource.Id));
                }
            }

            if (isNewFeed)
            {
                await _feedCommandRepository.Add(feed, cancellationToken);
            }
            else
            {
                await _feedCommandRepository.Update(feed, cancellationToken);
            }

            await _feedCommandRepository.SaveChanges(cancellationToken);

            _logger.LogInformation($"User {request.UserId} adds the feed {request.Title}");
            foreach (var evt in evts)
            {
                await _busControl.Publish(evt, cancellationToken);
            }

            return(feed.Id);
        }
 public Task Update(FeedAggregate feed, CancellationToken cancellation)
 {
     _dbContext.Feeds.Update(feed);
     return(Task.CompletedTask);
 }
 public Task Add(FeedAggregate feed, CancellationToken cancellationToken)
 {
     _dbContext.Add(feed);
     return(Task.CompletedTask);
 }