示例#1
0
        public PageDifferenceView GetPageDifference(int id)
        {
            var version = GetAllPagesQuery().FirstOrDefault(x => x.Id == id);

            if (version == null)
            {
                throw new PageNotFoundException("Failed to find the page with that version ID.");
            }

            if (IsGuestRequest && (version.ApprovalStatus != ApprovalStatus.Approved || !version.IsPublished))
            {
                throw new PageNotFoundException("Failed to find the page with that version ID.");
            }

            var versions = IsGuestRequest
                                ? version.Page.Versions.Where(x => x.IsPublished && x.ApprovalStatus == ApprovalStatus.Approved)
                                : version.Page.Versions;

            var previous = versions
                           .Where(x => x.Id < version.Id)
                           .OrderByDescending(x => x.Id)
                           .FirstOrDefault();

            var response = new PageDifferenceView
            {
                Id           = version.PageId,
                LastModified = DateTime.UtcNow.Subtract(version.ModifiedOn).ToTimeAgo(),
                CreatedBy    = version.CreatedBy.DisplayName
            };

            if (previous == null)
            {
                response.Html         = Converter.ToHtml(version.Text);
                response.Title        = version.Title;
                response.TitleForLink = PageView.ConvertTitleForLink(version.Title);
                response.Tags         = string.Join(", ", PageVersion.SplitTags(version.Tags));
            }
            else
            {
                response.Html         = HtmlDiff.Process(Converter.ToHtml(previous.Text), Converter.ToHtml(version.Text));
                response.Title        = HtmlDiff.Process(Converter.ToHtml(previous.Title), Converter.ToHtml(version.Title));
                response.TitleForLink = PageView.ConvertTitleForLink(previous.Title);
                response.Tags         = HtmlDiff.Process(Converter.ToHtml(string.Join(", ", PageVersion.SplitTags(previous.Tags))), Converter.ToHtml(string.Join(", ", PageVersion.SplitTags(version.Tags))));
            }

            return(response);
        }