Forráskód Böngészése

重构历史版本对比逻辑,优化数据映射

重构 `CompareVersion` 方法,移除对 `PostHistoryVersion` 的直接依赖,改为处理 `Post` 对象。
在 `PostMapper` 中新增 `ToPost` 和 `ProjectDto` 方法,优化数据转换逻辑。
更新 `CompareVersion.cshtml`,调整数据模型类型和页面显示逻辑以适配后端更改。
移除冗余引用,格式化代码,提升代码可读性。
懒得勤快 5 napja
szülő
commit
c8758c0f32

+ 2 - 0
src/Masuit.MyBlogs.Core/Configs/Mappers/PostMapper.cs

@@ -34,6 +34,8 @@ public static partial class PostMapper
     [MapProperty("Category", nameof(PostDto.CategoryName))]
     public static partial PostDto ToPostDto(this PostHistoryVersion cmd);
 
+    public static partial Post ToPost(this PostHistoryVersion history);
+
     public static partial IQueryable<PostDto> ProjectDto(this IQueryable<Post> q);
 
     [MapProperty("PostHistoryVersion", nameof(PostDataModel.ModifyCount))]

+ 4 - 4
src/Masuit.MyBlogs.Core/Controllers/PostController.cs

@@ -192,10 +192,10 @@ public sealed class PostController : BaseController
     public async Task<ActionResult> CompareVersion(int id, int v1, int v2)
     {
         var post = await PostService.GetAsync(p => p.Id == id && (p.Status == Status.Published || CurrentUser.IsAdmin));
-        var main = post.ToHistoryVersion() ?? throw new NotFoundException("文章未找到");
+        var main = post ?? throw new NotFoundException("文章未找到");
         CheckPermission(post);
-        var right = v1 <= 0 ? main : await PostHistoryVersionService.GetAsync(v => v.Id == v1) ?? throw new NotFoundException("文章未找到");
-        var left = v2 <= 0 ? main : await PostHistoryVersionService.GetAsync(v => v.Id == v2) ?? throw new NotFoundException("文章未找到");
+        var right = v1 <= 0 ? main : (await PostHistoryVersionService.GetAsync(v => v.Id == v1) ?? throw new NotFoundException("文章未找到")).ToPost();
+        var left = v2 <= 0 ? main : (await PostHistoryVersionService.GetAsync(v => v.Id == v2) ?? throw new NotFoundException("文章未找到")).ToPost();
         main.Id = id;
         var (html1, html2) = left.Content.HtmlDiff(right.Content);
         left.Content = await ReplaceVariables(html1).Next(s => CurrentUser.IsAdmin || Request.IsRobot() ? Task.FromResult(s) : s.InjectFingerprint(ClientIP.ToString()));
@@ -204,7 +204,7 @@ public sealed class PostController : BaseController
         right.ModifyDate = right.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
         ViewBag.Ads = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage, Request.Location(), main.CategoryId, main.Label);
         ViewBag.DisableCopy = post.DisableCopy;
-        return View(new[] { main, left, right });
+        return View(new[] { main, left, right }.OrderByDescending(v => v.ModifyDate).ToArray());
     }
 
     /// <summary>

+ 39 - 55
src/Masuit.MyBlogs.Core/Views/Post/CompareVersion.cshtml

@@ -1,35 +1,23 @@
-@using System.Text.Encodings.Web
-@using System.Text.RegularExpressions
-@using Masuit.MyBlogs.Core.Common
+@using Masuit.MyBlogs.Core.Common
 @using Masuit.MyBlogs.Core.Models.DTO
-@using Masuit.MyBlogs.Core.Models.Entity
 @using Masuit.MyBlogs.Core.Models.ViewModel
 @using Masuit.Tools.Core.Net
-@model Masuit.MyBlogs.Core.Models.Entity.PostHistoryVersion[]
+@model Masuit.MyBlogs.Core.Models.Entity.Post[]
 @{
   ViewBag.Title = Model[0].Title + "版本对比";
   Layout = "~/Views/Shared/_Layout.cshtml";
-  string[] colors = {
-    "success",
-    "info",
-    "primary",
-    "warning",
-    "danger",
-    "default",
-    "primary"
-  };
   IList<AdvertisementDto> ads = ViewBag.Ads;
 }
 <style>
-	ins {
-		background-color: #cfc;
-		text-decoration: none;
-	}
+    ins {
+        background-color: #cfc;
+        text-decoration: none;
+    }
 
