Fixing BuildArchive command

--HG--
branch : 1.x
This commit is contained in:
Andrew Connell
2013-03-20 10:54:21 -07:00
parent 6cf16fdc2c
commit b274ede4d8
2 changed files with 37 additions and 15 deletions

View File

@@ -164,7 +164,7 @@ namespace Orchard.Blogs.Commands {
[CommandName("blog build archive")]
[CommandHelp("blog build archive /BlogId:<id> \r\n\t" + "Rebuild the archive information for the blog specified by <id>")]
[OrchardSwitches("BlogId")]
public void BuilddArchive() {
public void BuildArchive() {
var blog = _blogService.Get(BlogId, VersionOptions.Latest);

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Blogs.Models;
using Orchard.ContentManagement;
@@ -22,24 +23,24 @@ namespace Orchard.Blogs.Services {
public void RebuildArchive(BlogPart blogPart) {
var first = _contentManager.Query<BlogPostPart>().OrderBy<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
var first = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Record.Id).OrderBy<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
if (first == null) {
return;
}
var last = _contentManager.Query<BlogPostPart>().OrderByDescending<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
var last = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Record.Id).OrderByDescending<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
DateTime? start = DateTime.MaxValue;
if (first.As<CommonPart>() != null) {
start = first.As<CommonPart>().CreatedUtc;
}
DateTime? end = DateTime.MinValue;
if (last.As<CommonPart>() != null) {
end = first.As<CommonPart>().CreatedUtc;
end = last.As<CommonPart>().CreatedUtc;
}
// delete previous archive records
foreach (var record in _blogArchiveRepository.Table.Where(x => x.BlogPart == blogPart.Record)) {
_blogArchiveRepository.Delete(record);
@@ -48,19 +49,40 @@ namespace Orchard.Blogs.Services {
if (!start.HasValue || !end.HasValue) {
return;
}
// get the time zone for the current request
var timeZone = _workContextAccessor.GetContext().CurrentTimeZone;
for (int year = start.Value.Year - 1; year <= end.Value.Year + 1; year++) {
for (int month = 1; month <= 12; month++) {
int yearCopy = year;
int monthCopy = month;
var from = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(yearCopy, monthCopy, 1), timeZone);
var to = from.AddMonths(1);
var count = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(x => x.CreatedUtc.Value >= from && x.CreatedUtc.Value < to).Count();
// build a collection of all the post dates
var blogPostDates = new List<DateTime>();
var blogPosts = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Record.Id);
foreach (var blogPost in blogPosts.List()) {
if (blogPost.As<CommonPart>() != null)
if (blogPost.As<CommonPart>().CreatedUtc.HasValue) {
DateTime timeZoneAdjustedCreatedDate = TimeZoneInfo.ConvertTimeFromUtc(blogPost.As<CommonPart>().CreatedUtc.Value, timeZone);
blogPostDates.Add(timeZoneAdjustedCreatedDate);
}
}
var newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPart.Record, Year = from.Year, Month = from.Month, PostCount = count };
for (int year = start.Value.Year; year <= end.Value.Year; year++) {
for (int month = 1; month <= 12; month++) {
var fromDateUtc = new DateTime(year, month, 1);
var from = TimeZoneInfo.ConvertTimeFromUtc(fromDateUtc, timeZone);
var to = TimeZoneInfo.ConvertTimeFromUtc(fromDateUtc.AddMonths(1), timeZone);
// skip the first months of the first year until a month has posts
// for instance, if started posting in May 2000, don't write counts for Jan 200 > April 2000... start May 2000
if (from < TimeZoneInfo.ConvertTimeFromUtc(new DateTime(start.Value.Year, start.Value.Month, 1), timeZone))
continue;
// skip the last months of the last year if no posts
// for instance, no need to have archives for months in the future
if (to > end.Value.AddMonths(1))
continue;
//var count = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(x => x.CreatedUtc.Value >= from && x.CreatedUtc.Value < to).Count();
var count = blogPostDates.Count(bp => bp >= @from && bp < to);
var newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPart.Record, Year = year, Month = month, PostCount = count };
_blogArchiveRepository.Create(newArchiveRecord);
}
}