mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-11-28 09:22:55 +08:00
Refactoring CommentsPart to enable querying over comments
--HG-- branch : 1.x
This commit is contained in:
@@ -57,6 +57,8 @@ namespace Orchard.Comments.Drivers {
|
||||
if (contentItem != null) {
|
||||
part.Record.CommentedOn = contentItem.Id;
|
||||
}
|
||||
|
||||
contentItem.As<CommentsPart>().Record.CommentPartRecords.Add(part.Record);
|
||||
}
|
||||
|
||||
var commentedOnContainer = context.Attribute(part.PartDefinition.Name, "CommentedOnContainer");
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Comments.Models;
|
||||
using Orchard.Comments.Services;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
|
||||
namespace Orchard.Comments.Drivers {
|
||||
[UsedImplicitly]
|
||||
public class CommentsPartDriver : ContentPartDriver<CommentsPart> {
|
||||
private readonly ICommentService _commentService;
|
||||
|
||||
public CommentsPartDriver(ICommentService commentService) {
|
||||
_commentService = commentService;
|
||||
}
|
||||
|
||||
protected override DriverResult Display(CommentsPart part, string displayType, dynamic shapeHelper) {
|
||||
if (part.CommentsShown == false)
|
||||
return null;
|
||||
@@ -15,9 +22,9 @@ namespace Orchard.Comments.Drivers {
|
||||
ContentShape("Parts_Comments",
|
||||
() => shapeHelper.Parts_Comments(ContentPart: part)),
|
||||
ContentShape("Parts_Comments_Count",
|
||||
() => shapeHelper.Parts_Comments_Count(ContentPart: part, CommentCount: part.Comments.Count, PendingCount: part.PendingComments.Count)),
|
||||
() => shapeHelper.Parts_Comments_Count(ContentPart: part, CommentCount: _commentService.GetCommentsForCommentedContent(part.ContentItem.Id).Count(), PendingCount: part.PendingComments.Count)),
|
||||
ContentShape("Parts_Comments_Count_SummaryAdmin",
|
||||
() => shapeHelper.Parts_Comments_Count_SummaryAdmin(ContentPart: part, CommentCount: part.Comments.Count, PendingCount: part.PendingComments.Count))
|
||||
() => shapeHelper.Parts_Comments_Count_SummaryAdmin(ContentPart: part, CommentCount: _commentService.GetCommentsForCommentedContent(part.ContentItem.Id).Count(), PendingCount: part.PendingComments.Count))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Comments.Models;
|
||||
using Orchard.Comments.Services;
|
||||
@@ -19,17 +20,18 @@ namespace Orchard.Comments.Handlers {
|
||||
OnInitializing<CommentsPart>((ctx, x) => {
|
||||
x.CommentsActive = true;
|
||||
x.CommentsShown = true;
|
||||
x.Comments = new List<CommentPart>();
|
||||
});
|
||||
|
||||
OnLoading<CommentsPart>((context, comments) => {
|
||||
comments._comments.Loader(list => contentManager
|
||||
.Query<CommentPart, CommentPartRecord>()
|
||||
.Where(x => x.CommentedOn == context.ContentItem.Id && x.Status == CommentStatus.Approved)
|
||||
.Where(x => x.CommentsPartRecord == context.ContentItem.As<CommentsPart>().Record && x.Status == CommentStatus.Approved)
|
||||
.List().ToList());
|
||||
|
||||
comments._pendingComments.Loader(list => contentManager
|
||||
.Query<CommentPart, CommentPartRecord>()
|
||||
.Where(x => x.CommentedOn == context.ContentItem.Id && x.Status == CommentStatus.Pending)
|
||||
.Where(x => x.CommentsPartRecord == context.ContentItem.As<CommentsPart>().Record && x.Status == CommentStatus.Pending)
|
||||
.List().ToList());
|
||||
});
|
||||
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
using System;
|
||||
using Orchard.Comments.Models;
|
||||
using Orchard.Comments.Services;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.Core.Contents.Extensions;
|
||||
using Orchard.Data.Migration;
|
||||
|
||||
namespace Orchard.Comments {
|
||||
public class Migrations : DataMigrationImpl {
|
||||
|
||||
private readonly ICommentService _commentService;
|
||||
|
||||
public Migrations(ICommentService commentService) {
|
||||
_commentService = commentService;
|
||||
}
|
||||
|
||||
public int Create() {
|
||||
SchemaBuilder.CreateTable("CommentPartRecord", table => table
|
||||
.ContentPartRecord()
|
||||
@@ -18,6 +26,7 @@ namespace Orchard.Comments {
|
||||
.Column<string>("CommentText", column => column.Unlimited())
|
||||
.Column<int>("CommentedOn")
|
||||
.Column<int>("CommentedOnContainer")
|
||||
.Column<int>("CommentsPartRecord_id")
|
||||
);
|
||||
|
||||
SchemaBuilder.CreateTable("CommentSettingsPartRecord", table => table
|
||||
@@ -48,7 +57,7 @@ namespace Orchard.Comments {
|
||||
|
||||
ContentDefinitionManager.AlterPartDefinition("CommentsPart", builder => builder.Attachable());
|
||||
|
||||
return 2;
|
||||
return 3;
|
||||
}
|
||||
|
||||
public int UpdateFrom1() {
|
||||
@@ -56,5 +65,24 @@ namespace Orchard.Comments {
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
public int UpdateFrom2() {
|
||||
//SchemaBuilder.AlterTable("CommentPartRecord", table => table
|
||||
// .AddColumn<int>("CommentsPartRecord_id")
|
||||
// );
|
||||
|
||||
// populate the CommentsPartRecord.Comments property
|
||||
foreach(var comment in _commentService.GetComments().List()) {
|
||||
var commentedContent = _commentService.GetCommentedContent(comment.Record.CommentedOn);
|
||||
var commentsPart = commentedContent.As<CommentsPart>();
|
||||
|
||||
// the comment part might have been removed since the comment was placed
|
||||
if(commentsPart != null) {
|
||||
commentsPart.Record.CommentPartRecords.Add(comment.Record);
|
||||
}
|
||||
}
|
||||
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,7 @@ namespace Orchard.Comments.Models {
|
||||
public virtual string CommentText { get; set; }
|
||||
public virtual int CommentedOn { get; set; }
|
||||
public virtual int CommentedOnContainer { get; set; }
|
||||
|
||||
public virtual CommentsPartRecord CommentsPartRecord { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Data.Conventions;
|
||||
|
||||
namespace Orchard.Comments.Models {
|
||||
public class CommentsPartRecord : ContentPartRecord {
|
||||
public virtual bool CommentsShown { get; set; }
|
||||
public virtual bool CommentsActive { get; set; }
|
||||
|
||||
[CascadeAllDeleteOrphan]
|
||||
public virtual IList<CommentPartRecord> CommentPartRecords { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@
|
||||
<Compile Include="Drivers\CommentsContainerPartDriver.cs" />
|
||||
<Compile Include="Drivers\CommentSettingsPartDriver.cs" />
|
||||
<Compile Include="Drivers\CommentsPartDriver.cs" />
|
||||
<Compile Include="Projections\CommentsFilter.cs" />
|
||||
<Compile Include="ResourceManifest.cs" />
|
||||
<Compile Include="Rules\CommentsActions.cs" />
|
||||
<Compile Include="Rules\CommentsForms.cs" />
|
||||
|
||||
@@ -84,6 +84,7 @@ namespace Orchard.Comments.Services {
|
||||
if (commentedOn != null && commentedOn.Container != null) {
|
||||
comment.Record.CommentedOnContainer = commentedOn.Container.ContentItem.Id;
|
||||
}
|
||||
commentedOn.As<CommentsPart>().Record.CommentPartRecords.Add(comment.Record);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ namespace Orchard.ContentManagement {
|
||||
return _session;
|
||||
}
|
||||
|
||||
internal ICriteria BindCriteriaByPath(ICriteria criteria, string path) {
|
||||
return criteria.GetCriteriaByPath(path) ?? criteria.CreateCriteria(path);
|
||||
internal ICriteria BindCriteriaByPath(ICriteria criteria, string path, string alias = null) {
|
||||
return criteria.GetCriteriaByPath(path) ?? (alias == null ? criteria.CreateCriteria(path) : criteria.CreateCriteria(path, alias));
|
||||
}
|
||||
|
||||
internal ICriteria BindTypeCriteria() {
|
||||
@@ -44,12 +44,12 @@ namespace Orchard.ContentManagement {
|
||||
internal ICriteria BindItemCriteria() {
|
||||
// [ContentItemVersionRecord] >join> [ContentItemRecord]
|
||||
|
||||
return BindCriteriaByPath(BindItemVersionCriteria(), "ContentItemRecord");
|
||||
return BindCriteriaByPath(BindItemVersionCriteria(), "ContentItemRecord", "ci");
|
||||
}
|
||||
|
||||
internal ICriteria BindItemVersionCriteria() {
|
||||
if (_itemVersionCriteria == null) {
|
||||
_itemVersionCriteria = BindSession().CreateCriteria<ContentItemVersionRecord>();
|
||||
_itemVersionCriteria = BindSession().CreateCriteria<ContentItemVersionRecord>("civ");
|
||||
}
|
||||
return _itemVersionCriteria;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user