From 8e420914aa2420b922815e33474f3379c4e948c1 Mon Sep 17 00:00:00 2001 From: johnniang Date: Mon, 29 Jul 2019 20:36:44 +0800 Subject: [PATCH] Add tags and categories for returning as content post api requests --- .../content/ContentArchiveController.java | 2 +- .../content/api/PostController.java | 11 +++--- .../freemarker/tag/CategoryTagDirective.java | 2 +- .../run/halo/app/model/vo/PostDetailVO.java | 14 +++++++- .../halo/app/service/PostCategoryService.java | 2 +- .../service/impl/PostCategoryServiceImpl.java | 2 +- .../app/service/impl/PostServiceImpl.java | 35 ++++++++++++------- 7 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java index acb363232..95c19469a 100644 --- a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java +++ b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java @@ -115,7 +115,7 @@ public class ContentArchiveController { postService.getPrePost(post.getCreateTime()).ifPresent(prePost -> model.addAttribute("prePost", prePost)); - List categories = postCategoryService.listCategoryBy(post.getId()); + List categories = postCategoryService.listCategoriesBy(post.getId()); List tags = postTagService.listTagsBy(post.getId()); Page comments = postCommentService.pageVosBy(post.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort)); diff --git a/src/main/java/run/halo/app/controller/content/api/PostController.java b/src/main/java/run/halo/app/controller/content/api/PostController.java index 6ec1514c5..eceaf2085 100644 --- a/src/main/java/run/halo/app/controller/content/api/PostController.java +++ b/src/main/java/run/halo/app/controller/content/api/PostController.java @@ -20,6 +20,7 @@ import run.halo.app.model.params.PostCommentParam; import run.halo.app.model.vo.BaseCommentVO; import run.halo.app.model.vo.BaseCommentWithParentVO; import run.halo.app.model.vo.CommentWithHasChildrenVO; +import run.halo.app.model.vo.PostDetailVO; import run.halo.app.service.OptionService; import run.halo.app.service.PostCommentService; import run.halo.app.service.PostService; @@ -69,22 +70,22 @@ public class PostController { @GetMapping("{postId:\\d+}") @ApiOperation("Gets a post") - public BasePostDetailDTO getBy(@PathVariable("postId") Integer postId, + public PostDetailVO getBy(@PathVariable("postId") Integer postId, @RequestParam(value = "formatDisabled", required = false, defaultValue = "true") Boolean formatDisabled, @RequestParam(value = "sourceDisabled", required = false, defaultValue = "false") Boolean sourceDisabled) { - BasePostDetailDTO detailDTO = postService.convertToDetail(postService.getById(postId)); + PostDetailVO postDetailVO = postService.convertToDetailVo(postService.getById(postId)); if (formatDisabled) { // Clear the format content - detailDTO.setFormatContent(null); + postDetailVO.setFormatContent(null); } if (sourceDisabled) { // Clear the original content - detailDTO.setOriginalContent(null); + postDetailVO.setOriginalContent(null); } - return detailDTO; + return postDetailVO; } @GetMapping("{postId:\\d+}/comments/top_view") diff --git a/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java index 44bd9d2de..54056d30d 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java @@ -44,7 +44,7 @@ public class CategoryTagDirective implements TemplateDirectiveModel { break; case "listByPostId": Integer postId = Integer.parseInt(params.get("postId").toString()); - env.setVariable("categories", builder.build().wrap(postCategoryService.listCategoryBy(postId))); + env.setVariable("categories", builder.build().wrap(postCategoryService.listCategoriesBy(postId))); break; case "count": env.setVariable("count", builder.build().wrap(categoryService.count())); diff --git a/src/main/java/run/halo/app/model/vo/PostDetailVO.java b/src/main/java/run/halo/app/model/vo/PostDetailVO.java index f87927319..410361881 100644 --- a/src/main/java/run/halo/app/model/vo/PostDetailVO.java +++ b/src/main/java/run/halo/app/model/vo/PostDetailVO.java @@ -2,8 +2,14 @@ package run.halo.app.model.vo; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.ToString; +import run.halo.app.model.dto.CategoryDTO; +import run.halo.app.model.dto.TagDTO; import run.halo.app.model.dto.post.BasePostDetailDTO; +import run.halo.app.model.entity.Category; +import run.halo.app.model.entity.Tag; +import java.util.List; import java.util.Set; /** @@ -12,11 +18,17 @@ import java.util.Set; * @author johnniang * @date 3/21/19 */ -@EqualsAndHashCode(callSuper = true) @Data +@ToString(callSuper = true) +@EqualsAndHashCode(callSuper = true) public class PostDetailVO extends BasePostDetailDTO { private Set tagIds; + private List tags; + private Set categoryIds; + + private List categories; } + diff --git a/src/main/java/run/halo/app/service/PostCategoryService.java b/src/main/java/run/halo/app/service/PostCategoryService.java index bca6cb80c..383666108 100644 --- a/src/main/java/run/halo/app/service/PostCategoryService.java +++ b/src/main/java/run/halo/app/service/PostCategoryService.java @@ -34,7 +34,7 @@ public interface PostCategoryService extends CrudService * @return a list of category */ @NonNull - List listCategoryBy(@NonNull Integer postId); + List listCategoriesBy(@NonNull Integer postId); /** * List category list map by post id collection. diff --git a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java index cd79ee8e0..0f82f4b58 100644 --- a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java @@ -48,7 +48,7 @@ public class PostCategoryServiceImpl extends AbstractCrudService listCategoryBy(Integer postId) { + public List listCategoriesBy(Integer postId) { Assert.notNull(postId, "Post id must not be null"); // Find all category ids diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index dd9d52e30..02a3ddc30 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -194,9 +194,7 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe log.debug("Created post categories: [{}]", postCategories); // Convert to post detail vo - return convertTo(post, - () -> ServiceUtils.fetchProperty(postTags, PostTag::getTagId), - () -> ServiceUtils.fetchProperty(postCategories, PostCategory::getCategoryId)); + return convertTo(post, tags, categories); } @Override @@ -397,7 +395,7 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe } } - List categories = postCategoryService.listCategoryBy(post.getId()); + List categories = postCategoryService.listCategoriesBy(post.getId()); if (categories.size() > 0) { content.append("categories:").append("\n"); @@ -413,9 +411,13 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe @Override public PostDetailVO convertToDetailVo(Post post) { - return convertTo(post, - () -> postTagService.listTagIdsByPostId(post.getId()), - () -> postCategoryService.listCategoryIdsByPostId(post.getId())); + // List tags + List tags = postTagService.listTagsBy(post.getId()); + // List categories + List categories = postCategoryService.listCategoriesBy(post.getId()); + + // Convert to detail vo + return convertTo(post, tags, categories); } @Override @@ -494,22 +496,29 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe /** * Converts to post detail vo. * - * @param post post must not be null - * @param tagIdSetSupplier tag id set supplier - * @param categoryIdSetSupplier category id set supplier + * @param post post must not be null + * @param tags tags + * @param categories categories * @return post detail vo */ @NonNull - private PostDetailVO convertTo(@NonNull Post post, @Nullable Supplier> tagIdSetSupplier, @Nullable Supplier> categoryIdSetSupplier) { + private PostDetailVO convertTo(@NonNull Post post, @Nullable List tags, @Nullable List categories) { Assert.notNull(post, "Post must not be null"); + // Convert to base detail vo PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post); + // Extract ids + Set tagIds = ServiceUtils.fetchProperty(tags, Tag::getId); + Set categoryIds = ServiceUtils.fetchProperty(categories, Category::getId); + // Get post tag ids - postDetailVO.setTagIds(tagIdSetSupplier == null ? Collections.emptySet() : tagIdSetSupplier.get()); + postDetailVO.setTagIds(tagIds); + postDetailVO.setTags(tagService.convertTo(tags)); // Get post category ids - postDetailVO.setCategoryIds(categoryIdSetSupplier == null ? Collections.emptySet() : categoryIdSetSupplier.get()); + postDetailVO.setCategoryIds(categoryIds); + postDetailVO.setCategories(categoryService.convertTo(categories)); return postDetailVO; }