diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Controllers/BlogController.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Controllers/BlogController.cs index 041857991..9be8d2729 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Controllers/BlogController.cs +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Controllers/BlogController.cs @@ -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(); diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Controllers/BlogPostController.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Controllers/BlogPostController.cs new file mode 100644 index 000000000..214159add --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Controllers/BlogPostController.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPost.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPost.cs index c6e66ced4..769b31be2 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPost.cs +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPost.cs @@ -2,6 +2,8 @@ using Orchard.Models; namespace Orchard.Blogs.Models { public class BlogPost : ContentPartForRecord { - + public string BlogSlug { get { return Record.Blog.Slug; } } + public string Title { get { return Record.Title; } } + public string Slug { get { return Record.Slug; } } } } \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPostRecord.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPostRecord.cs index 92294ddad..5569c3d32 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPostRecord.cs +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogPostRecord.cs @@ -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; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Orchard.Blogs.csproj b/src/Orchard.Web/Packages/Orchard.Blogs/Orchard.Blogs.csproj index 6e8b22f82..e0000862a 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Orchard.Blogs.csproj +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Orchard.Blogs.csproj @@ -61,7 +61,9 @@ + + @@ -73,6 +75,8 @@ + + @@ -81,6 +85,7 @@ + diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Routes.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Routes.cs index 91469bca9..9ce5fe203 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Routes.cs +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Routes.cs @@ -4,31 +4,46 @@ using System.Web.Routing; using Orchard.Mvc.Routes; namespace Orchard.Blogs { - //public class Routes : IRouteProvider { - // public void GetRoutes(ICollection routes) { - // foreach (var routeDescriptor in GetRoutes()) - // routes.Add(routeDescriptor); - // } + public class Routes : IRouteProvider + { + public void GetRoutes(ICollection routes) { + foreach (var routeDescriptor in GetRoutes()) + routes.Add(routeDescriptor); + } - // public IEnumerable 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 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()) + } + }; + } + } } \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Routing/IsBlogConstraint.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Routing/IsBlogConstraint.cs new file mode 100644 index 000000000..5399a0fa1 --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Routing/IsBlogConstraint.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogPostService.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogPostService.cs new file mode 100644 index 000000000..6e9fb5ff1 --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogPostService.cs @@ -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 _repository; + + public BlogPostService(IContentManager contentManager, IRepository 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() : null; + } + + public IEnumerable Get(Blog blog) { + //TODO: (erikpo) Sort by published desc + IEnumerable 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()); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogService.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogService.cs index b15e58140..155e119fd 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogService.cs +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogService.cs @@ -9,27 +9,26 @@ namespace Orchard.Blogs.Services { private readonly IContentManager _contentManager; private readonly IRepository _repository; - public BlogService(IContentManager contentManager, IRepository blogPostRepository) { + public BlogService(IContentManager contentManager, IRepository 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() : null; } public IEnumerable Get() { - IEnumerable blogs =_repository.Fetch(br => br.Enabled, bpr => bpr.Asc(bpr2 => bpr2.Name)); + IEnumerable blogs =_repository.Fetch(br => br.Enabled, br => br.Asc(br2 => br2.Name)); return blogs.Select(br => _contentManager.Get(br.Id).As()); } 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"); diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Services/IBlogPostService.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Services/IBlogPostService.cs new file mode 100644 index 000000000..700a9a937 --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Services/IBlogPostService.cs @@ -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 Get(Blog blog); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/Item.aspx b/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/Item.aspx index ca3927bb3..892028e9b 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/Item.aspx +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/Item.aspx @@ -5,6 +5,6 @@ <% Html.Include("Header"); %>

Blog

-
<%=Html.Encode(Model.Slug) %>
+
<%=Html.Encode(Model.Name) %>
<% Html.Include("Footer"); %> \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/List.aspx b/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/List.aspx index a4b9e6609..a8dfbf3d7 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/List.aspx +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Views/Blog/List.aspx @@ -9,7 +9,7 @@ if (Model.Count() > 0) { %> <% } %> diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Views/BlogPost/ListByBlog.aspx b/src/Orchard.Web/Packages/Orchard.Blogs/Views/BlogPost/ListByBlog.aspx new file mode 100644 index 000000000..bd31f3838 --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Views/BlogPost/ListByBlog.aspx @@ -0,0 +1,17 @@ +<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage>" %> +<%@ Import Namespace="Orchard.Blogs.Models"%> +<%@ Import Namespace="Orchard.Mvc.Html"%> +<%@ Import Namespace="Orchard.Mvc.ViewModels"%> +<% Html.Include("Header"); %> +
+

Posts

<% + //TODO: (erikpo) Replace this with an Html extension method of some sort (ListForModel?) + if (Model.Count() > 0) { %> + <% + } %> +
+<% Html.Include("Footer"); %> \ No newline at end of file