From 09c88f124c6493a0edfea57a5847d00f84239f7c Mon Sep 17 00:00:00 2001 From: ErikPorter Date: Fri, 22 Jan 2010 21:18:08 +0000 Subject: [PATCH] Hooked up blog archives ui to real data. Blog archives is now done. --HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045856 --- .../Models/BlogArchiveHandler.cs | 54 +++++++++++++++++++ .../Orchard.Blogs/Models/BlogArchiveRecord.cs | 9 ++++ .../Orchard.Blogs/Orchard.Blogs.csproj | 2 + .../Orchard.Blogs/Services/BlogPostService.cs | 39 ++++++-------- 4 files changed, 80 insertions(+), 24 deletions(-) create mode 100644 src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveHandler.cs create mode 100644 src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveRecord.cs diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveHandler.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveHandler.cs new file mode 100644 index 000000000..e61547de1 --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveHandler.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; +using Orchard.ContentManagement.Handlers; +using Orchard.Core.Common.Records; +using Orchard.Data; + +namespace Orchard.Blogs.Models { + [UsedImplicitly] + public class BlogArchiveHandler : ContentHandler { + public BlogArchiveHandler(IRepository blogArchiveRepository, IRepository commonRepository) { + OnPublished((context, bp) => RecalculateBlogArchive(blogArchiveRepository, commonRepository, bp)); + OnRemoved((context, bp) => RecalculateBlogArchive(blogArchiveRepository, commonRepository, bp)); + } + + private static void RecalculateBlogArchive(IRepository blogArchiveRepository, IRepository commonRepository, BlogPost blogPost) { + blogArchiveRepository.Flush(); + + //INFO: (erikpo) Remove all current blog archive records + var blogArchiveRecords = + from bar in blogArchiveRepository.Table + where bar.Blog == blogPost.Blog.Record + select bar; + blogArchiveRecords.ToList().ForEach(blogArchiveRepository.Delete); + + //INFO: (erikpo) Get all blog posts for the current blog + var postsQuery = + from bpr in commonRepository.Table + where bpr.ContentItemRecord.ContentType.Name == "blogpost" && bpr.Container.Id == blogPost.Blog.Record.Id + orderby bpr.PublishedUtc + select bpr; + + //INFO: (erikpo) Create a dictionary of all the year/month combinations and their count of posts that are published in this blog + var inMemoryBlogArchives = new Dictionary(postsQuery.Count()); + foreach (var post in postsQuery) { + if (!post.PublishedUtc.HasValue) + continue; + + var key = new DateTime(post.PublishedUtc.Value.Year, post.PublishedUtc.Value.Month, 1); + + if (inMemoryBlogArchives.ContainsKey(key)) + inMemoryBlogArchives[key]++; + else + inMemoryBlogArchives[key] = 1; + } + + //INFO: (erikpo) Create the new blog archive records based on the in memory values + foreach (KeyValuePair item in inMemoryBlogArchives) { + blogArchiveRepository.Create(new BlogArchiveRecord {Blog = blogPost.Blog.Record, Year = item.Key.Year, Month = item.Key.Month, PostCount = item.Value}); + } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveRecord.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveRecord.cs new file mode 100644 index 000000000..a876037ae --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Models/BlogArchiveRecord.cs @@ -0,0 +1,9 @@ +namespace Orchard.Blogs.Models { + public class BlogArchiveRecord { + public virtual int Id { get; set; } + public virtual BlogRecord Blog { get; set; } + public virtual int Year { get; set; } + public virtual int Month { get; set; } + public virtual int PostCount { 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 ac354d74b..60ef63c6e 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Orchard.Blogs.csproj +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Orchard.Blogs.csproj @@ -84,6 +84,8 @@ + + diff --git a/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogPostService.cs b/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogPostService.cs index 117b56bda..714aa88b9 100644 --- a/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogPostService.cs +++ b/src/Orchard.Web/Packages/Orchard.Blogs/Services/BlogPostService.cs @@ -5,13 +5,16 @@ using Orchard.Blogs.Controllers; using Orchard.Blogs.Models; using Orchard.Core.Common.Records; using Orchard.ContentManagement; +using Orchard.Data; namespace Orchard.Blogs.Services { public class BlogPostService : IBlogPostService { private readonly IContentManager _contentManager; + private readonly IRepository _blogArchiveRepository; - public BlogPostService(IContentManager contentManager) { + public BlogPostService(IContentManager contentManager, IRepository blogArchiveRepository) { _contentManager = contentManager; + _blogArchiveRepository = blogArchiveRepository; } public BlogPost Get(Blog blog, string slug) { @@ -65,29 +68,17 @@ namespace Orchard.Blogs.Services { } public IEnumerable> GetArchives(Blog blog) { - return new List> { - new KeyValuePair(new ArchiveData("2010/1"), 5), - new KeyValuePair(new ArchiveData("2009/12"), 23), - new KeyValuePair(new ArchiveData("2009/11"), 4), - new KeyValuePair(new ArchiveData("2009/9"), 1), - new KeyValuePair(new ArchiveData("2009/8"), 1), - new KeyValuePair(new ArchiveData("2009/7"), 1), - new KeyValuePair(new ArchiveData("2009/6"), 1), - new KeyValuePair(new ArchiveData("2009/5"), 1), - new KeyValuePair(new ArchiveData("2009/4"), 1), - new KeyValuePair(new ArchiveData("2009/3"), 1), - new KeyValuePair(new ArchiveData("2009/2"), 1), - new KeyValuePair(new ArchiveData("2009/1"), 1), - new KeyValuePair(new ArchiveData("2008/12"), 1), - new KeyValuePair(new ArchiveData("2008/11"), 1), - new KeyValuePair(new ArchiveData("2008/10"), 1), - new KeyValuePair(new ArchiveData("2008/9"), 1), - new KeyValuePair(new ArchiveData("2008/7"), 1), - new KeyValuePair(new ArchiveData("2008/6"), 1), - new KeyValuePair(new ArchiveData("2008/5"), 1), - new KeyValuePair(new ArchiveData("2008/4"), 1), - new KeyValuePair(new ArchiveData("2008/3"), 1) - }; + var query = + from bar in _blogArchiveRepository.Table + where bar.Blog == blog.Record + orderby bar.Year descending, bar.Month descending + select bar; + + return + query.ToList().Select( + bar => + new KeyValuePair(new ArchiveData(string.Format("{0}/{1}", bar.Year, bar.Month)), + bar.PostCount)); } public void Delete(BlogPost blogPost) {