Beispiel #1
0
 public static int NumberPostsLastNMonths(int monthsPrevious = 6)
 {
     using (var repo = new BlogPostRepo())
     {
         return repo.PublishedPosts.Count(x => x.PublishedOn.Value >= DateTime.UtcNow.AddMonths(-1 * monthsPrevious));
     }
 }
Beispiel #2
0
 /// <summary>
 /// Force get all items from the Git repo.
 /// </summary>
 /// <returns></returns>
 public async Task<ActionResult> ForceRepoRefresh()
 {
     using (var repo = new BlogPostRepo())
     {
         await repo.RefreshCachedBlogPosts();
         return RedirectToRoute("Front");
     }
 }
Beispiel #3
0
 public async Task<ActionResult> ListPostsForTag(string tag)
 {
     using (var repo = new BlogPostRepo())
     {
         var postsWithTag = repo.ListPostsWithTag(tag.Trim()).ToList();
         return View("PostsWithTag", new PostsWithTag_vm { Tag = tag.Trim(), Posts = postsWithTag });
     }
 }
Beispiel #4
0
 public ActionResult ViewPost(string slug)
 {
     using (var repo = new BlogPostRepo())
     {
         BlogPost bp = repo.GetPost(slug);
         return View(new BlogPost_vm { BlogPost = bp });
     }
 }
Beispiel #5
0
 public ActionResult GetFeaturedRecentPosts()
 {
     using (var repo = new BlogPostRepo())
     {
         var recents = repo.ListRecentBlogPosts(3).ToList();
         return PartialView("_RecentPosts", recents);
     }
 }        
Beispiel #6
0
 public ActionResult ListRecentPosts(bool showAll = false)
 {
     using (var repo = new BlogPostRepo())
     {
         int numPosts = showAll ? int.MaxValue : Helpers.Config.NumPostsFrontPage;
         var recents = repo.ListRecentBlogPosts(numPosts).ToList();
         return View("Front", new Front_vm { RecentBlogPosts = recents });
     }
 }
Beispiel #7
0
 public async Task<ActionResult> RefreshSearchIndex()
 {
     var azureIndexer = new BlogPostSearchIndex(Config.AzureSearchService, Config.AzureSearchApiKey);
     using (var blogPostRepo = new BlogPostRepo())
     {
         var blogPostsToIndex = blogPostRepo.PublishedPosts.Select(x => new IndexBlogPost { BlogPostBody = x.Body, Id = x.UrlSlug }).ToArray();
         await azureIndexer.AddToIndex(blogPostsToIndex);
     }
     return RedirectToRoute("Front");
 }
Beispiel #8
0
 public static double CalcAveragePostPerMonth()
 {
     using (var repo = new BlogPostRepo())
     {
         return repo.PublishedPosts
                    .Where(x => x.PublishedOn.Value >= DateTime.UtcNow.AddMonths(-11))
                    .GroupBy(x => x.PublishedOn.Value.Month)
                    .Select(g => new
                    {
                        Month = g.Key,
                        NumPosts = g.Count()
                    })
                    .Average(x => x.NumPosts);
     }
 }
Beispiel #9
0
 public IEnumerable<string> GetTopBlogPostTags()
 {
     using (var repo = new BlogPostRepo())
     {
         return repo.PublishedPosts
                 .SelectMany(x => x.Tags)
                  .GroupBy(x => x)
                  .Select(g => new
                  {
                      Tag = g.Key,
                      NumPosts = g.Count()
                  })
                  .OrderByDescending(x => x.NumPosts)
                  .Take(10)
                  .Select(x => x.Tag).ToList();
     }
 }
Beispiel #10
0
        public RssActionResult GenerateRSS()
        {
            using (var repo = new BlogPostRepo())
            {
                var items = repo.ListRecentBlogPosts(int.MaxValue).Select(x =>
                    new SyndicationItem(
                    title: x.Title,
                    content: x.Intro,
                    lastUpdatedTime: x.PublishedOn.Value,
                    id: GenerateBlogLink(x.UrlSlug),
                    itemAlternateLink: new Uri(GenerateBlogLink(x.UrlSlug)))
                    );

                SyndicationFeed feed = new SyndicationFeed
                {
                    Title = new TextSyndicationContent("{0}'s Blog".FormatWith(Thyme.Web.Helpers.Config.MyName)),
                    Description = new TextSyndicationContent("Blog feed for {0}".FormatWith(Request.Url.Host)),
                    Language = "en-us",
                    LastUpdatedTime = items.OrderByDescending(x => x.PublishDate).First().PublishDate.ToUniversalTime(),
                    Items = items,

                };
                feed.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(GenerateBlogLink())));
                return new RssActionResult() { Feed = feed };
            }
        }
Beispiel #11
0
        public async Task<ActionResult> SearchBlogPosts(string keywords)
        {
            keywords = keywords.RemoveSlugSeparators();
            ViewBag.SearchKeywords = keywords;
            var matchingPosts = new List<BlogPost>();
            var azureIndexer = new BlogPostSearchIndex(Config.AzureSearchService, Config.AzureSearchApiKey);

            using (var repo = new BlogPostRepo())
            {
                var results = await azureIndexer.SearchIndex(keywords);
                foreach (var result in results.OrderByDescending(x => x.Score))
                {
                    matchingPosts.Add(repo.GetPost(result.Document.Id));
                }
            }
            return View("SearchResults", new Search_vm { SearchKeywords = keywords, MatchingBlogPosts = matchingPosts });
        }