public void BeAnAggregateRoot()
        {
            var result = NewsRssSource.Create(
                name: "Test",
                url: "Source",
                isEnabled: true);

            result.ShouldBeAssignableTo <IAggregateRoot>();
        }
        public void BeCreated_UsingCreate_Method()
        {
            var result = NewsRssSource.Create(
                name: "Test",
                url: "Source",
                isEnabled: true);

            result.ShouldNotBeNull();
            result.Name.ShouldBe("Test");
            result.Url.ShouldBe("Source");
            result.IsEnabled.ShouldBeTrue();
        }
        protected override void Seed(NewsContext context)
        {
            var defaultRssSources = new List <NewsRssSource>
            {
                NewsRssSource.Create("Nasdaq: Business News", "http://articlefeeds.nasdaq.com/nasdaq/categories?category=Business", true),
                NewsRssSource.Create("Nasdaq: Forex and currencies", "http://articlefeeds.nasdaq.com/nasdaq/categories?category=Forex+and+Currencies", true),
                NewsRssSource.Create("DailyFX: Forex market news and analysis", "https://rss.dailyfx.com/feeds/all", true)
            };

            context.Set <NewsRssSource>().AddOrUpdate(x => x.Url, defaultRssSources.ToArray());

            context.SaveChanges();
        }
Ejemplo n.º 4
0
 public Task Update(NewsRssSource newsRssSource)
 {
     return(this._repository.UpdateAndSaveAsync <NewsRssSource, Guid>(newsRssSource));
 }
Ejemplo n.º 5
0
 public Task Add(NewsRssSource newsRssSource)
 {
     return(this._repository.AddAndSaveAsync <NewsRssSource, Guid>(newsRssSource));
 }
        private async Task <IEnumerable <NewsItem> > ReadRssFeed(NewsRssSource source, DateTime?from, DateTime?to)
        {
            if (source == null)
            {
                throw new ArgumentException("Source must be specified", nameof(source));
            }

            if (string.IsNullOrEmpty(source.Url))
            {
                throw new BusinessException("Source url must be specified");
            }

            try
            {
                Log.InfoFormat("Start fetching {0} RSS feed... (URL: {1})", source.Name, source.Url);

                using (var reader = XmlReader.Create(source.Url))
                {
                    var feed = SyndicationFeed.Load(reader);
                    if (feed != null)
                    {
                        var result = feed.Items
                                     .Where(x => (!from.HasValue || (x.PublishDate.Date >= from.Value.Date && x.PublishDate.Hour >= from.Value.Hour)) &&
                                            (!to.HasValue || (x.PublishDate.Date <= to.Value.Date && x.PublishDate.Hour <= to.Value.Hour)))
                                     .OrderBy(x => x.PublishDate)
                                     .ThenBy(x => x.LastUpdatedTime)
                                     .Select(x => ParseSyndicationItem(x, source.Name))
                                     .ToList();

                        if (result.Any())
                        {
                            await Task.Run(() =>
                            {
                                var latestCachedItems = RssCache.GetCachedRssItems(source.Name);
                                DateTime latestDate   = DateTime.MinValue;

                                if (latestCachedItems.Any())
                                {
                                    latestDate = latestCachedItems.Max(e => e.Date);
                                }

                                var itemsToShare = new List <NewsItem>();
                                result           = result.Where(item => item.Date > latestDate).ToList();
                                foreach (var rssItem in result)
                                {
                                    if (latestCachedItems.All(item => item.UniqueIdentifier != rssItem.UniqueIdentifier))
                                    {
                                        itemsToShare.Add(rssItem);
                                    }
                                }

                                itemsToShare = itemsToShare.GroupBy(e => e.UniqueIdentifier).Select(e => e.First()).ToList();

                                Log.InfoFormat("End fetching {0} RSS feed... (URL: {1}). Count of new items = {2}", source.Name, source.Url, itemsToShare.Count);

                                RssCache.UpdateCache(source.Name, itemsToShare);

                                if (itemsToShare.Count > 0)
                                {
                                    this.NewRssItemsEvent?.Invoke(itemsToShare);
                                }
                            });
                        }

                        return(result);
                    }
                }
            }
            // TODO: Add logging
            catch (Exception exception)
            {
                Log.ErrorFormat("Error fetching {0} RSS feed... (URL: {1}). Error: {2}", source.Name, source.Url, exception.Message);
                return(Enumerable.Empty <NewsItem>());
            }

            return(Enumerable.Empty <NewsItem>());
        }