diff --git a/src/main/java/cc/ryanc/halo/repository/PostRepository.java b/src/main/java/cc/ryanc/halo/repository/PostRepository.java index 66760ef06..4ed97b354 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostRepository.java @@ -48,6 +48,15 @@ public interface PostRepository extends BaseRepository, JpaSpecif */ long countByUrl(@NonNull String url); + /** + * Count by not url and post id not in. + * + * @param id post id must not be null + * @param url post url must not be null + * @return the count + */ + long countByIdNotAndUrl(@NonNull Integer id, @NonNull String url); + /** * Get post by url * diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java index 75263c4b1..72218bc73 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java @@ -189,7 +189,12 @@ public class PostServiceImpl extends AbstractCrudService implemen Assert.notNull(postOperation, "Post operation must not be null"); // Check url - long count = postRepository.countByUrl(post.getUrl()); + long count; + if (post.getId() != null) { + count = postRepository.countByIdNotAndUrl(post.getId(), post.getUrl()); + } else { + count = postRepository.countByUrl(post.getUrl()); + } if (count > 0) { throw new AlreadyExistsException("The post url has been exist already").setErrorData(post.getUrl());