Fixing and improving blog posts and archives count

This commit is contained in:
Sebastien Ros
2014-02-10 12:16:52 -08:00
parent 9831ea10e4
commit 6a43bad033
8 changed files with 68 additions and 27 deletions

View File

@@ -78,7 +78,7 @@ namespace Orchard.Blogs.Handlers {
_blogArchiveRepository.Flush();
// don't reduce archive count if the content item is not published
if (!blogPostPart.HasPublished) {
if (!_previousCreatedUtc.ContainsKey(blogPostPart.Id)) {
return;
}

View File

@@ -5,27 +5,22 @@ using Orchard.Blogs.Models;
using Orchard.Blogs.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Common.Models;
namespace Orchard.Blogs.Handlers {
[UsedImplicitly]
public class BlogPostPartHandler : ContentHandler {
private readonly IBlogService _blogService;
private readonly IBlogPostService _blogPostService;
public BlogPostPartHandler(IBlogService blogService, IBlogPostService blogPostService, RequestContext requestContext) {
_blogService = blogService;
_blogPostService = blogPostService;
OnGetDisplayShape<BlogPostPart>(SetModelProperties);
OnGetEditorShape<BlogPostPart>(SetModelProperties);
OnUpdateEditorShape<BlogPostPart>(SetModelProperties);
OnCreated<BlogPostPart>((context, part) => UpdateBlogPostCount(part));
OnPublished<BlogPostPart>((context, part) => UpdateBlogPostCount(part));
OnUnpublished<BlogPostPart>((context, part) => UpdateBlogPostCount(part));
OnVersioned<BlogPostPart>((context, part, newVersionPart) => UpdateBlogPostCount(newVersionPart));
OnRemoved<BlogPostPart>((context, part) => UpdateBlogPostCount(part));
OnCreated<BlogPostPart>((context, part) => blogService.ProcessBlogPostsCount(part.BlogPart.Id));
OnPublished<BlogPostPart>((context, part) => blogService.ProcessBlogPostsCount(part.BlogPart.Id));
OnUnpublished<BlogPostPart>((context, part) => blogService.ProcessBlogPostsCount(part.BlogPart.Id));
OnVersioned<BlogPostPart>((context, part, newVersionPart) => blogService.ProcessBlogPostsCount(newVersionPart.BlogPart.Id));
OnRemoved<BlogPostPart>((context, part) => blogService.ProcessBlogPostsCount(part.BlogPart.Id));
OnRemoved<BlogPart>(
(context, b) =>
@@ -33,19 +28,6 @@ namespace Orchard.Blogs.Handlers {
blogPost => context.ContentManager.Remove(blogPost.ContentItem)));
}
private void UpdateBlogPostCount(BlogPostPart blogPostPart) {
CommonPart commonPart = blogPostPart.As<CommonPart>();
if (commonPart != null &&
commonPart.Record.Container != null) {
BlogPart blogPart = blogPostPart.BlogPart ??
_blogService.Get(commonPart.Record.Container.Id, VersionOptions.Published).As<BlogPart>();
// Ensure the "right" set of published posts for the blog is obtained
blogPart.PostCount = _blogPostService.PostCount(blogPart);
}
}
private static void SetModelProperties(BuildShapeContext context, BlogPostPart blogPost) {
context.Shape.Blog = blogPost.BlogPart;
}

View File

@@ -108,9 +108,11 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Routes.cs" />
<Compile Include="Services\BlogPostService.cs" />
<Compile Include="Services\BlogPostsCountProcessor.cs" />
<Compile Include="Services\IArchiveService.cs" />
<Compile Include="Services\IBlogPostService.cs" />
<Compile Include="Services\IBlogService.cs" />
<Compile Include="Services\IBlogPostsCountProcessor.cs" />
<Compile Include="Services\XmlRpcHandler.cs" />
<Compile Include="ViewModels\BlogArchivesViewModel.cs" />
<Compile Include="ViewModels\RecentBlogPostsViewModel.cs" />

View File

@@ -58,7 +58,10 @@ namespace Orchard.Blogs.Services {
}
public int PostCount(BlogPart blogPart, VersionOptions versionOptions) {
return GetBlogQuery(blogPart, versionOptions).Count();
return _contentManager.Query(versionOptions, "BlogPost")
.Join<CommonPartRecord>().Where(
cr => cr.Container == blogPart.Record.ContentItemRecord)
.Count();
}
public IEnumerable<BlogPostPart> Get(BlogPart blogPart, ArchiveData archiveData) {

View File

@@ -0,0 +1,26 @@
using Orchard.Blogs.Models;
using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
namespace Orchard.Blogs.Services {
public class BlogPostsCountProcessor : IBlogPostsCountProcessor {
private readonly IContentManager _contentManager;
public BlogPostsCountProcessor(
IContentManager contentManager) {
_contentManager = contentManager;
}
public void Process(int blogPartId) {
var blogPart = _contentManager.Get<BlogPart>(blogPartId);
if (blogPart != null) {
var count = _contentManager.Query(VersionOptions.Published, "BlogPost")
.Join<CommonPartRecord>().Where(
cr => cr.Container.Id == blogPartId)
.Count();
blogPart.PostCount = count;
}
}
}
}

View File

@@ -5,14 +5,27 @@ using Orchard.Autoroute.Models;
using Orchard.Blogs.Models;
using Orchard.ContentManagement;
using Orchard.Core.Title.Models;
using Orchard.Environment.Configuration;
using Orchard.Environment.Descriptor;
using Orchard.Environment.State;
namespace Orchard.Blogs.Services {
[UsedImplicitly]
public class BlogService : IBlogService {
private readonly IContentManager _contentManager;
public BlogService(IContentManager contentManager) {
private readonly IProcessingEngine _processingEngine;
private readonly ShellSettings _shellSettings;
private readonly IShellDescriptorManager _shellDescriptorManager;
private readonly HashSet<int> _processedBlogParts = new HashSet<int>();
public BlogService(
IContentManager contentManager,
IProcessingEngine processingEngine,
ShellSettings shellSettings,
IShellDescriptorManager shellDescriptorManager) {
_contentManager = contentManager;
_processingEngine = processingEngine;
_shellSettings = shellSettings;
_shellDescriptorManager = shellDescriptorManager;
}
public BlogPart Get(string path) {
@@ -38,5 +51,12 @@ namespace Orchard.Blogs.Services {
public void Delete(ContentItem blog) {
_contentManager.Remove(blog);
}
public void ProcessBlogPostsCount(int blogPartId) {
if (!_processedBlogParts.Contains(blogPartId)) {
_processedBlogParts.Add(blogPartId);
_processingEngine.AddTask(_shellSettings, _shellDescriptorManager.GetShellDescriptor(), "IBlogPostsCountProcessor.Process", new Dictionary<string, object> { { "blogPartId", blogPartId } });
}
}
}
}

View File

@@ -0,0 +1,7 @@
using Orchard.Events;
namespace Orchard.Blogs.Services {
public interface IBlogPostsCountProcessor : IEventHandler {
void Process(int blogPartId);
}
}

View File

@@ -9,5 +9,6 @@ namespace Orchard.Blogs.Services {
IEnumerable<BlogPart> Get();
IEnumerable<BlogPart> Get(VersionOptions versionOptions);
void Delete(ContentItem blog);
void ProcessBlogPostsCount(int blogPartId);
}
}