Complete post removing api

pull/137/head
johnniang 2019-03-22 11:11:24 +08:00
parent 20e83c6b2b
commit 1721bb4e15
13 changed files with 187 additions and 31 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -62,4 +62,22 @@ public interface PostCategoryRepository extends BaseRepository<PostCategory, Int
*/
@NonNull
List<PostCategory> 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<PostCategory> 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<PostCategory> deleteByCategoryId(@NonNull Integer categoryId);
}

View File

@ -63,4 +63,21 @@ public interface PostTagRepository extends BaseRepository<PostTag, Integer> {
@NonNull
List<PostTag> findAllByPostIdIn(@NonNull Iterable<Integer> postIds);
/**
* Deletes post tags by post id.
*
* @param postId post id must not be null
* @return a list of post tag deleted
*/
@NonNull
List<PostTag> 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<PostTag> deleteByTagId(@NonNull Integer tagId);
}

View File

@ -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<Comment, Long> {
*/
@NonNull
Page<CommentVO> 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<Comment> 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<Integer, Long> countByPostIds(@Nullable Collection<Integer> postIds);
}

View File

@ -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<PostCategory, Integer>
* @return a category list map (key: postId, value: a list of category)
*/
@NonNull
Map<Integer, List<Category>> listCategoryListMap(Collection<Integer> postIds);
Map<Integer, List<Category>> listCategoryListMap(@Nullable Collection<Integer> postIds);
/**
* Lists post by category id.
@ -54,7 +55,7 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
* @return a list of post category
*/
@NonNull
List<PostCategory> mergeOrCreateByIfAbsent(@NonNull Integer postId, Set<Integer> categoryIds);
List<PostCategory> mergeOrCreateByIfAbsent(@NonNull Integer postId, @Nullable Set<Integer> categoryIds);
/**
* Lists by post id.
@ -82,4 +83,22 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
*/
@NonNull
Set<Integer> 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<PostCategory> 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<PostCategory> removeByCategoryId(@NonNull Integer categoryId);
}

View File

@ -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<Post, Integer> {
/**
* Save post with tags and categories
*
* @param post post
* @param tags tags
* @param categories categories
* @return saved post
*/
Post save(Post post, List<Tag> tags, List<Category> categories);
/**
* Remove post and relationship
*
* @param id id
*/
void remove(Integer id);
/**
* Lists latest posts of minimal.
*

View File

@ -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<PostTag, Integer> {
* @return tag map (key: postId, value: a list of tags)
*/
@NonNull
Map<Integer, List<Tag>> listTagListMapBy(Collection<Integer> postIds);
Map<Integer, List<Tag>> listTagListMapBy(@Nullable Collection<Integer> postIds);
/**
* Lists posts by tag id.
@ -65,7 +66,7 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
* @return a list of post tag
*/
@NonNull
List<PostTag> mergeOrCreateByIfAbsent(@NonNull Integer postId, Set<Integer> tagIds);
List<PostTag> mergeOrCreateByIfAbsent(@NonNull Integer postId, @Nullable Set<Integer> tagIds);
/**
* Lists post tags by post id.
@ -93,4 +94,22 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
*/
@NonNull
Set<Integer> 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<PostTag> 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<PostTag> removeByTagId(@NonNull Integer tagId);
}

View File

@ -154,4 +154,18 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
return postCategoryRepository.findAllCategoryIdsByPostId(postId);
}
@Override
public List<PostCategory> removeByPostId(Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return postCategoryRepository.deleteByPostId(postId);
}
@Override
public List<PostCategory> removeByCategoryId(Integer categoryId) {
Assert.notNull(categoryId, "Category id must not be null");
return postCategoryRepository.deleteByCategoryId(categoryId);
}
}

View File

@ -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<Post, Integer> 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<Post, Integer> 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<Post, Integer> 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<Post, Integer> 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<Post, Integer> 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<PostTag> postTags = postTagService.removeByPostId(postId);
log.debug("Removd post tags: [{}]", postTags);
// Remove post categories
List<PostCategory> 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<Post, Integer> 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<Post, Integer> implemen
* @return post detail vo
*/
@NonNull
private PostDetailVO convertTo(@NonNull Post post, Supplier<Set<Integer>> tagIdSetSupplier, Supplier<Set<Integer>> categoryIdSetSupplier) {
private PostDetailVO convertTo(@NonNull Post post, @Nullable Supplier<Set<Integer>> tagIdSetSupplier, @Nullable Supplier<Set<Integer>> categoryIdSetSupplier) {
Assert.notNull(post, "Post must not be null");
PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post);

View File

@ -170,4 +170,18 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
return postTagRepository.findAllTagIdsByPostId(postId);
}
@Override
public List<PostTag> removeByPostId(Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return postTagRepository.deleteByPostId(postId);
}
@Override
public List<PostTag> removeByTagId(Integer tagId) {
Assert.notNull(tagId, "Tag id must not be null");
return postTagRepository.deleteByTagId(tagId);
}
}

View File

@ -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);
}
}