From bdb1eeb80eb73b398c7785dbbe6ec15aaea20372 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Thu, 7 Apr 2022 15:31:33 +0800 Subject: [PATCH] fix: the content cannot be saved problem when the patch status of V1 version of post content is recycle status (#1814) * fix: when the patch status of V1 version of post content is recycle status, the content cannot be saved * fix: code style --- .../impl/ContentPatchLogServiceImpl.java | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/main/java/run/halo/app/service/impl/ContentPatchLogServiceImpl.java b/src/main/java/run/halo/app/service/impl/ContentPatchLogServiceImpl.java index f744c44fd..fbbe49fd7 100644 --- a/src/main/java/run/halo/app/service/impl/ContentPatchLogServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ContentPatchLogServiceImpl.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import org.springframework.data.domain.Example; +import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; @@ -73,8 +74,7 @@ public class ContentPatchLogServiceImpl extends AbstractCrudService example = Example.of(contentPatchLog); - return contentPatchLogRepository.exists(example); + boolean exists = contentPatchLogRepository.exists(example); + if (exists) { + return true; + } + contentPatchLog.setStatus(PostStatus.RECYCLE); + return contentPatchLogRepository.exists(Example.of(contentPatchLog)); } private ContentPatchLog updateDraftBy(Integer postId, String formatContent, String originalContent) { - ContentPatchLog draftPatchLog = - contentPatchLogRepository.findFirstByPostIdAndStatusOrderByVersionDesc(postId, - PostStatus.DRAFT); + ContentPatchLog draftPatchLog = findLatestDraftBy(postId); + if (draftPatchLog == null) { + throw new NotFoundException("The latest draft version must not be null to update."); + } // Is the draft version 1 if (Objects.equals(draftPatchLog.getVersion(), BASE_VERSION)) { // If it is V1, modify the content directly. @@ -154,6 +168,18 @@ public class ContentPatchLogServiceImpl extends AbstractCrudService new NotFoundException( "Post content patch log was not found or has been deleted."));