Made blogpost getting its blog property filled more generic (fixes last checkin)

--HG--
branch : dev
This commit is contained in:
Erik Porter
2010-03-08 16:45:11 -08:00
parent ec62b5e2dd
commit 6b27c2b2c7
3 changed files with 29 additions and 22 deletions

View File

@@ -40,13 +40,7 @@ namespace Orchard.Core.Common.Handlers {
var method = driverType.GetMethod("GetDisplayRouteValues");
if (method != null) {
try {
return
(RouteValueDictionary)
method.Invoke(driver,
new object[] {contentItem.Get(driverType.BaseType.GetGenericArguments()[0])});
}
catch {}
return (RouteValueDictionary)method.Invoke(driver, new object[] {contentItem.Get(driverType.BaseType.GetGenericArguments()[0])});
}
return null;

View File

@@ -27,16 +27,14 @@ namespace Orchard.Blogs.Controllers {
public IOrchardServices Services { get; set; }
private Localizer T { get; set; }
public ActionResult Create(string blogSlug) {
public ActionResult Create() {
if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, T("Not allowed to create blog post")))
return new HttpUnauthorizedResult();
Blog blog = _blogService.Get(blogSlug);
if (blog == null)
return new NotFoundResult();
var blogPost = Services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
blogPost.Blog = blog;
if (blogPost.Blog == null)
return new NotFoundResult();
var model = new CreateBlogPostViewModel {
BlogPost = Services.ContentManager.BuildEditorModel(blogPost)
@@ -46,17 +44,15 @@ namespace Orchard.Blogs.Controllers {
}
[HttpPost]
public ActionResult Create(string blogSlug, CreateBlogPostViewModel model) {
public ActionResult Create(CreateBlogPostViewModel model) {
if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't create blog post")))
return new HttpUnauthorizedResult();
Blog blog = _blogService.Get(blogSlug);
if (blog == null)
var blogPost = Services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
if (blogPost.Blog == null)
return new NotFoundResult();
// Validate form input
var blogPost = Services.ContentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
blogPost.Blog = blog;
model.BlogPost = Services.ContentManager.UpdateEditorModel(blogPost, this);
if (!ModelState.IsValid) {
@@ -89,11 +85,11 @@ namespace Orchard.Blogs.Controllers {
if (!Services.Authorizer.Authorize(Permissions.EditBlogPost, T("Couldn't edit blog post")))
return new HttpUnauthorizedResult();
Blog blog = _blogService.Get(blogSlug);
var blog = _blogService.Get(blogSlug);
if (blog == null)
return new NotFoundResult();
BlogPost post = _blogPostService.Get(postId, VersionOptions.Latest);
var post = _blogPostService.Get(postId, VersionOptions.Latest);
if (post == null)
return new NotFoundResult();

View File

@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Web.Routing;
using JetBrains.Annotations;
using Orchard.Blogs.Drivers;
using Orchard.Blogs.Models;
@@ -18,7 +19,7 @@ namespace Orchard.Blogs.Handlers {
private readonly IRoutableService _routableService;
private readonly IOrchardServices _orchardServices;
public BlogPostHandler(IBlogPostService blogPostService, IRoutableService routableService, IOrchardServices orchardServices) {
public BlogPostHandler(IBlogService blogService, IBlogPostService blogPostService, IRoutableService routableService, IOrchardServices orchardServices, RequestContext requestContext) {
_blogPostService = blogPostService;
_routableService = routableService;
_orchardServices = orchardServices;
@@ -41,6 +42,22 @@ namespace Orchard.Blogs.Handlers {
blog.PostCount = posts.Count;
});
OnActivated<BlogPost>((context, bp) => {
var blogSlug = requestContext.RouteData.Values.ContainsKey("blogSlug") ? requestContext.RouteData.Values["blogSlug"] as string : null;
if (!string.IsNullOrEmpty(blogSlug)) {
bp.Blog = blogService.Get(blogSlug);
return;
}
var containerId = requestContext.HttpContext.Request.Form["containerId"];
if (!string.IsNullOrEmpty(containerId)) {
int cId;
if (int.TryParse(containerId, out cId)) {
bp.Blog = context.ContentItem.ContentManager.Get(cId).As<Blog>();
return;
}
}
});
OnCreated<BlogPost>((context, bp) => updateBlogPostCount(bp.Blog));
OnPublished<BlogPost>((context, bp) => updateBlogPostCount(bp.Blog));
OnVersioned<BlogPost>((context, bp1, bp2) => updateBlogPostCount(bp2.Blog));