Fix blog post count in front-end

BlogPostCount per blog is now recomputed from the list published blog whenever there is a change in any posts of the blog. This is more deterministic than trying to keep incrementing/decrementing when the changes happen.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045749
This commit is contained in:
rpaquay
2010-01-20 21:55:49 +00:00
parent 3291efeb71
commit b88fe2667d
4 changed files with 18 additions and 9 deletions

View File

@@ -21,9 +21,7 @@ namespace Orchard.Blogs.Models {
get { return Record.Description; }
set { Record.Description = value; }
}
//public bool Enabled { get { return Record.Enabled; } }
public int PostCount {
get { return Record.PostCount; }
set { Record.PostCount = value; }

View File

@@ -1,3 +1,4 @@
using System;
using System.Linq;
using JetBrains.Annotations;
using Orchard.Blogs.Controllers;
@@ -18,8 +19,19 @@ namespace Orchard.Blogs.Models {
Filters.Add(new ActivatingFilter<BodyAspect>(BlogPostDriver.ContentType.Name));
Filters.Add(new StorageFilter<CommonVersionRecord>(commonRepository));
OnCreated<BlogPost>((context, bp) => bp.Blog.PostCount++);
OnRemoved<BlogPost>((context, bp) => bp.Blog.PostCount--);
Action<Blog> updateBlogPostCount =
(blog => {
// Ensure we get the "right" set of published posts for the blog
blog.ContentItem.ContentManager.Flush();
var posts = blogPostService.Get(blog, VersionOptions.Published).ToList();
blog.PostCount = posts.Count;
});
OnCreated<BlogPost>((context, bp) => updateBlogPostCount(bp.Blog));
OnPublished<BlogPost>((context, bp) => updateBlogPostCount(bp.Blog));
OnVersioned<BlogPost>((context, bp1, bp2) => updateBlogPostCount(bp2.Blog));
OnRemoved<BlogPost>((context, bp) => updateBlogPostCount(bp.Blog));
OnRemoved<Blog>(
(context, b) =>

View File

@@ -3,9 +3,7 @@ using Orchard.ContentManagement.Records;
namespace Orchard.Blogs.Models {
public class BlogRecord : ContentPartRecord {
[CascadeAllDeleteOrphan]
public virtual string Description { get; set; }
//public virtual bool Enabled { get; set; }
public virtual int PostCount { get; set; }
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Blogs.Controllers;
using Orchard.Blogs.Models;
using Orchard.Core.Common.Records;
using Orchard.ContentManagement;
@@ -19,7 +20,7 @@ namespace Orchard.Blogs.Services {
public BlogPost Get(Blog blog, string slug, VersionOptions versionOptions) {
return
_contentManager.Query(versionOptions, "blogpost").Join<RoutableRecord>().Where(rr => rr.Slug == slug).
_contentManager.Query(versionOptions, BlogPostDriver.ContentType.Name).Join<RoutableRecord>().Where(rr => rr.Slug == slug).
Join<CommonRecord>().Where(cr => cr.Container == blog.Record.ContentItemRecord).List().
SingleOrDefault().As<BlogPost>();
}
@@ -106,7 +107,7 @@ namespace Orchard.Blogs.Services {
private IContentQuery<ContentItem, CommonRecord> GetBlogQuery(ContentPart<BlogRecord> blog, VersionOptions versionOptions) {
return
_contentManager.Query(versionOptions, "blogpost").Join<CommonRecord>().Where(
_contentManager.Query(versionOptions, BlogPostDriver.ContentType.Name).Join<CommonRecord>().Where(
cr => cr.Container == blog.Record.ContentItemRecord).OrderByDescending(cr => cr.CreatedUtc);
}
}