mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-03 03:58:13 +08:00
#18199: Optimize Comments counting on containers
Work Item: 18199 --HG-- branch : 1.x
This commit is contained in:
@@ -1,40 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Comments.Models;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Comments.Services;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.Core.Common.Models;
|
||||
|
||||
namespace Orchard.Comments.Drivers {
|
||||
[UsedImplicitly]
|
||||
public class CommentsContainerPartDriver : ContentPartDriver<CommentsContainerPart> {
|
||||
private readonly ICommentService _commentService;
|
||||
|
||||
public CommentsContainerPartDriver(ICommentService commentService) {
|
||||
_commentService = commentService;
|
||||
}
|
||||
|
||||
protected override DriverResult Display(CommentsContainerPart part, string displayType, dynamic shapeHelper) {
|
||||
var commentsForCommentedContent = _commentService.GetCommentsForCommentedContent(part.ContentItem.Id);
|
||||
|
||||
return Combined(
|
||||
|
||||
ContentShape("Parts_Comments_Count",
|
||||
() => {
|
||||
var childItems = GetChildItems(part);
|
||||
return shapeHelper.Parts_Comments_Count(ContentPart: part, CommentCount: GetCount(childItems), PendingCount: GetPendingCount(childItems));
|
||||
}),
|
||||
() => shapeHelper.Parts_Comments_Count(ContentPart: part, CommentCount: commentsForCommentedContent.Count(), PendingCount: commentsForCommentedContent.Where(x => x.Status == CommentStatus.Pending).Count())),
|
||||
ContentShape("Parts_Comments_Count_SummaryAdmin",
|
||||
() => {
|
||||
var childItems = GetChildItems(part);
|
||||
return shapeHelper.Parts_Comments_Count_SummaryAdmin(ContentPart: part, CommentCount: GetCount(childItems), PendingCount: GetPendingCount(childItems));
|
||||
})
|
||||
() => shapeHelper.Parts_Comments_Count_SummaryAdmin(ContentPart: part, CommentCount: commentsForCommentedContent.Count(), PendingCount: commentsForCommentedContent.Where(x => x.Status == CommentStatus.Pending).Count()))
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<ContentItem> GetChildItems(CommentsContainerPart part) {
|
||||
return part.ContentItem.ContentManager.Query()
|
||||
.Where<CommonPartRecord>(rec => rec.Container == part.ContentItem.Record).List();
|
||||
}
|
||||
|
||||
private static int GetPendingCount(IEnumerable<ContentItem> parts) {
|
||||
return parts.Aggregate(0, (seed, item) => seed + (item.Has<CommentsPart>() ? item.As<CommentsPart>().PendingComments.Count : 0));
|
||||
}
|
||||
|
||||
private static int GetCount(IEnumerable<ContentItem> parts) {
|
||||
return parts.Aggregate(0, (seed, item) => seed + (item.Has<CommentsPart>() ? item.As<CommentsPart>().Comments.Count : 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user