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);
        }
        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 async Task <bool> Handle(DeleteFeedCommand 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.Remove(request.UserId);
            await _feedCommandRepository.Delete(feed, cancellationToken);

            await _feedCommandRepository.SaveChanges(cancellationToken);

            _logger.LogInformation($"the feed {request.FeedId} is removed");
            return(true);
        }