Enhance post creation api

pull/137/head
johnniang 2019-03-21 22:11:15 +08:00
parent 3d80e34215
commit f27f4f8833
4 changed files with 19 additions and 19 deletions

View File

@ -5,6 +5,7 @@ import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -44,4 +45,13 @@ public interface PostCategoryRepository extends BaseRepository<PostCategory, Int
*/
@NonNull
List<PostCategory> findAllByPostIdIn(@NonNull Iterable<Integer> postIds);
/**
* Finds all post categories by post id.
*
* @param postId post id must not be null
* @return a list of post categories
*/
@NonNull
List<PostCategory> findAllByPostId(@NonNull Integer postId);
}

View File

@ -96,12 +96,15 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
}
// Build post categories
List<PostCategory> postCategories = categoryIds.stream().map(categoryId -> {
Set<PostCategory> postCategories = categoryIds.stream().map(categoryId -> {
PostCategory postCategory = new PostCategory();
postCategory.setPostId(postId);
postCategory.setCategoryId(categoryId);
return postCategory;
}).collect(Collectors.toList());
}).collect(Collectors.toSet());
// List all post categories and remove them
postCategories.removeAll(postCategoryRepository.findAllByPostId(postId));
// Create them
return createInBatch(postCategories);

View File

@ -120,6 +120,9 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
return postTag;
}).collect(Collectors.toSet());
// Get post tag exist and remove them
postTags.removeAll(postTagRepository.findAllByPostId(postId));
// Create in batch
return createInBatch(postTags);
}

View File

@ -31,24 +31,8 @@ public class PostController {
private final PostService postService;
private final TagService tagService;
private final CategoryService categoryService;
private final PostTagService postTagService;
private final PostCategoryService postCategoryService;
public PostController(PostService postService,
TagService tagService,
CategoryService categoryService,
PostTagService postTagService,
PostCategoryService postCategoryService) {
public PostController(PostService postService) {
this.postService = postService;
this.tagService = tagService;
this.categoryService = categoryService;
this.postTagService = postTagService;
this.postCategoryService = postCategoryService;
}
@GetMapping("latest")