- Comments: create comment, commentvalidator service and interface, minor fix to comment filtering in admin...

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041466
This commit is contained in:
suhacan
2009-11-19 23:59:24 +00:00
parent 2b2380c8cc
commit 463490f648
8 changed files with 129 additions and 7 deletions

View File

@@ -1,12 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Web.Mvc; using System.Web.Mvc;
using Orchard.Comments.Models; using Orchard.Comments.Models;
using Orchard.Localization; using Orchard.Localization;
using Orchard.Logging; using Orchard.Logging;
using Orchard.Settings;
using Orchard.UI.Notify; using Orchard.UI.Notify;
using Orchard.Security; using Orchard.Security;
using Orchard.Comments.ViewModels; using Orchard.Comments.ViewModels;
@@ -104,6 +104,34 @@ namespace Orchard.Comments.Controllers {
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
public ActionResult Create() {
return View(new CommentsCreateViewModel());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection input) {
var viewModel = new CommentsCreateViewModel();
try {
UpdateModel(viewModel, input.ToValueProvider());
if (!_authorizer.Authorize(Permissions.AddComment, T("Couldn't add comment")))
return new HttpUnauthorizedResult();
Comment comment = new Comment {
Author = viewModel.Name,
CommentDate = DateTime.Now,
CommentText = viewModel.CommentText,
Email = viewModel.Email,
SiteName = viewModel.SiteName,
UserName = CurrentUser.UserName ?? "Anonymous"
};
_commentService.CreateComment(comment);
return RedirectToAction("Index");
}
catch (Exception exception) {
_notifier.Error(T("Creating Comment failed: " + exception.Message));
return View(viewModel);
}
}
private static CommentEntry CreateCommentEntry(Comment comment) { private static CommentEntry CreateCommentEntry(Comment comment) {
return new CommentEntry { return new CommentEntry {
Comment = comment, Comment = comment,

View File

@@ -4,6 +4,8 @@ namespace Orchard.Comments.Models {
public class Comment { public class Comment {
public virtual int Id { get; set; } public virtual int Id { get; set; }
public virtual string Author { get; set; } public virtual string Author { get; set; }
public virtual string SiteName { get; set; }
public virtual string UserName { get; set; }
public virtual string Email { get; set; } public virtual string Email { get; set; }
public virtual CommentStatus Status { get; set; } public virtual CommentStatus Status { get; set; }
public virtual DateTime CommentDate { get; set; } public virtual DateTime CommentDate { get; set; }

View File

@@ -68,11 +68,14 @@
<Compile Include="Permissions.cs" /> <Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\CommentService.cs" /> <Compile Include="Services\CommentService.cs" />
<Compile Include="Services\CommentValidator.cs" />
<Compile Include="ViewModels\CommentsCreateViewModel.cs" />
<Compile Include="ViewModels\CommentsIndexViewModel.cs" /> <Compile Include="ViewModels\CommentsIndexViewModel.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Package.txt" /> <Content Include="Package.txt" />
<Content Include="Views\Admin\Index.aspx" /> <Content Include="Views\Admin\Index.aspx" />
<Content Include="Views\Admin\Create.aspx" />
<Content Include="Views\Models\EditorTemplates\CommentSettingsRecord.ascx" /> <Content Include="Views\Models\EditorTemplates\CommentSettingsRecord.ascx" />
<Content Include="Web.config" /> <Content Include="Web.config" />
<Content Include="Views\Web.config" /> <Content Include="Views\Web.config" />

View File

@@ -1,28 +1,38 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Orchard.Comments.Models; using Orchard.Comments.Models;
using Orchard.Data; using Orchard.Data;
using Orchard.Logging; using Orchard.Logging;
using Orchard.Security;
using Orchard.Settings;
using Orchard.UI.Notify;
namespace Orchard.Comments.Services { namespace Orchard.Comments.Services {
public interface ICommentService : IDependency { public interface ICommentService : IDependency {
IEnumerable<Comment> GetComments(); IEnumerable<Comment> GetComments();
IEnumerable<Comment> GetComments(CommentStatus status); IEnumerable<Comment> GetComments(CommentStatus status);
Comment GetComment(int id); Comment GetComment(int id);
void CreateComment(Comment comment);
void MarkCommentAsSpam(int commentId); void MarkCommentAsSpam(int commentId);
void DeleteComment(int commentId); void DeleteComment(int commentId);
} }
public class CommentService : ICommentService { public class CommentService : ICommentService {
private readonly IRepository<Comment> _commentRepository; private readonly IRepository<Comment> _commentRepository;
private readonly ICommentValidator _commentValidator;
private readonly IAuthorizer _authorizer;
private readonly INotifier _notifier;
public CommentService(IRepository<Comment> commentRepository) { public CommentService(IRepository<Comment> commentRepository, ICommentValidator commentValidator, IAuthorizer authorizer, INotifier notifier) {
_commentRepository = commentRepository; _commentRepository = commentRepository;
_commentValidator = commentValidator;
_authorizer = authorizer;
_notifier = notifier;
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
} }
public ILogger Logger { get; set; } public ILogger Logger { get; set; }
public ISite CurrentSite { get; set; }
#region Implementation of ICommentService #region Implementation of ICommentService
@@ -31,13 +41,18 @@ namespace Orchard.Comments.Services {
} }
public IEnumerable<Comment> GetComments(CommentStatus status) { public IEnumerable<Comment> GetComments(CommentStatus status) {
return from comment in _commentRepository.Table.ToList() where comment.Status == CommentStatus.Approved select comment; return from comment in _commentRepository.Table.ToList() where comment.Status == status select comment;
} }
public Comment GetComment(int id) { public Comment GetComment(int id) {
return _commentRepository.Get(id); return _commentRepository.Get(id);
} }
public void CreateComment(Comment comment) {
comment.Status = _commentValidator.ValidateComment(comment) ? CommentStatus.Approved : CommentStatus.Spam;
_commentRepository.Create(comment);
}
public void MarkCommentAsSpam(int commentId) { public void MarkCommentAsSpam(int commentId) {
Comment comment = GetComment(commentId); Comment comment = GetComment(commentId);
comment.Status = CommentStatus.Spam; comment.Status = CommentStatus.Spam;

View File

@@ -0,0 +1,25 @@
using Orchard.Comments.Models;
using Orchard.Logging;
namespace Orchard.Comments.Services {
public interface ICommentValidator : IDependency {
bool ValidateComment(Comment comment);
}
public class CommentValidator : ICommentValidator {
public CommentValidator() {
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
#region Implementation of ICommentValidator
public bool ValidateComment(Comment comment) {
//TODO: integrate spam filter.
return true;
}
#endregion
}
}

View File

@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
using Orchard.Mvc.ViewModels;
namespace Orchard.Comments.ViewModels {
public class CommentsCreateViewModel : AdminViewModel {
[Required]
public string Name { get; set; }
[Required]
public string Email { get; set; }
public string SiteName { get; set; }
public string CommentText { get; set; }
}
}

View File

@@ -0,0 +1,35 @@
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CommentsCreateViewModel>" %>
<%@ Import Namespace="Orchard.Comments.ViewModels"%>
<%@ Import Namespace="Orchard.Mvc.Html" %>
<% Html.Include("Header"); %>
<% Html.BeginForm(); %>
<%= Html.ValidationSummary() %>
<div class="yui-g">
<h2 class="separator">Add a Comment</h2>
<h3>Information</h3>
<ol>
<li>
<label for="Name">Name:</label>
<input id="Text1" class="inputText inputTextLarge" name="Name" type="text" value="<%= Model.Name %>" />
</li>
<li>
<label for="Email">Email:</label>
<input id="Email" class="inputText inputTextLarge" name="Email" type="text" value="<%= Model.Email%>" />
</li>
<li>
<label for="SiteName">SiteName:</label>
<input id="SiteName" class="inputText inputTextLarge" name="SiteName" type="text" value="<%= Model.SiteName %>" />
</li>
<li>
<label for="CommentText">Leave a comment</label>
<textarea id="CommentText" rows="10" cols="30" name="CommentText">
<%= Model.CommentText %>
</textarea>
</li>
<li>
<input type="submit" class="button" value="Save" />
</li>
</ol>
</div>
<% Html.EndForm(); %>
<% Html.Include("Footer"); %>

View File

@@ -7,6 +7,7 @@
<div class="yui-g"> <div class="yui-g">
<h2 class="separator">Manage Comments</h2> <h2 class="separator">Manage Comments</h2>
<%=Html.ValidationSummary() %> <%=Html.ValidationSummary() %>
<%=Html.ActionLink("Add a new comment", "Create", new {}, new {@class="floatRight topSpacer"}) %>
<ol class="horizontal actions floatLeft"> <ol class="horizontal actions floatLeft">
<li> <li>
<label class="floatLeft" for="publishActions"> Actions:</label> <label class="floatLeft" for="publishActions"> Actions:</label>
@@ -64,8 +65,8 @@
<td><% if (commentEntry.Comment.Status == CommentStatus.Spam) {%> Spam <% } %> <td><% if (commentEntry.Comment.Status == CommentStatus.Spam) {%> Spam <% } %>
<% else {%> Approved <% } %> <% else {%> Approved <% } %>
</td> </td>
<td><%= commentEntry.Comment.Author %></td> <td><%= commentEntry.Comment.UserName %></td>
<td><%= commentEntry.Comment.CommentText.Substring(32) %></td> <td><%= commentEntry.Comment.CommentText == null ? String.Empty : commentEntry.Comment.CommentText.Substring(32) %></td>
<td><%= commentEntry.Comment.CommentDate %></td> <td><%= commentEntry.Comment.CommentDate %></td>
<td>Link to commented item</td> <td>Link to commented item</td>
</tr> </tr>