mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-24 13:33:34 +08:00
Moving create, update, delete actions from BlogController and BlogPostController into BlogAdminController and BlogPostController for consistency.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045254
This commit is contained in:
@@ -1,36 +1,24 @@
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Blogs.Extensions;
|
||||
using Orchard.Blogs.Models;
|
||||
using Orchard.Blogs.Services;
|
||||
using Orchard.Blogs.ViewModels;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Mvc.Results;
|
||||
using Orchard.Security;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Blogs.Controllers {
|
||||
[ValidateInput(false)]
|
||||
public class BlogController : Controller, IUpdateModel {
|
||||
public class BlogController : Controller {
|
||||
private readonly IOrchardServices _services;
|
||||
private readonly ISessionLocator _sessionLocator;
|
||||
private readonly IAuthorizer _authorizer;
|
||||
private readonly INotifier _notifier;
|
||||
private readonly IBlogService _blogService;
|
||||
|
||||
public BlogController(IOrchardServices services, ISessionLocator sessionLocator, IAuthorizer authorizer, INotifier notifier, IBlogService blogService) {
|
||||
_services = services;
|
||||
_sessionLocator = sessionLocator;
|
||||
_authorizer = authorizer;
|
||||
_notifier = notifier;
|
||||
_blogService = blogService;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
private Localizer T { get; set; }
|
||||
|
||||
public ActionResult List() {
|
||||
var model = new BlogsViewModel {
|
||||
Blogs = _blogService.Get().Select(b => _services.ContentManager.BuildDisplayModel(b, "Summary"))
|
||||
@@ -52,109 +40,5 @@ namespace Orchard.Blogs.Controllers {
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
public ActionResult Create() {
|
||||
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
||||
if (!_authorizer.Authorize(Permissions.CreateBlog, T("Not allowed to create blogs")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
Blog blog = _services.ContentManager.New<Blog>("blog");
|
||||
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var model = new CreateBlogViewModel {
|
||||
Blog = _services.ContentManager.BuildEditorModel(blog)
|
||||
};
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Create(CreateBlogViewModel model) {
|
||||
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
||||
if (!_authorizer.Authorize(Permissions.CreateBlog, T("Couldn't create blog")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
model.Blog = _services.ContentManager.UpdateEditorModel(_services.ContentManager.New<Blog>("blog"), this);
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return View(model);
|
||||
|
||||
_services.ContentManager.Create(model.Blog.Item.ContentItem);
|
||||
|
||||
//TEMP: (erikpo) ensure information has committed for this record
|
||||
var session = _sessionLocator.For(typeof(BlogRecord));
|
||||
session.Flush();
|
||||
|
||||
return Redirect(Url.BlogForAdmin(model.Blog.Item.Slug));
|
||||
}
|
||||
|
||||
public ActionResult Edit(string blogSlug) {
|
||||
//TODO: (erikpo) Might think about moving this to an ActionFilter/Attribute
|
||||
if (!_authorizer.Authorize(Permissions.ModifyBlog, T("Not allowed to edit blog")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var model = new BlogEditViewModel {
|
||||
Blog = _services.ContentManager.BuildEditorModel(blog)
|
||||
};
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Edit(string blogSlug, FormCollection input) {
|
||||
if (!_authorizer.Authorize(Permissions.ModifyBlog, T("Couldn't edit blog")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var model = new BlogEditViewModel {
|
||||
Blog = _services.ContentManager.UpdateEditorModel(blog, this)
|
||||
};
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return View(model);
|
||||
|
||||
_notifier.Information(T("Blog information updated"));
|
||||
|
||||
return Redirect(Url.BlogsForAdmin());
|
||||
}
|
||||
|
||||
//[HttpPost] <- todo: (heskew) make all add/edit/remove POST only and verify the AntiForgeryToken
|
||||
public ActionResult Delete(string blogSlug) {
|
||||
if (!_authorizer.Authorize(Permissions.DeleteBlog, T("Couldn't delete blog")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
//TODO: (erikpo) Move looking up the current blog up into a modelbinder
|
||||
Blog blog = _blogService.Get(blogSlug);
|
||||
|
||||
if (blog == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
_blogService.Delete(blog);
|
||||
|
||||
_notifier.Information(T("Blog was successfully deleted"));
|
||||
|
||||
return Redirect(Url.BlogsForAdmin());
|
||||
}
|
||||
|
||||
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
|
||||
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
||||
}
|
||||
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
|
||||
ModelState.AddModelError(key, errorMessage.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user