Fixing up urls for blogs and setting up some initial services, controller actions, routes, etc for BlogPost.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041606
This commit is contained in:
ErikPorter
2009-11-21 00:38:41 +00:00
parent 0ef4d4d7e4
commit b919cacbf7
13 changed files with 173 additions and 37 deletions

View File

@@ -16,8 +16,8 @@ namespace Orchard.Blogs.Controllers {
}
//TODO: (erikpo) Should think about moving the slug parameter and get call and null check up into a model binder or action filter
public ActionResult Item(string slug) {
Blog blog = _blogService.Get(slug);
public ActionResult Item(string blogSlug) {
Blog blog = _blogService.Get(blogSlug);
if (blog == null)
return new NotFoundResult();

View File

@@ -0,0 +1,40 @@
using System.Web.Mvc;
using Orchard.Blogs.Models;
using Orchard.Blogs.Services;
using Orchard.Mvc.Results;
namespace Orchard.Blogs.Controllers {
public class BlogPostController : Controller {
private readonly IBlogService _blogService;
private readonly IBlogPostService _blogPostService;
public BlogPostController(IBlogService blogService, IBlogPostService blogPostService) {
_blogService = blogService;
_blogPostService = blogPostService;
}
public ActionResult ListByBlog(string blogSlug) {
Blog blog = _blogService.Get(blogSlug);
if (blog == null)
return new NotFoundResult();
return View(_blogPostService.Get(blog));
}
//TODO: (erikpo) Should think about moving the slug parameters and get calls and null checks up into a model binder or action filter
public ActionResult Item(string blogSlug, string postSlug) {
Blog blog = _blogService.Get(blogSlug);
if (blog == null)
return new NotFoundResult();
BlogPost post = _blogPostService.Get(blog, postSlug);
if (post == null)
return new NotFoundResult();
return View(post);
}
}
}

View File

@@ -2,6 +2,8 @@ using Orchard.Models;
namespace Orchard.Blogs.Models {
public class BlogPost : ContentPartForRecord<BlogPostRecord> {
public string BlogSlug { get { return Record.Blog.Slug; } }
public string Title { get { return Record.Title; } }
public string Slug { get { return Record.Slug; } }
}
}

View File

@@ -3,6 +3,7 @@ using Orchard.Models.Records;
namespace Orchard.Blogs.Models {
public class BlogPostRecord : ContentPartRecord {
public virtual BlogRecord Blog { get; set; }
public virtual string Title { get; set; }
public virtual string Slug { get; set; }
}
}

View File

@@ -61,7 +61,9 @@
<Reference Include="System.Web.Mobile" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\BlogPostController.cs" />
<Compile Include="Extensions\BlogCreateViewModelExtensions.cs" />
<Compile Include="Routing\IsBlogConstraint.cs" />
<Compile Include="Services\BlogService.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Controllers\BlogController.cs" />
@@ -73,6 +75,8 @@
<Compile Include="Models\BlogRecord.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Routes.cs" />
<Compile Include="Services\BlogPostService.cs" />
<Compile Include="Services\IBlogPostService.cs" />
<Compile Include="Services\CreateBlogParams.cs" />
<Compile Include="Services\IBlogService.cs" />
<Compile Include="ViewModels\CreateBlogViewModel.cs" />
@@ -81,6 +85,7 @@
<Content Include="Package.txt" />
<Content Include="Views\Admin\Create.aspx" />
<Content Include="Views\Admin\Index.aspx" />
<Content Include="Views\BlogPost\ListByBlog.aspx" />
<Content Include="Views\Blog\Item.aspx" />
<Content Include="Views\Blog\List.aspx" />
<Content Include="Web.config" />

View File

@@ -4,31 +4,46 @@ using System.Web.Routing;
using Orchard.Mvc.Routes;
namespace Orchard.Blogs {
//public class Routes : IRouteProvider {
// public void GetRoutes(ICollection<RouteDescriptor> routes) {
// foreach (var routeDescriptor in GetRoutes())
// routes.Add(routeDescriptor);
// }
public class Routes : IRouteProvider
{
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
// public IEnumerable<RouteDescriptor> GetRoutes() {
// return new[] {
// new RouteDescriptor {
// Route = new Route(
// "{*slug}",
// new RouteValueDictionary {
// {"area", "Orchard.CmsPages"},
// {"controller", "templates"},
// {"action", "show"}
// },
// new RouteValueDictionary {
// {"slug", ""}
// },
// new RouteValueDictionary {
// {"area", "Orchard.CmsPages"}
// },
// new MvcRouteHandler())
// }
// };
// }
//}
public IEnumerable<RouteDescriptor> GetRoutes() {
return new[] {
new RouteDescriptor {
Route = new Route(
"Blogs",
new RouteValueDictionary {
{"area", "Orchard.Blogs"},
{"controller", "Blog"},
{"action", "List"}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "Orchard.Blogs"}
},
new MvcRouteHandler())
},
new RouteDescriptor {
Route = new Route(
"{blogSlug}",
new RouteValueDictionary {
{"area", "Orchard.Blogs"},
{"controller", "Blog"},
{"action", "Item"}
},
new RouteValueDictionary()/* {
{"blogSlug", new IsBlogConstraint()}
}*/,
new RouteValueDictionary {
{"area", "Orchard.Blogs"}
},
new MvcRouteHandler())
}
};
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Web;
using System.Web.Routing;
using Orchard.Blogs.Services;
namespace Orchard.Blogs.Routing {
public class IsBlogConstraint : IRouteConstraint {
private readonly IBlogService _blogService;
public IsBlogConstraint(IBlogService blogService) {
_blogService = blogService;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
return _blogService.Get(values[parameterName].ToString()) != null;
}
}
}

View File

@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using Orchard.Blogs.Models;
using Orchard.Data;
using Orchard.Models;
namespace Orchard.Blogs.Services {
public class BlogPostService : IBlogPostService {
private readonly IContentManager _contentManager;
private readonly IRepository<BlogPostRecord> _repository;
public BlogPostService(IContentManager contentManager, IRepository<BlogPostRecord> repository) {
_contentManager = contentManager;
_repository = repository;
}
public BlogPost Get(Blog blog, string slug) {
BlogPostRecord record = _repository.Get(bpr => bpr.Blog.Id == blog.Record.Id && bpr.Blog.Enabled && bpr.Slug == slug);
ContentItem item = _contentManager.Get(record.Id);
return item != null ? item.As<BlogPost>() : null;
}
public IEnumerable<BlogPost> Get(Blog blog) {
//TODO: (erikpo) Sort by published desc
IEnumerable<BlogPostRecord> items =_repository.Fetch(bpr => bpr.Blog.Id == blog.Record.Id && bpr.Blog.Enabled/*, bpr => bpr.Asc(bpr2 => bpr2.Slug)*/);
return items.Select(br => _contentManager.Get(br.Id).As<BlogPost>());
}
}
}

View File

@@ -9,27 +9,26 @@ namespace Orchard.Blogs.Services {
private readonly IContentManager _contentManager;
private readonly IRepository<BlogRecord> _repository;
public BlogService(IContentManager contentManager, IRepository<BlogRecord> blogPostRepository) {
public BlogService(IContentManager contentManager, IRepository<BlogRecord> repository) {
_contentManager = contentManager;
_repository = blogPostRepository;
_repository = repository;
}
public Blog Get(string slug) {
BlogRecord record = _repository.Get(br => br.Slug == slug);
BlogRecord record = _repository.Get(br => br.Slug == slug && br.Enabled);
ContentItem item = _contentManager.Get(record.Id);
return item != null ? item.As<Blog>() : null;
}
public IEnumerable<Blog> Get() {
IEnumerable<BlogRecord> blogs =_repository.Fetch(br => br.Enabled, bpr => bpr.Asc(bpr2 => bpr2.Name));
IEnumerable<BlogRecord> blogs =_repository.Fetch(br => br.Enabled, br => br.Asc(br2 => br2.Name));
return blogs.Select(br => _contentManager.Get(br.Id).As<Blog>());
}
public Blog CreateBlog(CreateBlogParams parameters) {
BlogRecord record = new BlogRecord()
{Name = parameters.Name, Slug = parameters.Slug, Enabled = parameters.Enabled};
BlogRecord record = new BlogRecord() {Name = parameters.Name, Slug = parameters.Slug, Enabled = parameters.Enabled};
//TODO: (erikpo) Need an extension method or something for this default behavior
ContentItem contentItem = _contentManager.New("blog");

View File

@@ -0,0 +1,9 @@
using System.Collections.Generic;
using Orchard.Blogs.Models;
namespace Orchard.Blogs.Services {
public interface IBlogPostService : IDependency {
BlogPost Get(Blog blog, string slug);
IEnumerable<BlogPost> Get(Blog blog);
}
}

View File

@@ -5,6 +5,6 @@
<% Html.Include("Header"); %>
<div class="yui-g">
<h2>Blog</h2>
<div><%=Html.Encode(Model.Slug) %></div>
<div><%=Html.Encode(Model.Name) %></div>
</div>
<% Html.Include("Footer"); %>

View File

@@ -9,7 +9,7 @@
if (Model.Count() > 0) { %>
<ul><%
foreach (Blog blog in Model) { %>
<li><a href="<%=Url.Action("Item", "Blog", new {slug = blog.Slug}) %>"><%=Html.Encode(blog.Name) %></a></li><%
<li><a href="<%=Url.Action("Item", "Blog", new {blogSlug = blog.Slug}) %>"><%=Html.Encode(blog.Name) %></a></li><%
} %>
</ul><%
} %>

View File

@@ -0,0 +1,17 @@
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<BlogPost>>" %>
<%@ Import Namespace="Orchard.Blogs.Models"%>
<%@ Import Namespace="Orchard.Mvc.Html"%>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<% Html.Include("Header"); %>
<div class="yui-g">
<h2>Posts</h2><%
//TODO: (erikpo) Replace this with an Html extension method of some sort (ListForModel?)
if (Model.Count() > 0) { %>
<ul><%
foreach (BlogPost post in Model) { %>
<li><a href="<%=Url.Action("Item", "BlogPost", new {blogSlug = post.BlogSlug, postSlug = post.Slug}) %>"><%=Html.Encode(post.Title) %></a></li><%
} %>
</ul><%
} %>
</div>
<% Html.Include("Footer"); %>