mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
Refactor comment creation code (don't use CommentRecord as data object)
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045905
This commit is contained in:
@@ -8,6 +8,7 @@ using Orchard.Comments.Models;
|
|||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
|
using Orchard.Services;
|
||||||
using Orchard.Settings;
|
using Orchard.Settings;
|
||||||
using Orchard.UI.Notify;
|
using Orchard.UI.Notify;
|
||||||
using Orchard.Security;
|
using Orchard.Security;
|
||||||
@@ -18,14 +19,14 @@ namespace Orchard.Comments.Controllers {
|
|||||||
[ValidateInput(false)]
|
[ValidateInput(false)]
|
||||||
public class AdminController : Controller {
|
public class AdminController : Controller {
|
||||||
private readonly ICommentService _commentService;
|
private readonly ICommentService _commentService;
|
||||||
private readonly IContentManager _contentManager;
|
|
||||||
private readonly IAuthorizer _authorizer;
|
private readonly IAuthorizer _authorizer;
|
||||||
|
private readonly IClock _clock;
|
||||||
private readonly INotifier _notifier;
|
private readonly INotifier _notifier;
|
||||||
|
|
||||||
public AdminController(ICommentService commentService, IContentManager contentManager, INotifier notifier, IAuthorizer authorizer) {
|
public AdminController(ICommentService commentService, IContentManager contentManager, INotifier notifier, IAuthorizer authorizer, IClock clock) {
|
||||||
_commentService = commentService;
|
_commentService = commentService;
|
||||||
_contentManager = contentManager;
|
|
||||||
_authorizer = authorizer;
|
_authorizer = authorizer;
|
||||||
|
_clock = clock;
|
||||||
_notifier = notifier;
|
_notifier = notifier;
|
||||||
Logger = NullLogger.Instance;
|
Logger = NullLogger.Instance;
|
||||||
T = NullLocalizer.Instance;
|
T = NullLocalizer.Instance;
|
||||||
@@ -141,17 +142,15 @@ namespace Orchard.Comments.Controllers {
|
|||||||
return new HttpUnauthorizedResult();
|
return new HttpUnauthorizedResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
CommentRecord commentRecord = new CommentRecord {
|
var context = new CreateCommentContext {
|
||||||
Author = viewModel.Name,
|
Author = viewModel.Name,
|
||||||
CommentDateUtc = DateTime.UtcNow,
|
|
||||||
CommentText = viewModel.CommentText,
|
CommentText = viewModel.CommentText,
|
||||||
Email = viewModel.Email,
|
Email = viewModel.Email,
|
||||||
SiteName = viewModel.SiteName,
|
SiteName = viewModel.SiteName,
|
||||||
UserName = CurrentUser == null ? "Anonymous" : CurrentUser.UserName,
|
|
||||||
CommentedOn = viewModel.CommentedOn,
|
CommentedOn = viewModel.CommentedOn,
|
||||||
};
|
};
|
||||||
|
|
||||||
var comment = _commentService.CreateComment(commentRecord);
|
var comment = _commentService.CreateComment(context);
|
||||||
|
|
||||||
if (!String.IsNullOrEmpty(returnUrl)) {
|
if (!String.IsNullOrEmpty(returnUrl)) {
|
||||||
return Redirect(returnUrl);
|
return Redirect(returnUrl);
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Castle.Core;
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Orchard.Comments.Models;
|
using Orchard.Comments.Models;
|
||||||
using Orchard.ContentManagement.Aspects;
|
using Orchard.ContentManagement.Aspects;
|
||||||
@@ -8,6 +8,7 @@ using Orchard.Data;
|
|||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.Security;
|
using Orchard.Security;
|
||||||
|
using Orchard.Services;
|
||||||
using Orchard.Settings;
|
using Orchard.Settings;
|
||||||
using Orchard.UI.Notify;
|
using Orchard.UI.Notify;
|
||||||
|
|
||||||
@@ -19,7 +20,7 @@ namespace Orchard.Comments.Services {
|
|||||||
IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status);
|
IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status);
|
||||||
Comment GetComment(int id);
|
Comment GetComment(int id);
|
||||||
ContentItemMetadata GetDisplayForCommentedContent(int id);
|
ContentItemMetadata GetDisplayForCommentedContent(int id);
|
||||||
Comment CreateComment(CommentRecord commentRecord);
|
Comment CreateComment(CreateCommentContext commentRecord);
|
||||||
void UpdateComment(int id, string name, string email, string siteName, string commentText, CommentStatus status);
|
void UpdateComment(int id, string name, string email, string siteName, string commentText, CommentStatus status);
|
||||||
void ApproveComment(int commentId);
|
void ApproveComment(int commentId);
|
||||||
void PendComment(int commentId);
|
void PendComment(int commentId);
|
||||||
@@ -30,27 +31,39 @@ namespace Orchard.Comments.Services {
|
|||||||
void EnableCommentsForCommentedContent(int id);
|
void EnableCommentsForCommentedContent(int id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class CreateCommentContext {
|
||||||
|
public virtual string Author { get; set; }
|
||||||
|
public virtual string SiteName { get; set; }
|
||||||
|
public virtual string Email { get; set; }
|
||||||
|
public virtual string CommentText { get; set; }
|
||||||
|
public virtual int CommentedOn { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public class CommentService : ICommentService {
|
public class CommentService : ICommentService {
|
||||||
private readonly IRepository<ClosedCommentsRecord> _closedCommentsRepository;
|
private readonly IRepository<ClosedCommentsRecord> _closedCommentsRepository;
|
||||||
private readonly ICommentValidator _commentValidator;
|
private readonly ICommentValidator _commentValidator;
|
||||||
private readonly IContentManager _contentManager;
|
private readonly IContentManager _contentManager;
|
||||||
|
private readonly IClock _clock;
|
||||||
|
|
||||||
public CommentService(IRepository<CommentRecord> commentRepository,
|
public CommentService(IRepository<CommentRecord> commentRepository,
|
||||||
IRepository<ClosedCommentsRecord> closedCommentsRepository,
|
IRepository<ClosedCommentsRecord> closedCommentsRepository,
|
||||||
IRepository<HasCommentsRecord> hasCommentsRepository,
|
IRepository<HasCommentsRecord> hasCommentsRepository,
|
||||||
ICommentValidator commentValidator,
|
ICommentValidator commentValidator,
|
||||||
IContentManager contentManager,
|
IContentManager contentManager,
|
||||||
|
IClock clock,
|
||||||
IAuthorizer authorizer,
|
IAuthorizer authorizer,
|
||||||
INotifier notifier) {
|
INotifier notifier) {
|
||||||
_closedCommentsRepository = closedCommentsRepository;
|
_closedCommentsRepository = closedCommentsRepository;
|
||||||
_commentValidator = commentValidator;
|
_commentValidator = commentValidator;
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
|
_clock = clock;
|
||||||
Logger = NullLogger.Instance;
|
Logger = NullLogger.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ILogger Logger { get; set; }
|
public ILogger Logger { get; set; }
|
||||||
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
||||||
|
protected virtual IUser CurrentUser { get; [UsedImplicitly] private set; }
|
||||||
|
|
||||||
|
|
||||||
#region Implementation of ICommentService
|
#region Implementation of ICommentService
|
||||||
@@ -94,19 +107,18 @@ namespace Orchard.Comments.Services {
|
|||||||
return _contentManager.GetItemMetadata(content);
|
return _contentManager.GetItemMetadata(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Comment CreateComment(CommentRecord commentRecord) {
|
public Comment CreateComment(CreateCommentContext context) {
|
||||||
var comment = _contentManager.Create<Comment>("comment");
|
var comment = _contentManager.Create<Comment>("comment");
|
||||||
|
|
||||||
//TODO:(rpaquay) CommentRecord should never be used as "data object"
|
comment.Record.Author = context.Author;
|
||||||
comment.Record.Author = commentRecord.Author;
|
comment.Record.CommentDateUtc = _clock.UtcNow;
|
||||||
comment.Record.CommentDateUtc = commentRecord.CommentDateUtc;
|
comment.Record.CommentText = context.CommentText;
|
||||||
comment.Record.CommentText = commentRecord.CommentText;
|
comment.Record.Email = context.Email;
|
||||||
comment.Record.Email = commentRecord.Email;
|
comment.Record.SiteName = context.SiteName;
|
||||||
comment.Record.SiteName = commentRecord.SiteName;
|
comment.Record.UserName = (CurrentUser == null ? "Anonymous" : CurrentUser.UserName);
|
||||||
comment.Record.UserName = commentRecord.UserName;
|
comment.Record.CommentedOn = context.CommentedOn;
|
||||||
comment.Record.CommentedOn = commentRecord.CommentedOn;
|
|
||||||
|
|
||||||
comment.Record.Status = _commentValidator.ValidateComment(commentRecord) ? CommentStatus.Pending : CommentStatus.Spam;
|
comment.Record.Status = _commentValidator.ValidateComment(comment.Record) ? CommentStatus.Pending : CommentStatus.Spam;
|
||||||
|
|
||||||
// store id of the next layer for large-grained operations, e.g. rss on blog
|
// store id of the next layer for large-grained operations, e.g. rss on blog
|
||||||
//TODO:(rpaquay) Get rid of this (comment aspect takes care of container)
|
//TODO:(rpaquay) Get rid of this (comment aspect takes care of container)
|
||||||
@@ -158,7 +170,9 @@ namespace Orchard.Comments.Services {
|
|||||||
|
|
||||||
public void EnableCommentsForCommentedContent(int id) {
|
public void EnableCommentsForCommentedContent(int id) {
|
||||||
var closedComments = _closedCommentsRepository.Fetch(x => x.ContentItemId == id);
|
var closedComments = _closedCommentsRepository.Fetch(x => x.ContentItemId == id);
|
||||||
closedComments.ForEach(c => _closedCommentsRepository.Delete(c));
|
foreach (var c in closedComments) {
|
||||||
|
_closedCommentsRepository.Delete(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Reference in New Issue
Block a user