perf: optimize the update logic of post editTime field (#2195)

* fix(): 🚸 文章更新时间 逻辑优化;

只有当修改文章标题 或文章内容时 去更新posts表的edit_time字段

* fix():  文章更新时间 逻辑优化;

只有当修改文章标题 或文章内容时 去更新posts表的edit_time字段

* check code-style

* Update the editTime when saving the draft

* remove useless code
pull/2209/head
若如初见 2022-07-04 11:14:24 +08:00 committed by GitHub
parent 567c8a2971
commit 2a5277a33f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 6 deletions

View File

@ -36,6 +36,7 @@ import run.halo.app.model.vo.PostDetailVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;
import run.halo.app.service.assembler.PostAssembler;
import run.halo.app.utils.DateUtils;
import run.halo.app.utils.HaloUtils;
/**
@ -164,8 +165,14 @@ public class PostController {
public BasePostDetailDTO updateDraftBy(
@PathVariable("postId") Integer postId,
@RequestBody PostContentParam contentParam) {
Post postToUse = postService.getById(postId);
Post postToUse = postService.getWithLatestContentById(postId);
String formattedContent = contentParam.decideContentBy(postToUse.getEditorType());
// Update the editTime when the content of the posts changes
if (!postToUse.getContent().getOriginalContent()
.equals(contentParam.getOriginalContent())) {
postToUse.setEditTime(DateUtils.now());
postService.update(postToUse);
}
// Update draft content
Post post = postService.updateDraftContent(formattedContent,
contentParam.getOriginalContent(), postId);

View File

@ -10,7 +10,7 @@ import run.halo.app.model.entity.BasePost;
import run.halo.app.model.entity.Content;
import run.halo.app.model.enums.PostEditorType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.impl.BasePostServiceImpl;
import run.halo.app.utils.DateUtils;
import run.halo.app.utils.MarkdownUtils;
/**
@ -73,6 +73,13 @@ public abstract class BasePostParam {
} else {
postContent.setContent(content);
}
// update the editTime only when changing the post title or post content
if (!Objects.equals(title, post.getTitle())
|| !Objects.equals(originalContent, post.getContent().getOriginalContent())) {
post.setEditTime(DateUtils.now());
}
post.setContent(Content.PatchedContent.of(postContent));
}
}

View File

@ -314,8 +314,6 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
postContent.getContent(), postContent.getOriginalContent());
} else {
// The sheet will be updated
// Set edit time
post.setEditTime(DateUtils.now());
contentService.createOrUpdateDraftBy(post.getId(),
postContent.getContent(), postContent.getOriginalContent());
// Update it

View File

@ -194,8 +194,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
@Transactional(rollbackFor = Exception.class)
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds,
Set<PostMeta> metas, boolean autoSave) {
// Set edit time
postToUpdate.setEditTime(DateUtils.now());
PostDetailVO updatedPost = createOrUpdate(postToUpdate, tagIds, categoryIds, metas);
if (!autoSave) {
// Log the creation