示例#1
0
        public async Task <(string viewPath, BlogPostListVM viewModel)> GetBlogIndexAsync(int?page)
        {
            if (!page.HasValue || page <= 0)
            {
                page = BlogPostService.DEFAULT_PAGE_INDEX;
            }

            var blogSettings = await settingService.GetSettingsAsync <BlogSettings>();

            var posts = await blogPostService.GetListAsync(page.Value, blogSettings.PostPerPage);

            var blogPostListVM = await blogViewModelHelper.GetBlogPostListVMAsync(posts, page.Value);

            return("../Blog/Index", blogPostListVM);
        }
示例#2
0
        /// <summary>
        /// Returns the rss xml string for the blog or a blog category.
        /// The rss feed always returns first page with 10 results.
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        private async Task <string> GetFeed(Category cat = null)
        {
            var key  = cat == null ? BlogCache.KEY_MAIN_RSSFEED : string.Format(BlogCache.KEY_CAT_RSSFEED, cat.Slug);
            var time = cat == null ? BlogCache.Time_MainRSSFeed : BlogCache.Time_CatRSSFeed;

            return(await distributedCache.GetAsync(key, time, async() =>
            {
                var sw = new StringWriter();
                using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings()
                {
                    Async = true, Indent = true
                }))
                {
                    var postList = cat == null ?
                                   await blogPostService.GetListAsync(1, 10, cacheable: false) :
                                   await blogPostService.GetListForCategoryAsync(cat.Slug, 1);
                    var coreSettings = await settingService.GetSettingsAsync <CoreSettings>();
                    var blogSettings = await settingService.GetSettingsAsync <BlogSettings>();
                    var vm = await blogViewModelHelper.GetBlogPostListVMAsync(postList);

                    var settings = await settingService.GetSettingsAsync <CoreSettings>();
                    var channelTitle = cat == null ? settings.Title : $"{cat.Title} - {settings.Title}";
                    var channelDescription = coreSettings.Tagline;
                    var channelLink = $"{Request.Scheme}://{Request.Host}";
                    var channelLastPubDate = postList.Posts.Count <= 0 ? DateTimeOffset.UtcNow : postList.Posts[0].CreatedOn;

                    var writer = new RssFeedWriter(xmlWriter);
                    await writer.WriteTitle(channelTitle);
                    await writer.WriteDescription(channelDescription);
                    await writer.Write(new SyndicationLink(new Uri(channelLink)));
                    await writer.WritePubDate(channelLastPubDate);
                    await writer.WriteGenerator("https://www.fanray.com");

                    foreach (var postVM in vm.BlogPostViewModels)
                    {
                        var post = postVM;
                        var item = new SyndicationItem()
                        {
                            Id = postVM.Permalink, // guid https://www.w3schools.com/xml/rss_tag_guid.asp
                            Title = post.Title,
                            Description = blogSettings.FeedShowExcerpt ? post.Excerpt : post.Body,
                            Published = post.CreatedOn,
                        };

                        // link to the post
                        item.AddLink(new SyndicationLink(new Uri(postVM.CanonicalUrl)));

                        // category takes in both cats and tags
                        item.AddCategory(new SyndicationCategory(post.Category.Title));
                        foreach (var tag in post.Tags)
                        {
                            item.AddCategory(new SyndicationCategory(tag.Title));
                        }

                        // https://www.w3schools.com/xml/rss_tag_author.asp
                        // the author tag exposes email
                        //item.AddContributor(new SyndicationPerson(post.User.DisplayName, post.User.Email));

                        await writer.Write(item);
                    }

                    xmlWriter.Flush();
                }

                return sw.ToString();
            }));
        }