public void RebuildArchive(CasePart casePart) { var first = _contentManager.Query <CasePostPart>().Where <CommonPartRecord>(bp => bp.Container.Id == casePart.Id).OrderBy <CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault(); if (first == null) { return; } var last = _contentManager.Query <CasePostPart>().Where <CommonPartRecord>(bp => bp.Container.Id == casePart.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 _caseArchiveRepository.Table.Where(x => x.CasePart.Id == casePart.Id)) { _caseArchiveRepository.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 casePostDates = new List <DateTime>(); var casePosts = _contentManager.Query <CasePostPart>().Where <CommonPartRecord>(bp => bp.Container.Id == casePart.Id); foreach (var casePost in casePosts.List()) { if (casePost.As <CommonPart>() != null) { if (casePost.As <CommonPart>().CreatedUtc.HasValue) { DateTime timeZoneAdjustedCreatedDate = TimeZoneInfo.ConvertTimeFromUtc(casePost.As <CommonPart>().CreatedUtc.Value, timeZone); casePostDates.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<CasePostPart>().Where<CommonPartRecord>(x => x.CreatedUtc.Value >= from && x.CreatedUtc.Value < to).Count(); var count = casePostDates.Count(bp => bp >= @from && bp < to); var newArchiveRecord = new CasePartArchiveRecord { CasePart = casePart.ContentItem.Record, Year = year, Month = month, PostCount = count }; _caseArchiveRepository.Create(newArchiveRecord); } } }
private void IncreaseCaseArchive(CasePostPart 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.CasePart.Id == blogPostPart.CasePart.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.CasePart.Id == blogPostPart.CasePart.Id && x.Month == newMonth && x.Year == newYear); // if record can't be found create it if (newArchiveRecord == null) { newArchiveRecord = new CasePartArchiveRecord { CasePart = blogPostPart.CasePart.ContentItem.Record, Year = newYear, Month = newMonth, PostCount = 0 }; _blogArchiveRepository.Create(newArchiveRecord); } newArchiveRecord.PostCount++; }