From e0db263538fd94fb46136387c19258b3c8ae0f30 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Wed, 13 Oct 2010 15:42:36 -0700 Subject: [PATCH] Adding Blog pagination - Yeah !!!! --HG-- branch : dev --- .../Controllers/BlogController.cs | 27 +++++++++++++---- .../Orchard.Blogs/Orchard.Blogs.csproj | 4 ++- .../Modules/Orchard.Blogs/Routes.cs | 18 ++++++++++++ .../Orchard.Blogs/Services/BlogPostService.cs | 5 ++++ .../Services/IBlogPostService.cs | 1 + .../Modules/Orchard.Blogs/Styles/admin.css | 2 +- .../Orchard.Blogs/Styles/pagination.css | 19 ++++++++++++ .../ViewModels/DisplayBlogViewModel.cs | 10 +++++++ .../Orchard.Blogs/Views/Blog/Item.cshtml | 29 ++++++++++++++++++- .../Items/Content.Summary.BlogPost.cshtml | 2 +- 10 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Blogs/Styles/pagination.css create mode 100644 src/Orchard.Web/Modules/Orchard.Blogs/ViewModels/DisplayBlogViewModel.cs diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogController.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogController.cs index 3e1371533..61c82efa8 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogController.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogController.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Web; using System.Web.Mvc; @@ -6,6 +7,7 @@ using System.Xml.Linq; using Orchard.Blogs.Models; using Orchard.Blogs.Routing; using Orchard.Blogs.Services; +using Orchard.Blogs.ViewModels; using Orchard.DisplayManagement; using Orchard.Logging; using Orchard.Mvc.Results; @@ -16,12 +18,14 @@ namespace Orchard.Blogs.Controllers { public class BlogController : Controller { private readonly IOrchardServices _services; private readonly IBlogService _blogService; + private readonly IBlogPostService _blogPostService; private readonly IBlogSlugConstraint _blogSlugConstraint; private readonly RouteCollection _routeCollection; - public BlogController(IOrchardServices services, IBlogService blogService, IBlogSlugConstraint blogSlugConstraint, RouteCollection routeCollection, IShapeHelperFactory shapeHelperFactory) { + public BlogController(IOrchardServices services, IBlogService blogService, IBlogPostService blogPostService, IBlogSlugConstraint blogSlugConstraint, RouteCollection routeCollection, IShapeHelperFactory shapeHelperFactory) { _services = services; _blogService = blogService; + _blogPostService = blogPostService; _blogSlugConstraint = blogSlugConstraint; _routeCollection = routeCollection; Logger = NullLogger.Instance; @@ -44,18 +48,31 @@ namespace Orchard.Blogs.Controllers { } //TODO: (erikpo) Should move the slug parameter and get call and null check up into a model binder - public ActionResult Item(string blogSlug) { + public ActionResult Item(string blogSlug, int page) { + const int pageSize = 10; + var correctedSlug = _blogSlugConstraint.FindSlug(blogSlug); if (correctedSlug == null) return new NotFoundResult(); - var blog = _blogService.Get(correctedSlug); + BlogPart blog = _blogService.Get(correctedSlug); if (blog == null) return new NotFoundResult(); - //todo: (heskew) "Blog" should be an alternative instead of a display type - var model = _services.ContentManager.BuildDisplay(blog, "Blog"); + var blogPosts = _blogPostService.Get(blog, (page - 1)*pageSize, pageSize).Select(b => _services.ContentManager.BuildDisplay(b, "Summary.BlogPost")); + + var list = Shape.List(); + list.AddRange(blogPosts); + + var model = new DisplayBlogViewModel { + BlogPostList = list, + BlogPart = blog, + Page = page, + PageSize = pageSize + }; + return View(model); + } public ActionResult LiveWriterManifest(string blogSlug) { diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Orchard.Blogs.csproj b/src/Orchard.Web/Modules/Orchard.Blogs/Orchard.Blogs.csproj index 59cf0b655..3a5307772 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Orchard.Blogs.csproj +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Orchard.Blogs.csproj @@ -107,6 +107,7 @@ + @@ -118,6 +119,7 @@ + @@ -131,7 +133,6 @@ - Code @@ -190,6 +191,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs index 1dec7508b..e979ab47f 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Routes.cs @@ -259,6 +259,24 @@ namespace Orchard.Blogs { Priority = 11, Route = new Route( "{blogSlug}", + new RouteValueDictionary { + {"area", "Orchard.Blogs"}, + {"controller", "Blog"}, + {"action", "Item"}, + {"page", 1} + }, + new RouteValueDictionary { + {"blogSlug", _blogSlugConstraint} + }, + new RouteValueDictionary { + {"area", "Orchard.Blogs"} + }, + new MvcRouteHandler()) + }, + new RouteDescriptor { + Priority = 11, + Route = new Route( + "{blogSlug}/Page/{*page}", new RouteValueDictionary { {"area", "Orchard.Blogs"}, {"controller", "Blog"}, diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogPostService.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogPostService.cs index 41ad67258..f6353f6de 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogPostService.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogPostService.cs @@ -49,6 +49,11 @@ namespace Orchard.Blogs.Services { return GetBlogQuery(blogPart, versionOptions).List().Select(ci => ci.As()); } + + public IEnumerable Get(BlogPart blogPart, int skip, int count) { + return GetBlogQuery(blogPart, VersionOptions.Published).Slice(skip, count).ToList().Select(ci => ci.As()); + } + public IEnumerable Get(BlogPart blogPart, ArchiveData archiveData) { var query = GetBlogQuery(blogPart, VersionOptions.Published); diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Services/IBlogPostService.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Services/IBlogPostService.cs index 5a1810689..713ac10da 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Services/IBlogPostService.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Services/IBlogPostService.cs @@ -12,6 +12,7 @@ namespace Orchard.Blogs.Services { IEnumerable Get(BlogPart blogPart); IEnumerable Get(BlogPart blogPart, VersionOptions versionOptions); IEnumerable Get(BlogPart blogPart, ArchiveData archiveData); + IEnumerable Get(BlogPart blogPart, int skip, int count); IEnumerable> GetArchives(BlogPart blogPart); void Delete(BlogPostPart blogPostPart); void Publish(BlogPostPart blogPostPart); diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Styles/admin.css b/src/Orchard.Web/Modules/Orchard.Blogs/Styles/admin.css index ee764226d..c2189359d 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Styles/admin.css +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Styles/admin.css @@ -1,3 +1,3 @@ #main .blog-description p { margin-bottom:1em; -} \ No newline at end of file +} diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Styles/pagination.css b/src/Orchard.Web/Modules/Orchard.Blogs/Styles/pagination.css new file mode 100644 index 000000000..719fb28f4 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Styles/pagination.css @@ -0,0 +1,19 @@ +ul.pagination +{ + list-style-type:none; +} + +ul.pagination li a +{ + font-size:13px; +} + +ul.pagination li.newer +{ + float:left; +} + +ul.pagination li.older +{ + float:right; +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/ViewModels/DisplayBlogViewModel.cs b/src/Orchard.Web/Modules/Orchard.Blogs/ViewModels/DisplayBlogViewModel.cs new file mode 100644 index 000000000..626690efa --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Blogs/ViewModels/DisplayBlogViewModel.cs @@ -0,0 +1,10 @@ +using Orchard.Blogs.Models; + +namespace Orchard.Blogs.ViewModels { + public class DisplayBlogViewModel { + public BlogPart BlogPart { get; set; } + public dynamic BlogPostList { get; set; } + public int Page { get; set; } + public int PageSize { get; set; } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Views/Blog/Item.cshtml b/src/Orchard.Web/Modules/Orchard.Blogs/Views/Blog/Item.cshtml index 631e9eb0f..609b08f2f 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Views/Blog/Item.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Views/Blog/Item.cshtml @@ -1 +1,28 @@ -@Display(Model) \ No newline at end of file +@model Orchard.Blogs.ViewModels.DisplayBlogViewModel +@using Orchard.Blogs.Extensions; + +@{ + Style.Include("pagination.css"); +} + +@if (Model.BlogPostList.Items.Count > 0) { + @Display(Model.BlogPostList) + +
    + @if(Model.BlogPostList.Items.Count == Model.PageSize) { +
  • + @Html.ActionLink(T("Older Posts").Text, "Item", new { Area = "Orchard.Blogs", blogSlug = Model.BlogPart.Slug, page = Model.Page + 1 }) +
  • + } + + @if(Model.Page > 1) { +
  • + @Html.ActionLink(T("Newer Posts").Text, "Item", new { Area = "Orchard.Blogs", blogSlug = Model.BlogPart.Slug, page = Model.Page - 1 }) +
  • + } +
+} +else { +

@T("There are no posts for this blog.")

+} + diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Views/Items/Content.Summary.BlogPost.cshtml b/src/Orchard.Web/Modules/Orchard.Blogs/Views/Items/Content.Summary.BlogPost.cshtml index c4f84a8bc..e217cb228 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Views/Items/Content.Summary.BlogPost.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Views/Items/Content.Summary.BlogPost.cshtml @@ -5,4 +5,4 @@ @using Orchard.Core.Common.ViewModels;

@Html.Link((string)Model.Title, Url.BlogPost((BlogPostPart)Model.ContentItem.Get(typeof(BlogPostPart))))

@Html.PublishedState(new CommonMetadataViewModel((CommonPart)Model.ContentItem.Get(typeof(CommonPart))), T) | @Display(Model.meta)
-
@Display(Model.Primary)
\ No newline at end of file +
@Display(Model.Primary)