public void RebuildArchive(BlogPart blogPart) { 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>().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 = last.As<CommonPart>().CreatedUtc; } // delete previous archive records foreach (var record in _blogArchiveRepository.Table.Where(x => x.BlogPart == blogPart.Record)) { _blogArchiveRepository.Delete(record); } if (!start.HasValue || !end.HasValue) { return; } // get the time zone for the current request var timeZone = _workContextAccessor.GetContext().CurrentTimeZone; // 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); } } 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); } } }
private void IncreaseBlogArchive(BlogPostPart blogPostPart) { _blogArchiveRepository.Flush(); var commonPart = blogPostPart.As<ICommonPart>(); if(commonPart == null || !commonPart.CreatedUtc.HasValue) return; // get the time zone for the current request var timeZone = _workContextAccessor.GetContext().CurrentTimeZone; var previousCreatedUtc = _previousCreatedUtc.ContainsKey(blogPostPart.Id) ? _previousCreatedUtc[blogPostPart.Id] : DateTime.MinValue; previousCreatedUtc = TimeZoneInfo.ConvertTimeFromUtc(previousCreatedUtc, timeZone); var previousMonth = previousCreatedUtc.Month; var previousYear = previousCreatedUtc.Year; var newCreatedUtc = commonPart.CreatedUtc; newCreatedUtc = TimeZoneInfo.ConvertTimeFromUtc(newCreatedUtc.Value, timeZone); var newMonth = newCreatedUtc.Value.Month; var newYear = newCreatedUtc.Value.Year; // if archives are the same there is nothing to do if (previousMonth == newMonth && previousYear == newYear) { return; } // decrement previous archive record var previousArchiveRecord = _blogArchiveRepository .Table .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id && x.Month == previousMonth && x.Year == previousYear); if (previousArchiveRecord != null && previousArchiveRecord.PostCount > 0) { previousArchiveRecord.PostCount--; } // if previous count is now zero, delete the record if (previousArchiveRecord != null && previousArchiveRecord.PostCount == 0) { _blogArchiveRepository.Delete(previousArchiveRecord); } // increment new archive record var newArchiveRecord = _blogArchiveRepository .Table .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id && x.Month == newMonth && x.Year == newYear); // if record can't be found create it if (newArchiveRecord == null) { newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPostPart.BlogPart.ContentItem.Record, Year = newYear, Month = newMonth, PostCount = 0 }; _blogArchiveRepository.Create(newArchiveRecord); } newArchiveRecord.PostCount++; }