diff --git a/src/main/java/cc/ryanc/halo/model/entity/PostCategory.java b/src/main/java/cc/ryanc/halo/model/entity/PostCategory.java index 967c46d2c..a38e7b656 100644 --- a/src/main/java/cc/ryanc/halo/model/entity/PostCategory.java +++ b/src/main/java/cc/ryanc/halo/model/entity/PostCategory.java @@ -21,7 +21,7 @@ import java.util.Objects; @SQLDelete(sql = "update post_categories set deleted = true where id = ?") @Where(clause = "deleted = false") @Data -@ToString +@ToString(callSuper = true) public class PostCategory extends BaseEntity { @Id diff --git a/src/main/java/cc/ryanc/halo/model/entity/PostTag.java b/src/main/java/cc/ryanc/halo/model/entity/PostTag.java index 7473b1e5e..7a0a3e556 100644 --- a/src/main/java/cc/ryanc/halo/model/entity/PostTag.java +++ b/src/main/java/cc/ryanc/halo/model/entity/PostTag.java @@ -19,7 +19,7 @@ import java.util.Objects; @Table(name = "post_tags") @SQLDelete(sql = "update post_tags set deleted = true where id = ?") @Where(clause = "deleted = false") -@ToString +@ToString(callSuper = true) public class PostTag extends BaseEntity { @Id diff --git a/src/main/java/cc/ryanc/halo/model/projection/CommentCountProjection.java b/src/main/java/cc/ryanc/halo/model/projection/CommentCountProjection.java new file mode 100644 index 000000000..dcbd0aa30 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/projection/CommentCountProjection.java @@ -0,0 +1,21 @@ +package cc.ryanc.halo.model.projection; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Comment count projection + * + * @author johnniang + * @date 3/22/19 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CommentCountProjection { + + private Long count; + + private Integer postId; +} diff --git a/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java b/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java index 9ed98f8e2..e70e8f6ac 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java @@ -62,4 +62,22 @@ public interface PostCategoryRepository extends BaseRepository findAllByCategoryId(@NonNull Integer categoryId); + + /** + * Deletes post categories by post id. + * + * @param postId post id must not be null + * @return a list of post category deleted + */ + @NonNull + List deleteByPostId(@NonNull Integer postId); + + /** + * Deletes post categories by category id. + * + * @param categoryId category id must not be null + * @return a list of post category deleted + */ + @NonNull + List deleteByCategoryId(@NonNull Integer categoryId); } diff --git a/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java b/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java index f4abc2a76..8323f28db 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java @@ -63,4 +63,21 @@ public interface PostTagRepository extends BaseRepository { @NonNull List findAllByPostIdIn(@NonNull Iterable postIds); + /** + * Deletes post tags by post id. + * + * @param postId post id must not be null + * @return a list of post tag deleted + */ + @NonNull + List deleteByPostId(@NonNull Integer postId); + + /** + * Deletes post tags by tag id. + * + * @param tagId tag id must not be null + * @return a list of post tag deleted + */ + @NonNull + List deleteByTagId(@NonNull Integer tagId); } diff --git a/src/main/java/cc/ryanc/halo/service/CommentService.java b/src/main/java/cc/ryanc/halo/service/CommentService.java index 1036131fb..94f22cc40 100644 --- a/src/main/java/cc/ryanc/halo/service/CommentService.java +++ b/src/main/java/cc/ryanc/halo/service/CommentService.java @@ -7,6 +7,11 @@ import cc.ryanc.halo.service.base.CrudService; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; + +import java.util.Collection; +import java.util.List; +import java.util.Map; /** * Comment service. @@ -33,4 +38,22 @@ public interface CommentService extends CrudService { */ @NonNull Page pageBy(@NonNull CommentStatus status, @NonNull Pageable pageable); + + /** + * Lists comments by post id. + * + * @param postId post id must not be null + * @return a list of comment + */ + @NonNull + List listBy(@NonNull Integer postId); + + /** + * Count by post id collection. + * + * @param postIds post id collection + * @return a count map, key: post id, value: comment count + */ + @NonNull + Map countByPostIds(@Nullable Collection postIds); } diff --git a/src/main/java/cc/ryanc/halo/service/PostCategoryService.java b/src/main/java/cc/ryanc/halo/service/PostCategoryService.java index 58ad8d541..ee37d4e8d 100644 --- a/src/main/java/cc/ryanc/halo/service/PostCategoryService.java +++ b/src/main/java/cc/ryanc/halo/service/PostCategoryService.java @@ -5,6 +5,7 @@ import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.PostCategory; import cc.ryanc.halo.service.base.CrudService; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import java.util.Collection; import java.util.List; @@ -35,7 +36,7 @@ public interface PostCategoryService extends CrudService * @return a category list map (key: postId, value: a list of category) */ @NonNull - Map> listCategoryListMap(Collection postIds); + Map> listCategoryListMap(@Nullable Collection postIds); /** * Lists post by category id. @@ -54,7 +55,7 @@ public interface PostCategoryService extends CrudService * @return a list of post category */ @NonNull - List mergeOrCreateByIfAbsent(@NonNull Integer postId, Set categoryIds); + List mergeOrCreateByIfAbsent(@NonNull Integer postId, @Nullable Set categoryIds); /** * Lists by post id. @@ -82,4 +83,22 @@ public interface PostCategoryService extends CrudService */ @NonNull Set listCategoryIdsByPostId(@NonNull Integer postId); + + /** + * Removes post categories by post id. + * + * @param postId post id must not be null + * @return a list of post category deleted + */ + @NonNull + List removeByPostId(@NonNull Integer postId); + + /** + * Removes post categories by category id. + * + * @param categoryId category id must not be null + * @return a list of post category deleted + */ + @NonNull + List removeByCategoryId(@NonNull Integer categoryId); } diff --git a/src/main/java/cc/ryanc/halo/service/PostService.java b/src/main/java/cc/ryanc/halo/service/PostService.java index 798f41422..178c3bfae 100755 --- a/src/main/java/cc/ryanc/halo/service/PostService.java +++ b/src/main/java/cc/ryanc/halo/service/PostService.java @@ -2,9 +2,7 @@ package cc.ryanc.halo.service; import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO; import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO; -import cc.ryanc.halo.model.entity.Category; import cc.ryanc.halo.model.entity.Post; -import cc.ryanc.halo.model.entity.Tag; import cc.ryanc.halo.model.enums.PostStatus; import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.vo.PostDetailVO; @@ -15,7 +13,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.lang.NonNull; import org.springframework.transaction.annotation.Transactional; -import java.util.List; import java.util.Set; /** @@ -26,23 +23,6 @@ import java.util.Set; */ public interface PostService extends CrudService { - /** - * Save post with tags and categories - * - * @param post post - * @param tags tags - * @param categories categories - * @return saved post - */ - Post save(Post post, List tags, List categories); - - /** - * Remove post and relationship - * - * @param id id - */ - void remove(Integer id); - /** * Lists latest posts of minimal. * diff --git a/src/main/java/cc/ryanc/halo/service/PostTagService.java b/src/main/java/cc/ryanc/halo/service/PostTagService.java index 75bf25e3a..d47a72cfd 100644 --- a/src/main/java/cc/ryanc/halo/service/PostTagService.java +++ b/src/main/java/cc/ryanc/halo/service/PostTagService.java @@ -7,6 +7,7 @@ import cc.ryanc.halo.model.entity.Tag; import cc.ryanc.halo.service.base.CrudService; import org.springframework.data.domain.Sort; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import java.util.Collection; import java.util.List; @@ -46,7 +47,7 @@ public interface PostTagService extends CrudService { * @return tag map (key: postId, value: a list of tags) */ @NonNull - Map> listTagListMapBy(Collection postIds); + Map> listTagListMapBy(@Nullable Collection postIds); /** * Lists posts by tag id. @@ -65,7 +66,7 @@ public interface PostTagService extends CrudService { * @return a list of post tag */ @NonNull - List mergeOrCreateByIfAbsent(@NonNull Integer postId, Set tagIds); + List mergeOrCreateByIfAbsent(@NonNull Integer postId, @Nullable Set tagIds); /** * Lists post tags by post id. @@ -93,4 +94,22 @@ public interface PostTagService extends CrudService { */ @NonNull Set listTagIdsByPostId(@NonNull Integer postId); + + /** + * Removes post tags by post id. + * + * @param postId post id must not be null + * @return a list of post tag + */ + @NonNull + List removeByPostId(@NonNull Integer postId); + + /** + * Removes post tags by tag id. + * + * @param tagId tag id must not be null + * @return a list of post tag + */ + @NonNull + List removeByTagId(@NonNull Integer tagId); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java index 1ff9f9a68..b8b51a911 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java @@ -154,4 +154,18 @@ public class PostCategoryServiceImpl extends AbstractCrudService removeByPostId(Integer postId) { + Assert.notNull(postId, "Post id must not be null"); + + return postCategoryRepository.deleteByPostId(postId); + } + + @Override + public List removeByCategoryId(Integer categoryId) { + Assert.notNull(categoryId, "Category id must not be null"); + + return postCategoryRepository.deleteByCategoryId(categoryId); + } } 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 72218bc73..6324160fe 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java @@ -23,7 +23,9 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import java.util.Collections; @@ -54,6 +56,8 @@ public class PostServiceImpl extends AbstractCrudService implemen private final PostCategoryService postCategoryService; + private final CommentService commentService; + public PostServiceImpl(PostRepository postRepository, TagService tagService, CategoryService categoryService, @@ -160,7 +164,7 @@ public class PostServiceImpl extends AbstractCrudService implemen } /** - * Count posts by status and type + * Counts posts by status and type * * @param status status * @param type type @@ -191,8 +195,10 @@ public class PostServiceImpl extends AbstractCrudService implemen // Check url long count; if (post.getId() != null) { + // For updating count = postRepository.countByIdNotAndUrl(post.getId(), post.getUrl()); } else { + // For creating count = postRepository.countByUrl(post.getUrl()); } @@ -229,7 +235,7 @@ public class PostServiceImpl extends AbstractCrudService implemen } /** - * Get post by url. + * Gets post by url. * * @param url post url. * @param type post type enum. @@ -250,8 +256,28 @@ public class PostServiceImpl extends AbstractCrudService implemen return convertTo(post); } + @Override + @Transactional + public Post removeById(Integer postId) { + Assert.notNull(postId, "Post id must not be null"); + + log.debug("Removing post: [{}]", postId); + + // Remove post tags + List postTags = postTagService.removeByPostId(postId); + + log.debug("Removd post tags: [{}]", postTags); + + // Remove post categories + List postCategories = postCategoryService.removeByPostId(postId); + + log.debug("Removed post categories: [{}]", postCategories); + + return super.removeById(postId); + } + /** - * Convert to post detail vo. + * Converts to post detail vo. * * @param post post must not be null * @return post detail vo @@ -264,7 +290,7 @@ public class PostServiceImpl extends AbstractCrudService implemen } /** - * Convert to post detail vo. + * Converts to post detail vo. * * @param post post must not be null * @param tagIdSetSupplier tag id set supplier @@ -272,7 +298,7 @@ public class PostServiceImpl extends AbstractCrudService implemen * @return post detail vo */ @NonNull - private PostDetailVO convertTo(@NonNull Post post, Supplier> tagIdSetSupplier, Supplier> categoryIdSetSupplier) { + private PostDetailVO convertTo(@NonNull Post post, @Nullable Supplier> tagIdSetSupplier, @Nullable Supplier> categoryIdSetSupplier) { Assert.notNull(post, "Post must not be null"); PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post); diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java index f8e03be14..af0d3f18e 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java @@ -170,4 +170,18 @@ public class PostTagServiceImpl extends AbstractCrudService im return postTagRepository.findAllTagIdsByPostId(postId); } + + @Override + public List removeByPostId(Integer postId) { + Assert.notNull(postId, "Post id must not be null"); + + return postTagRepository.deleteByPostId(postId); + } + + @Override + public List removeByTagId(Integer tagId) { + Assert.notNull(tagId, "Tag id must not be null"); + + return postTagRepository.deleteByTagId(tagId); + } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java index b52de55f5..17ddc182e 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java @@ -72,4 +72,9 @@ public class PostController { return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds()); } + @DeleteMapping("{postId:\\d+}") + public void deletePermanently(@PathVariable("postId") Integer postId) { + // Remove it + postService.removeById(postId); + } }