Add tags and categories for returning as content post api requests

pull/296/head
johnniang 2019-07-29 20:36:44 +08:00
parent acc1448f6d
commit 8e420914aa
7 changed files with 45 additions and 23 deletions

View File

@ -115,7 +115,7 @@ public class ContentArchiveController {
postService.getPrePost(post.getCreateTime()).ifPresent(prePost -> model.addAttribute("prePost", prePost));
List<Category> categories = postCategoryService.listCategoryBy(post.getId());
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
List<Tag> tags = postTagService.listTagsBy(post.getId());
Page<BaseCommentVO> comments = postCommentService.pageVosBy(post.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort));

View File

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

View File

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

View File

@ -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<Integer> tagIds;
private List<TagDTO> tags;
private Set<Integer> categoryIds;
private List<CategoryDTO> categories;
}

View File

@ -34,7 +34,7 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
* @return a list of category
*/
@NonNull
List<Category> listCategoryBy(@NonNull Integer postId);
List<Category> listCategoriesBy(@NonNull Integer postId);
/**
* List category list map by post id collection.

View File

@ -48,7 +48,7 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
}
@Override
public List<Category> listCategoryBy(Integer postId) {
public List<Category> listCategoriesBy(Integer postId) {
Assert.notNull(postId, "Post id must not be null");
// Find all category ids

View File

@ -194,9 +194,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> 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<Post> implements PostSe
}
}
List<Category> categories = postCategoryService.listCategoryBy(post.getId());
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
if (categories.size() > 0) {
content.append("categories:").append("\n");
@ -413,9 +411,13 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
@Override
public PostDetailVO convertToDetailVo(Post post) {
return convertTo(post,
() -> postTagService.listTagIdsByPostId(post.getId()),
() -> postCategoryService.listCategoryIdsByPostId(post.getId()));
// List tags
List<Tag> tags = postTagService.listTagsBy(post.getId());
// List categories
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
// Convert to detail vo
return convertTo(post, tags, categories);
}
@Override
@ -494,22 +496,29 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> 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<Set<Integer>> tagIdSetSupplier, @Nullable Supplier<Set<Integer>> categoryIdSetSupplier) {
private PostDetailVO convertTo(@NonNull Post post, @Nullable List<Tag> tags, @Nullable List<Category> categories) {
Assert.notNull(post, "Post must not be null");
// Convert to base detail vo
PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post);
// Extract ids
Set<Integer> tagIds = ServiceUtils.fetchProperty(tags, Tag::getId);
Set<Integer> 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;
}