Avoiding select n+1 when comments shapes are hidden from a list.

This commit is contained in:
Bertrand Le Roy 2013-10-07 12:39:01 -07:00
parent 42ab42e6e2
commit fec758bb5c

View File

@ -22,20 +22,24 @@ namespace Orchard.Comments.Drivers {
}
protected override DriverResult Display(CommentsPart part, string displayType, dynamic shapeHelper) {
if (part.CommentsShown == false)
return null;
var commentsForCommentedContent = _commentService.GetCommentsForCommentedContent(part.ContentItem.Id);
var pendingCount = new Lazy<int>(() => commentsForCommentedContent.Where(x => x.Status == CommentStatus.Pending).Count());
var approvedCount = new Lazy<int>(() => commentsForCommentedContent.Where(x => x.Status == CommentStatus.Approved).Count());
return Combined(
ContentShape("Parts_ListOfComments",
() => {
if (part.CommentsShown == false)
return null;
// create a hierarchy of shapes
var firstLevelShapes = new List<dynamic>();
var allShapes = new Dictionary<int, dynamic>();
var comments = commentsForCommentedContent.Where(x => x.Status == CommentStatus.Approved).OrderBy(x => x.Position).List().ToList();
var comments = _commentService
.GetCommentsForCommentedContent(part.ContentItem.Id)
.Where(x => x.Status == CommentStatus.Approved)
.OrderBy(x => x.Position)
.List()
.ToList();
var approvedCount = comments.Count();
foreach (var item in comments) {
var shape = shapeHelper.Parts_Comment(ContentPart: item, ContentItem: item.ContentItem);
@ -54,10 +58,14 @@ namespace Orchard.Comments.Drivers {
var list = shapeHelper.List(Items: firstLevelShapes);
return shapeHelper.Parts_ListOfComments(List: list, CommentCount: approvedCount.Value);
return shapeHelper.Parts_ListOfComments(
List: list,
CommentCount: approvedCount);
}),
ContentShape("Parts_CommentForm",
() => {
if (part.CommentsShown == false)
return null;
var newComment = _contentManager.New("Comment");
if (newComment.Has<CommentPart>()) newComment.As<CommentPart>().CommentedOn = part.Id;
@ -66,9 +74,39 @@ namespace Orchard.Comments.Drivers {
return shapeHelper.Parts_CommentForm(EditorShape: editorShape);
}),
ContentShape("Parts_Comments_Count",
() => shapeHelper.Parts_Comments_Count(CommentCount: approvedCount.Value, PendingCount: pendingCount.Value)),
() => {
if (part.CommentsShown == false)
return null;
var comments = _commentService
.GetCommentsForCommentedContent(part.ContentItem.Id);
var approvedCount = comments
.Where(x => x.Status == CommentStatus.Approved)
.Count();
var pendingCount = comments
.Where(x => x.Status == CommentStatus.Pending)
.Count();
return shapeHelper.Parts_Comments_Count(
CommentCount: approvedCount,
PendingCount: pendingCount);
}),
ContentShape("Parts_Comments_Count_SummaryAdmin",
() => shapeHelper.Parts_Comments_Count_SummaryAdmin(CommentCount: approvedCount.Value, PendingCount: pendingCount.Value))
() => {
var comments = _commentService
.GetCommentsForCommentedContent(part.ContentItem.Id);
var approvedCount = comments
.Where(x => x.Status == CommentStatus.Approved)
.Count();
var pendingCount = comments
.Where(x => x.Status == CommentStatus.Pending)
.Count();
return shapeHelper.Parts_Comments_Count_SummaryAdmin(
CommentCount: approvedCount,
PendingCount: pendingCount);
})
);
}