mirror of https://github.com/halo-dev/halo
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 stylepull/1827/head
parent
8591a1ecbf
commit
bdb1eeb80e
|
@ -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<ContentPatch
|
|||
if (latestPatchLog == null) {
|
||||
// There is no patchLog record
|
||||
version = 1;
|
||||
} else if (PostStatus.PUBLISHED.equals(latestPatchLog.getStatus())
|
||||
|| PostStatus.INTIMATE.equals(latestPatchLog.getStatus())) {
|
||||
} else if (shouldUpgradeVersion(latestPatchLog)) {
|
||||
// There is no draft, a draft record needs to be created
|
||||
// so the version number needs to be incremented
|
||||
version = latestPatchLog.getVersion() + 1;
|
||||
|
@ -116,7 +116,7 @@ public class ContentPatchLogServiceImpl extends AbstractCrudService<ContentPatch
|
|||
contentPatchLog.setStatus(PostStatus.DRAFT);
|
||||
ContentPatchLog latestPatchLog =
|
||||
contentPatchLogRepository.findFirstByPostIdOrderByVersionDesc(postId);
|
||||
if (latestPatchLog != null) {
|
||||
if (shouldUpgradeVersion(latestPatchLog)) {
|
||||
contentPatchLog.setVersion(latestPatchLog.getVersion() + 1);
|
||||
} else {
|
||||
contentPatchLog.setVersion(BASE_VERSION);
|
||||
|
@ -125,19 +125,33 @@ public class ContentPatchLogServiceImpl extends AbstractCrudService<ContentPatch
|
|||
return contentPatchLog;
|
||||
}
|
||||
|
||||
private boolean shouldUpgradeVersion(ContentPatchLog latestPatchLog) {
|
||||
if (latestPatchLog == null) {
|
||||
return false;
|
||||
}
|
||||
return PostStatus.PUBLISHED.equals(latestPatchLog.getStatus())
|
||||
|| PostStatus.INTIMATE.equals(latestPatchLog.getStatus());
|
||||
}
|
||||
|
||||
private boolean existDraftBy(Integer postId) {
|
||||
ContentPatchLog contentPatchLog = new ContentPatchLog();
|
||||
contentPatchLog.setPostId(postId);
|
||||
contentPatchLog.setStatus(PostStatus.DRAFT);
|
||||
Example<ContentPatchLog> 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<ContentPatch
|
|||
return draftPatchLog;
|
||||
}
|
||||
|
||||
private ContentPatchLog findLatestDraftBy(Integer postId) {
|
||||
ContentPatchLog draftPatchLog =
|
||||
contentPatchLogRepository.findFirstByPostIdAndStatusOrderByVersionDesc(postId,
|
||||
PostStatus.DRAFT);
|
||||
if (draftPatchLog == null) {
|
||||
draftPatchLog =
|
||||
contentPatchLogRepository.findFirstByPostIdAndStatusOrderByVersionDesc(postId,
|
||||
PostStatus.RECYCLE);
|
||||
}
|
||||
return draftPatchLog;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PatchedContent applyPatch(ContentPatchLog patchLog) {
|
||||
Assert.notNull(patchLog, "The contentRecord must not be null.");
|
||||
|
@ -198,8 +224,7 @@ public class ContentPatchLogServiceImpl extends AbstractCrudService<ContentPatch
|
|||
|
||||
@Override
|
||||
public ContentPatchLog getDraftByPostId(Integer postId) {
|
||||
return contentPatchLogRepository.findFirstByPostIdAndStatusOrderByVersionDesc(postId,
|
||||
PostStatus.DRAFT);
|
||||
return findLatestDraftBy(postId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -219,8 +244,9 @@ public class ContentPatchLogServiceImpl extends AbstractCrudService<ContentPatch
|
|||
return applyPatch(contentPatchLog);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ContentPatchLog getById(Integer id) {
|
||||
public ContentPatchLog getById(@NonNull Integer id) {
|
||||
return contentPatchLogRepository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(
|
||||
"Post content patch log was not found or has been deleted."));
|
||||
|
|
Loading…
Reference in New Issue