예제 #1
0
        public void RebuildArchive(BlogPart blogPart)
        {
            var first = _contentManager.Query <BlogPostPart>().Where <CommonPartRecord>(bp => bp.Container.Id == blogPart.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.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.Id == blogPart.Id))
            {
                _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.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.ContentItem.Record, Year = year, Month = month, PostCount = count
                    };
                    _blogArchiveRepository.Create(newArchiveRecord);
                }
            }
        }
예제 #2
0
        private void RecalculateBlogArchive(IRepository <BlogPartArchiveRecord> blogArchiveRepository, BlogPostPart blogPostPart)
        {
            blogArchiveRepository.Flush();

            var commonPart = blogPostPart.As <CommonPart>();

            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) ? _previousCreatedUtc[blogPostPart] : DateTime.MinValue;

            previousCreatedUtc = TimeZoneInfo.ConvertTimeFromUtc(previousCreatedUtc, timeZone);

            var previousMonth = previousCreatedUtc.Month;
            var previousYear  = previousCreatedUtc.Year;

            var newCreatedUtc = commonPart.CreatedUtc;

            newCreatedUtc = newCreatedUtc.HasValue ? TimeZoneInfo.ConvertTimeFromUtc(newCreatedUtc.Value, timeZone) : newCreatedUtc;

            var newMonth = newCreatedUtc.HasValue ? newCreatedUtc.Value.Month : 0;
            var newYear  = newCreatedUtc.HasValue ? newCreatedUtc.Value.Year : 0;

            // if archives are the same there is nothing to do
            if (previousMonth == newMonth && previousYear == newYear)
            {
                return;
            }

            // decrement previous archive record
            var previousArchiveRecord = blogArchiveRepository.Table
                                        .Where(x => x.BlogPart == blogPostPart.BlogPart.Record &&
                                               x.Month == previousMonth &&
                                               x.Year == previousYear)
                                        .FirstOrDefault();

            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
                                   .Where(x => x.BlogPart == blogPostPart.BlogPart.Record &&
                                          x.Month == newMonth &&
                                          x.Year == newYear)
                                   .FirstOrDefault();

            // if record can't be found create it
            if (newArchiveRecord == null)
            {
                newArchiveRecord = new BlogPartArchiveRecord {
                    BlogPart = blogPostPart.BlogPart.Record, Year = newYear, Month = newMonth, PostCount = 0
                };
                blogArchiveRepository.Create(newArchiveRecord);
            }

            newArchiveRecord.PostCount++;
        }