mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
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
This commit is contained in:
parent
3922abc31e
commit
09c88f124c
@ -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<BlogArchiveRecord> blogArchiveRepository, IRepository<CommonRecord> commonRepository) {
|
||||
OnPublished<BlogPost>((context, bp) => RecalculateBlogArchive(blogArchiveRepository, commonRepository, bp));
|
||||
OnRemoved<BlogPost>((context, bp) => RecalculateBlogArchive(blogArchiveRepository, commonRepository, bp));
|
||||
}
|
||||
|
||||
private static void RecalculateBlogArchive(IRepository<BlogArchiveRecord> blogArchiveRepository, IRepository<CommonRecord> 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<DateTime, int>(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<DateTime, int> item in inMemoryBlogArchives) {
|
||||
blogArchiveRepository.Create(new BlogArchiveRecord {Blog = blogPost.Blog.Record, Year = item.Key.Year, Month = item.Key.Month, PostCount = item.Value});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
@ -84,6 +84,8 @@
|
||||
<Compile Include="Extensions\UrlHelperExtensions.cs" />
|
||||
<Compile Include="Filters\ArchivesFilter.cs" />
|
||||
<Compile Include="Models\ArchiveData.cs" />
|
||||
<Compile Include="Models\BlogArchiveHandler.cs" />
|
||||
<Compile Include="Models\BlogArchiveRecord.cs" />
|
||||
<Compile Include="Permissions.cs" />
|
||||
<Compile Include="Routing\IsArchiveConstraint.cs" />
|
||||
<Compile Include="Routing\IsBlogConstraint.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<BlogArchiveRecord> _blogArchiveRepository;
|
||||
|
||||
public BlogPostService(IContentManager contentManager) {
|
||||
public BlogPostService(IContentManager contentManager, IRepository<BlogArchiveRecord> blogArchiveRepository) {
|
||||
_contentManager = contentManager;
|
||||
_blogArchiveRepository = blogArchiveRepository;
|
||||
}
|
||||
|
||||
public BlogPost Get(Blog blog, string slug) {
|
||||
@ -65,29 +68,17 @@ namespace Orchard.Blogs.Services {
|
||||
}
|
||||
|
||||
public IEnumerable<KeyValuePair<ArchiveData, int>> GetArchives(Blog blog) {
|
||||
return new List<KeyValuePair<ArchiveData, int>> {
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2010/1"), 5),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/12"), 23),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/11"), 4),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/9"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/8"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/7"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/6"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/5"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/4"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/3"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/2"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/1"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/12"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/11"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/10"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/9"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/7"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/6"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/5"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/4"), 1),
|
||||
new KeyValuePair<ArchiveData, int>(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<ArchiveData, int>(new ArchiveData(string.Format("{0}/{1}", bar.Year, bar.Month)),
|
||||
bar.PostCount));
|
||||
}
|
||||
|
||||
public void Delete(BlogPost blogPost) {
|
||||
|
Loading…
Reference in New Issue
Block a user