mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-22 20:13:50 +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.Logging;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Services;
|
||||
using Orchard.Settings;
|
||||
using Orchard.UI.Notify;
|
||||
using Orchard.Security;
|
||||
@@ -18,14 +19,14 @@ namespace Orchard.Comments.Controllers {
|
||||
[ValidateInput(false)]
|
||||
public class AdminController : Controller {
|
||||
private readonly ICommentService _commentService;
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IAuthorizer _authorizer;
|
||||
private readonly IClock _clock;
|
||||
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;
|
||||
_contentManager = contentManager;
|
||||
_authorizer = authorizer;
|
||||
_clock = clock;
|
||||
_notifier = notifier;
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
@@ -141,17 +142,15 @@ namespace Orchard.Comments.Controllers {
|
||||
return new HttpUnauthorizedResult();
|
||||
}
|
||||
|
||||
CommentRecord commentRecord = new CommentRecord {
|
||||
var context = new CreateCommentContext {
|
||||
Author = viewModel.Name,
|
||||
CommentDateUtc = DateTime.UtcNow,
|
||||
CommentText = viewModel.CommentText,
|
||||
Email = viewModel.Email,
|
||||
SiteName = viewModel.SiteName,
|
||||
UserName = CurrentUser == null ? "Anonymous" : CurrentUser.UserName,
|
||||
CommentedOn = viewModel.CommentedOn,
|
||||
};
|
||||
|
||||
var comment = _commentService.CreateComment(commentRecord);
|
||||
var comment = _commentService.CreateComment(context);
|
||||
|
||||
if (!String.IsNullOrEmpty(returnUrl)) {
|
||||
return Redirect(returnUrl);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Castle.Core;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Comments.Models;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
@@ -8,6 +8,7 @@ using Orchard.Data;
|
||||
using Orchard.Logging;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Security;
|
||||
using Orchard.Services;
|
||||
using Orchard.Settings;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
@@ -19,7 +20,7 @@ namespace Orchard.Comments.Services {
|
||||
IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status);
|
||||
Comment GetComment(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 ApproveComment(int commentId);
|
||||
void PendComment(int commentId);
|
||||
@@ -30,27 +31,39 @@ namespace Orchard.Comments.Services {
|
||||
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]
|
||||
public class CommentService : ICommentService {
|
||||
private readonly IRepository<ClosedCommentsRecord> _closedCommentsRepository;
|
||||
private readonly ICommentValidator _commentValidator;
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IClock _clock;
|
||||
|
||||
public CommentService(IRepository<CommentRecord> commentRepository,
|
||||
IRepository<ClosedCommentsRecord> closedCommentsRepository,
|
||||
IRepository<HasCommentsRecord> hasCommentsRepository,
|
||||
ICommentValidator commentValidator,
|
||||
IContentManager contentManager,
|
||||
IClock clock,
|
||||
IAuthorizer authorizer,
|
||||
INotifier notifier) {
|
||||
_closedCommentsRepository = closedCommentsRepository;
|
||||
_commentValidator = commentValidator;
|
||||
_contentManager = contentManager;
|
||||
_clock = clock;
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
||||
protected virtual IUser CurrentUser { get; [UsedImplicitly] private set; }
|
||||
|
||||
|
||||
#region Implementation of ICommentService
|
||||
@@ -94,19 +107,18 @@ namespace Orchard.Comments.Services {
|
||||
return _contentManager.GetItemMetadata(content);
|
||||
}
|
||||
|
||||
public Comment CreateComment(CommentRecord commentRecord) {
|
||||
public Comment CreateComment(CreateCommentContext context) {
|
||||
var comment = _contentManager.Create<Comment>("comment");
|
||||
|
||||
//TODO:(rpaquay) CommentRecord should never be used as "data object"
|
||||
comment.Record.Author = commentRecord.Author;
|
||||
comment.Record.CommentDateUtc = commentRecord.CommentDateUtc;
|
||||
comment.Record.CommentText = commentRecord.CommentText;
|
||||
comment.Record.Email = commentRecord.Email;
|
||||
comment.Record.SiteName = commentRecord.SiteName;
|
||||
comment.Record.UserName = commentRecord.UserName;
|
||||
comment.Record.CommentedOn = commentRecord.CommentedOn;
|
||||
comment.Record.Author = context.Author;
|
||||
comment.Record.CommentDateUtc = _clock.UtcNow;
|
||||
comment.Record.CommentText = context.CommentText;
|
||||
comment.Record.Email = context.Email;
|
||||
comment.Record.SiteName = context.SiteName;
|
||||
comment.Record.UserName = (CurrentUser == null ? "Anonymous" : CurrentUser.UserName);
|
||||
comment.Record.CommentedOn = context.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
|
||||
//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) {
|
||||
var closedComments = _closedCommentsRepository.Fetch(x => x.ContentItemId == id);
|
||||
closedComments.ForEach(c => _closedCommentsRepository.Delete(c));
|
||||
foreach (var c in closedComments) {
|
||||
_closedCommentsRepository.Delete(c);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
Reference in New Issue
Block a user