public async Task <bool> Handle(UnSubscribeDatasourceCommand request, CancellationToken cancellationToken)
        {
            var feed = await _feedCommandRepository.Get(request.FeedId, cancellationToken);

            if (feed == null)
            {
                _logger.LogError($"Feed {request.FeedId} doesn't exist");
                throw new NewsAggregatorResourceNotFoundException(string.Format(Global.FeedDoesntExist, feed.Id));
            }

            feed.UnsubscribeDataSource(request.UserId, request.DatasourceId);
            var datasources = await _dataSourceService.Get(new string[] { request.DatasourceId }, cancellationToken);

            if (!datasources.Any())
            {
                _logger.LogError($"DataSource {request.DatasourceId} doesn't exist");
                throw new NewsAggregatorException(string.Format(Global.DataSourceDoesntExist, request.DatasourceId));
            }

            await _feedCommandRepository.Update(feed, cancellationToken);

            await _feedCommandRepository.SaveChanges(cancellationToken);

            _logger.LogInformation($"Unsubscribe from the datasource {request.DatasourceId} of the feed {request.FeedId}");
            return(true);
        }
コード例 #2
0
        protected Exercise Create(Exercise Exercise, UserModel User = null)
        {
            Exercise.ExerciseQuestions = _questionService.GetByExerciseId(Exercise.ExerciseId, User);
            Exercise.DataSource        = _dataSourceService.Get(Exercise.DataSourceId);

            // Link the child objects to the question
            foreach (var item in Exercise.ExerciseQuestions)
            {
                item.Exercise = Exercise;
            }

            return(Exercise);
        }
コード例 #3
0
        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);
        }