diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs index bad560846..b25c29dd8 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; using Orchard.Blogs.Models; +using Orchard.Blogs.Services; +using Orchard.ContentManagement; using Orchard.ContentManagement.Handlers; using Orchard.Core.Common.Models; using Orchard.Data; @@ -10,12 +12,12 @@ using Orchard.Data; namespace Orchard.Blogs.Handlers { [UsedImplicitly] public class BlogPartArchiveHandler : ContentHandler { - public BlogPartArchiveHandler(IRepository blogArchiveRepository, IRepository commonRepository) { - OnPublished((context, bp) => RecalculateBlogArchive(blogArchiveRepository, commonRepository, bp)); - OnRemoved((context, bp) => RecalculateBlogArchive(blogArchiveRepository, commonRepository, bp)); + public BlogPartArchiveHandler(IRepository blogArchiveRepository, IBlogPostService blogPostService) { + OnPublished((context, bp) => RecalculateBlogArchive(blogArchiveRepository, blogPostService, bp)); + OnRemoved((context, bp) => RecalculateBlogArchive(blogArchiveRepository, blogPostService, bp)); } - private static void RecalculateBlogArchive(IRepository blogArchiveRepository, IRepository commonRepository, BlogPostPart blogPostPart) { + private static void RecalculateBlogArchive(IRepository blogArchiveRepository, IBlogPostService blogPostService, BlogPostPart blogPostPart) { blogArchiveRepository.Flush(); // remove all current blog archive records @@ -26,19 +28,16 @@ namespace Orchard.Blogs.Handlers { blogArchiveRecords.ToList().ForEach(blogArchiveRepository.Delete); // get all blog posts for the current blog - var postsQuery = - from bpr in commonRepository.Table - where bpr.ContentItemRecord.ContentType.Name == "BlogPost" && bpr.Container.Id == blogPostPart.BlogPart.Record.Id - orderby bpr.PublishedUtc - select bpr; + var posts = blogPostService.Get(blogPostPart.BlogPart, VersionOptions.Published); // create a dictionary of all the year/month combinations and their count of posts that are published in this blog - var inMemoryBlogArchives = new Dictionary(postsQuery.Count()); - foreach (var post in postsQuery) { - if (!post.PublishedUtc.HasValue) + var inMemoryBlogArchives = new Dictionary(posts.Count()); + foreach (var post in posts) { + if (!post.Has()) continue; - var key = new DateTime(post.PublishedUtc.Value.Year, post.PublishedUtc.Value.Month, 1); + var commonPart = post.As(); + var key = new DateTime(commonPart.PublishedUtc.Value.Year, commonPart.PublishedUtc.Value.Month, 1); if (inMemoryBlogArchives.ContainsKey(key)) inMemoryBlogArchives[key]++;