Exemple #1
0
        public async Task <IEnumerable <RssItem> > GetItemsAsync(Core.Entities.Site site, int count)
        {
            var posts = await _postsRepository.GetAllWithBlocksAsync(entities =>
                                                                     entities.Where(e => e.IsPublished).ForSite(site).OrderByDescending(p => p.DatePublished).Take(count));

            DateTimeOffset?mostRecentPubDate = DateTimeOffset.MinValue;
            var            commentsData      =
                await _commentsProvider.GetCommentsDataAsync(posts.items.Select(p => p as IContentItem).ToArray(), site);

            var items = new List <RssItem>();

            foreach (var post in posts.items)
            {
                if (post.DatePublished != null)
                {
                    var newsDate = post.DatePublished;
                    if (newsDate > mostRecentPubDate)
                    {
                        mostRecentPubDate = newsDate;
                    }
                    var postUrl = _linkGenerator.GeneratePublicUrl(post, site);
                    var item    = new RssItem
                    {
                        Title           = post.Title,
                        Description     = GetDescription(post),
                        Link            = postUrl,
                        PublicationDate = newsDate.Value.DateTime,
                        Author          = post.Author.Name,
                        Guid            = new RssGuid(postUrl.ToString(), true)
                    };

                    if (commentsData.ContainsKey(post.Id))
                    {
                        item.Comments = commentsData[post.Id].uri;
                    }

                    foreach (var section in post.Sections)
                    {
                        item.Categories.Add(new RssCategory(section.Title,
                                                            _linkGenerator.GeneratePublicUrl(section).ToString()));
                    }

                    items.Add(item);
                }
            }

            return(items);
        }