-	del {
-		color: #999;
-		background-color: #FEC8C8;
-	}
+    del {
+        color: #999;
+        background-color: #FEC8C8;
+    }
 </style>
 <link href="~/UEditorPlus/third-party/SyntaxHighlighter/shCoreDefault.css" rel="stylesheet"/>
 <div class="container-fluid">
@@ -69,18 +57,18 @@
       <!-- 头部信息 -->
       <section class="article-header">
         <div class="article-title-row">
-          <h2 class="article-title">@Model[0].Title</h2>
+          <h2 class="article-title">@Model[1].Title</h2>
         </div>
         <!-- 文章标签展示区,可放在文章信息区或内容区上方,支持多个标签 -->
         <div class="article-meta-row">
           <div>
             <span class="modifier">
-              <a asp-action="Author" asp-controller="Home" asp-route-author="@Model[0].Modifier">@Model[0].Modifier</a>
+              <a asp-action="Author" asp-controller="Home" asp-route-author="@Model[1].Modifier">@Model[1].Modifier</a>
             </span>
             <span>
               修改于
-              <a asp-action="Archieve" asp-controller="Home" asp-route-dd="@Model[0].ModifyDate.Day" asp-route-mm="@Model[0].ModifyDate.Month" asp-route-mode="ModifyDate" asp-route-yyyy="@Model[0].ModifyDate.Year">
-                <time class="text-success">@Model[0].ModifyDate.ToTimeZoneF(Context.Session.Get<string>(SessionKey.TimeZone))</time>
+              <a asp-action="Archieve" asp-controller="Home" asp-route-dd="@Model[1].ModifyDate.Day" asp-route-mm="@Model[1].ModifyDate.Month" asp-route-mode="ModifyDate" asp-route-yyyy="@Model[1].ModifyDate.Year">
+                <time class="text-success">@Model[1].ModifyDate.ToTimeZoneF(Context.Session.Get<string>(SessionKey.TimeZone))</time>
               </a>
             </span>
           </div>
@@ -92,16 +80,16 @@
               <i class="icon-map-pin"></i>
               <span class="article-tag">
                 @{
-                  await Html.RenderPartialAsync("CategoryPath", Model[0].Category);
+                  await Html.RenderPartialAsync("CategoryPath", Model[1].Category);
                 }
               </span>
             </span>
           </div>
