Correct archive when date changed (#8306)

This commit is contained in:
ElenaRepository
2020-01-16 13:12:11 -08:00
committed by Sébastien Ros
parent bb8348626b
commit d94d50f67d
2 changed files with 62 additions and 35 deletions
@@ -15,10 +15,10 @@ namespace Orchard.Blogs.Handlers {
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IContentManager _contentManager;
// contains the creation time of a blog part before it has been changed
private readonly Dictionary<int, DateTime> _previousCreatedUtc = new Dictionary<int,DateTime>();
private readonly Dictionary<int, DateTime> _previousCreatedUtc = new Dictionary<int, DateTime>();
public BlogPartArchiveHandler(
IRepository<BlogPartArchiveRecord> blogArchiveRepository,
IRepository<BlogPartArchiveRecord> blogArchiveRepository,
IBlogPostService blogPostService,
IWorkContextAccessor workContextAccessor,
IContentManager contentManager) {
@@ -26,11 +26,11 @@ namespace Orchard.Blogs.Handlers {
_workContextAccessor = workContextAccessor;
_contentManager = contentManager;
OnUpdating<CommonPart>((context, cp) => { if(context.ContentItem.Has<BlogPostPart>()) SavePreviousCreatedDate(context.Id);});
OnUpdating<CommonPart>((context, cp) => { if (context.ContentItem.Has<BlogPostPart>()) SavePreviousCreatedDate(context.Id); });
OnRemoving<BlogPostPart>((context, bp) => SavePreviousCreatedDate(context.Id));
OnUnpublishing<BlogPostPart>((context, bp) => SavePreviousCreatedDate(context.Id));
OnPublished<BlogPostPart>((context, bp) => IncreaseBlogArchive(bp));
OnPublished<BlogPostPart>((context, bp) => ManageBlogArchiveSync(bp));
OnUnpublished<BlogPostPart>((context, bp) => ReduceBlogArchive(bp));
OnRemoved<BlogPostPart>((context, bp) => ReduceBlogArchive(bp));
}
@@ -71,58 +71,56 @@ namespace Orchard.Blogs.Handlers {
&& x.Month == datetime.Month
&& x.Year == datetime.Year);
if(previousArchiveRecord == null)
if (previousArchiveRecord == null)
return;
if (previousArchiveRecord.PostCount > 1)
previousArchiveRecord.PostCount--;
else
_blogArchiveRepository.Delete(previousArchiveRecord);
blogPostPart.ArchiveSync = null;
}
private void ReduceBlogArchiveByDate(int BlogPartId, DateTime? dateBlogPost) {
_blogArchiveRepository.Flush();
if (BlogPartId!= 0 && dateBlogPost.HasValue) {
var previousArchiveRecord = _blogArchiveRepository.Table
.FirstOrDefault(x => x.BlogPart.Id == BlogPartId
&& x.Month == dateBlogPost.Value.Month
&& x.Year == dateBlogPost.Value.Year);
if (previousArchiveRecord == null)
return;
if (previousArchiveRecord.PostCount > 1)
previousArchiveRecord.PostCount--;
else
_blogArchiveRepository.Delete(previousArchiveRecord);
}
else {
return;
}
}
private void IncreaseBlogArchive(BlogPostPart blogPostPart) {
_blogArchiveRepository.Flush();
var commonPart = blogPostPart.As<ICommonPart>();
if(commonPart == null || !commonPart.CreatedUtc.HasValue)
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
@@ -136,7 +134,31 @@ namespace Orchard.Blogs.Handlers {
_blogArchiveRepository.Create(newArchiveRecord);
}
newArchiveRecord.PostCount++;
newArchiveRecord.PostCount++;
}
private void ManageBlogArchiveSync(BlogPostPart blogPostPart) {
var commonPart = blogPostPart.As<ICommonPart>();
if (commonPart == null || !commonPart.CreatedUtc.HasValue)
return;
var timeZone = _workContextAccessor.GetContext().CurrentTimeZone;
var creationDate = TimeZoneInfo.ConvertTimeFromUtc(commonPart.CreatedUtc.Value, timeZone);
if (blogPostPart.ArchiveSync == null) {
IncreaseBlogArchive(blogPostPart);
blogPostPart.ArchiveSync = creationDate;
}
else {
if (creationDate == blogPostPart.ArchiveSync) {
return;
}
else {
ReduceBlogArchiveByDate(blogPostPart.BlogPart.Id, blogPostPart.ArchiveSync);
IncreaseBlogArchive(blogPostPart);
blogPostPart.ArchiveSync = creationDate;
}
}
}
}
}
@@ -49,5 +49,10 @@ namespace Orchard.Blogs.Models {
public DateTime? PublishedUtc {
get { return this.As<ICommonPart>().PublishedUtc; }
}
public DateTime? ArchiveSync {
get { return this.Retrieve(x => x.ArchiveSync); }
set { this.Store(x => x.ArchiveSync, value); }
}
}
}