-          @if (Model[0].Seminar.Any()) {
+          @if (Model[1].Seminar.Any()) {
             <div class="article-tags-row">
               <span style="font-weight:600;color:#1566b6;font-size:1.06rem;">原所属专题:</span>
               @{
-                var seminars = Model[0].Seminar;
+                var seminars = Model[1].Seminar;
                 foreach (var s in seminars) {
                   <span class="article-tag">
                     <a asp-action="Index" asp-controller="Seminar" asp-route-id="@s.Id">@s.Title</a>
@@ -111,10 +99,10 @@
             </div>
           }
           @{
-            if (!string.IsNullOrEmpty(Model[0].Label)) {
+            if (!string.IsNullOrEmpty(Model[1].Label)) {
               <div class="article-tags-row">
                 <span style="font-weight:600;color:#1566b6;font-size:1.06rem;">标签:</span>
-                @foreach (string s in Model[0].Label.Split([',', ','], StringSplitOptions.RemoveEmptyEntries)) {
+                @foreach (string s in Model[1].Label.Split([',', ','], StringSplitOptions.RemoveEmptyEntries)) {
                   <span class="article-tag">
                     <a asp-action="Tag" asp-controller="Home" asp-route-tag="@s">
                       @s
@@ -129,13 +117,11 @@
       <!-- 内容区 -->
       <section class="article-content-card">
         <div class="article-content">
-          <div class="old-card">
-            您目前正在查看该文章的历史版本,文章的历史版本可能无法为您提供最准确及时的资讯,不具备任何参考价值,
-            <a asp-action="Details" asp-controller="Post" asp-route-id="@Model[0].PostId">点击前往</a>该文章的最新版本。
-          </div>
-          @Html.Raw(Model[0].Content)
+          @Html.Raw(Model[1].Content)
         </div>
-        <div class="encrypted-content"> @Html.Raw(Model[0].ProtectContent)</div>
+        @{
+            await Html.RenderPartialAsync("/Views/Post/ProtectContent.cshtml", Model[1]);
+        }
         @if (ads.Any()) {
           await Html.RenderPartialAsync("_ArticleListAdvertisement", ads[0]);
         }
@@ -145,18 +131,18 @@
       <!-- 头部信息 -->
       <section class="article-header">
         <div class="article-title-row">
-          <h2 class="article-title">@Model[1].Title</h2>
+          <h2 class="article-title">@Model[2].Title</h2>
         </div>
         <!-- 文章标签展示区,可放在文章信息区或内容区上方,支持多个标签 -->
         <div class="article-meta-row">
           <div>
             <span class="modifier">
-              <a asp-action="Author" asp-controller="Home" asp-route-author="@Model[1].Modifier">@Model[1].Modifier</a>
+              <a asp-action="Author" asp-controller="Home" asp-route-author="@Model[2].Modifier">@Model[2].Modifier</a>
             </span>
             <span>
               修改于
-              <a asp-action="Archieve" asp-controller="Home" asp-route-dd="@Model[1].ModifyDate.Day" asp-route-mm="@Model[1].ModifyDate.Month" asp-route-mode="ModifyDate" asp-route-yyyy="@Model[1].ModifyDate.Year">
-                <time class="text-success">@Model[1].ModifyDate.ToTimeZoneF(Context.Session.Get<string>(SessionKey.TimeZone))</time>
+              <a asp-action="Archieve" asp-controller="Home" asp-route-dd="@Model[2].ModifyDate.Day" asp-route-mm="@Model[2].ModifyDate.Month" asp-route-mode="ModifyDate" asp-route-yyyy="@Model[2].ModifyDate.Year">
+                <time class="text-success">@Model[2].ModifyDate.ToTimeZoneF(Context.Session.Get<string>(SessionKey.TimeZone))</time>
               </a>
             </span>
           </div>
@@ -168,16 +154,16 @@
               <i class="icon-map-pin"></i>
               <span class="article-tag">
                 @{
-                  await Html.RenderPartialAsync("CategoryPath", Model[1].Category);
+                  await Html.RenderPartialAsync("CategoryPath", Model[2].Category);
                 }
               </span>
             </span>
           </div>
-          @if (Model[1].Seminar.Any()) {
+          @if (Model[2].Seminar.Any()) {
             <div class="article-tags-row">
               <span style="font-weight:600;color:#1566b6;font-size:1.06rem;">原所属专题:</span>
               @{
-                var seminars = Model[1].Seminar;
+                var seminars = Model[2].Seminar;
                 foreach (var s in seminars) {
                   <span class="article-tag">
                     <a asp-action="Index" asp-controller="Seminar" asp-route-id="@s.Id">@s.Title</a>
@@ -187,10 +173,10 @@
             </div>
           }
           @{
-            if (!string.IsNullOrEmpty(Model[1].Label)) {
+            if (!string.IsNullOrEmpty(Model[2].Label)) {
               <div class="article-tags-row">
                 <span style="font-weight:600;color:#1566b6;font-size:1.06rem;">标签:</span>
-                @foreach (string s in Model[1].Label.Split([',', ','], StringSplitOptions.RemoveEmptyEntries)) {
+                @foreach (string s in Model[2].Label.Split([',', ','], StringSplitOptions.RemoveEmptyEntries)) {
                   <span class="article-tag">
                     <a asp-action="Tag" asp-controller="Home" asp-route-tag="@s">
                       @s
@@ -205,13 +191,11 @@
       <!-- 内容区 -->
       <section class="article-content-card">
         <div class="article-content">
-          <div class="old-card">
-            您目前正在查看该文章的历史版本,文章的历史版本可能无法为您提供最准确及时的资讯,不具备任何参考价值,
-            <a asp-action="Details" asp-controller="Post" asp-route-id="@Model[1].PostId">点击前往</a>该文章的最新版本。
-          </div>
-          @Html.Raw(Model[1].Content)
+          @Html.Raw(Model[2].Content)
         </div>
-        <div class="encrypted-content"> @Html.Raw(Model[1].ProtectContent)</div>
+        @{
+            await Html.RenderPartialAsync("/Views/Post/ProtectContent.cshtml", Model[2]);
+        }
         @if (ads.Count > 1) {
           await Html.RenderPartialAsync("_ArticleListAdvertisement", ads[1]);
         }
@@ -224,10 +208,10 @@
 window.onload=function() {
   SyntaxHighlighter.all();
   SyntaxHighlighter.defaults['toolbar'] = false;
-	@if (ViewBag.DisableCopy) {
+    @if (ViewBag.DisableCopy) {
     <text>
-	GlobalCopyrightProtect();
-		</text>
+    GlobalCopyrightProtect();
+        </text>
   }
 }
 </script>