feat: support post metas

pull/755/head
guqing 2019-12-01 18:35:53 +08:00
parent a3bffe67a7
commit 7cc3c787cb
14 changed files with 264 additions and 17 deletions

View File

@ -13,7 +13,9 @@ import run.halo.app.cache.StringCacheStore;
import run.halo.app.model.dto.post.BasePostMinimalDTO; import run.halo.app.model.dto.post.BasePostMinimalDTO;
import run.halo.app.model.dto.post.BasePostSimpleDTO; import run.halo.app.model.dto.post.BasePostSimpleDTO;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostMetaParam;
import run.halo.app.model.params.PostParam; import run.halo.app.model.params.PostParam;
import run.halo.app.model.params.PostQuery; import run.halo.app.model.params.PostQuery;
import run.halo.app.model.vo.PostDetailVO; import run.halo.app.model.vo.PostDetailVO;
@ -102,8 +104,7 @@ public class PostController {
@RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave) { @RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave) {
// Convert to // Convert to
Post post = postParam.convertTo(); Post post = postParam.convertTo();
return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds(), postParam.getPostMetas(), autoSave);
return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds(), autoSave);
} }
@PutMapping("{postId:\\d+}") @PutMapping("{postId:\\d+}")
@ -114,8 +115,7 @@ public class PostController {
Post postToUpdate = postService.getById(postId); Post postToUpdate = postService.getById(postId);
postParam.update(postToUpdate); postParam.update(postToUpdate);
return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds(), postParam.getPostMetas(), autoSave);
return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds(), autoSave);
} }
@PutMapping("{postId:\\d+}/status/{status}") @PutMapping("{postId:\\d+}/status/{status}")

View File

@ -16,6 +16,7 @@ import run.halo.app.cache.lock.CacheLock;
import run.halo.app.exception.ForbiddenException; import run.halo.app.exception.ForbiddenException;
import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.entity.Tag; import run.halo.app.model.entity.Tag;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.BaseCommentVO; import run.halo.app.model.vo.BaseCommentVO;
@ -46,6 +47,8 @@ public class ContentArchiveController {
private final PostCategoryService postCategoryService; private final PostCategoryService postCategoryService;
private final PostMetaService postMetaService;
private final PostTagService postTagService; private final PostTagService postTagService;
private final PostCommentService postCommentService; private final PostCommentService postCommentService;
@ -57,6 +60,7 @@ public class ContentArchiveController {
public ContentArchiveController(PostService postService, public ContentArchiveController(PostService postService,
ThemeService themeService, ThemeService themeService,
PostCategoryService postCategoryService, PostCategoryService postCategoryService,
PostMetaService postMetaService,
PostTagService postTagService, PostTagService postTagService,
PostCommentService postCommentService, PostCommentService postCommentService,
OptionService optionService, OptionService optionService,
@ -64,6 +68,7 @@ public class ContentArchiveController {
this.postService = postService; this.postService = postService;
this.themeService = themeService; this.themeService = themeService;
this.postCategoryService = postCategoryService; this.postCategoryService = postCategoryService;
this.postMetaService = postMetaService;
this.postTagService = postTagService; this.postTagService = postTagService;
this.postCommentService = postCommentService; this.postCommentService = postCommentService;
this.optionService = optionService; this.optionService = optionService;
@ -157,12 +162,14 @@ public class ContentArchiveController {
List<Category> categories = postCategoryService.listCategoriesBy(post.getId()); List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
List<Tag> tags = postTagService.listTagsBy(post.getId()); List<Tag> tags = postTagService.listTagsBy(post.getId());
List<PostMeta> postMetas = postMetaService.listPostMetasBy(post.getId());
Page<BaseCommentVO> comments = postCommentService.pageVosBy(post.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort)); Page<BaseCommentVO> comments = postCommentService.pageVosBy(post.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort));
model.addAttribute("is_post", true); model.addAttribute("is_post", true);
model.addAttribute("post", postService.convertToDetailVo(post)); model.addAttribute("post", postService.convertToDetailVo(post));
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
model.addAttribute("postMetas", postMetas);
model.addAttribute("tags", tags); model.addAttribute("tags", tags);
model.addAttribute("comments", comments); model.addAttribute("comments", comments);

View File

@ -52,7 +52,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
String blogUrl = optionService.getBlogBaseUrl(); String blogUrl = optionService.getBlogBaseUrl();
log.info("Halo started at {}", blogUrl); log.info("Halo started at {}", blogUrl);
log.info("Halo admin started at {}{}", blogUrl, haloProperties.getAdminPath()); log.info("Halo admin started at {}/{}", blogUrl, haloProperties.getAdminPath());
if (!haloProperties.isDocDisabled()) { if (!haloProperties.isDocDisabled()) {
log.debug("Halo api doc was enabled at {}/swagger-ui.html", blogUrl); log.debug("Halo api doc was enabled at {}/swagger-ui.html", blogUrl);
} }

View File

@ -0,0 +1,29 @@
package run.halo.app.model.dto;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import run.halo.app.model.dto.base.OutputConverter;
import run.halo.app.model.entity.PostMeta;
import java.util.Date;
/**
* Post meta output dto.
* @author guqing
* @date 2019-11-30
*/
@Data
@ToString
@EqualsAndHashCode
public class PostMetaDTO implements OutputConverter<PostMetaDTO, PostMeta> {
private Long id;
private Integer postId;
private String key;
private String value;
private Date createTime;
}

View File

@ -2,8 +2,10 @@ package run.halo.app.model.params;
import lombok.Data; import lombok.Data;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import run.halo.app.model.dto.base.InputConverter; import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.enums.PostCreateFrom; import run.halo.app.model.enums.PostCreateFrom;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.utils.SlugUtils; import run.halo.app.utils.SlugUtils;
@ -12,6 +14,7 @@ import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
@ -59,6 +62,8 @@ public class PostParam implements InputConverter<Post> {
private Set<Integer> categoryIds; private Set<Integer> categoryIds;
private Set<PostMetaParam> postMetas;
@Override @Override
public Post convertTo() { public Post convertTo() {
url = StringUtils.isBlank(url) ? SlugUtils.slug(title) : SlugUtils.slug(url); url = StringUtils.isBlank(url) ? SlugUtils.slug(title) : SlugUtils.slug(url);
@ -80,4 +85,17 @@ public class PostParam implements InputConverter<Post> {
InputConverter.super.update(post); InputConverter.super.update(post);
} }
public Set<PostMeta> getPostMetas() {
Set<PostMeta> postMetaSet = new HashSet<>();
if(CollectionUtils.isEmpty(postMetas)) {
return postMetaSet;
}
for(PostMetaParam postMetaParam : postMetas) {
PostMeta postMeta = postMetaParam.convertTo();
postMetaSet.add(postMeta);
}
return postMetaSet;
}
} }

View File

@ -4,9 +4,11 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
import run.halo.app.model.dto.CategoryDTO; import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.dto.PostMetaDTO;
import run.halo.app.model.dto.TagDTO; import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.dto.post.BasePostDetailDTO; import run.halo.app.model.dto.post.BasePostDetailDTO;
import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.entity.Tag; import run.halo.app.model.entity.Tag;
import java.util.List; import java.util.List;
@ -30,5 +32,9 @@ public class PostDetailVO extends BasePostDetailDTO {
private Set<Integer> categoryIds; private Set<Integer> categoryIds;
private List<CategoryDTO> categories; private List<CategoryDTO> categories;
private Set<Long> postMetaIds;
private List<PostMetaDTO> postMetas;
} }

View File

@ -3,8 +3,10 @@ package run.halo.app.model.vo;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import run.halo.app.model.dto.CategoryDTO; import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.dto.PostMetaDTO;
import run.halo.app.model.dto.TagDTO; import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.dto.post.BasePostSimpleDTO; import run.halo.app.model.dto.post.BasePostSimpleDTO;
import run.halo.app.model.entity.PostMeta;
import java.util.List; import java.util.List;
@ -24,4 +26,5 @@ public class PostListVO extends BasePostSimpleDTO {
private List<CategoryDTO> categories; private List<CategoryDTO> categories;
private List<PostMetaDTO> postMetas;
} }

View File

@ -1,8 +1,12 @@
package run.halo.app.repository; package run.halo.app.repository;
import org.springframework.lang.NonNull;
import run.halo.app.model.entity.PostMeta; import run.halo.app.model.entity.PostMeta;
import run.halo.app.repository.base.BaseMetaRepository; import run.halo.app.repository.base.BaseMetaRepository;
import java.util.List;
import java.util.Set;
/** /**
* PostMeta repository. * PostMeta repository.
* *
@ -11,4 +15,21 @@ import run.halo.app.repository.base.BaseMetaRepository;
* @date 2019-08-04 * @date 2019-08-04
*/ */
public interface PostMetaRepository extends BaseMetaRepository<PostMeta> { public interface PostMetaRepository extends BaseMetaRepository<PostMeta> {
/**
* Deletes post metas by post id.
*
* @param postId post id must not be null
* @return a list of post meta deleted
*/
@NonNull
List<PostMeta> deleteByPostId(@NonNull Integer postId);
/**
* Finds all post metas by post id.
* @param postIds post id must not be null
* @return a list of post meta
*/
@NonNull
List<PostMeta> findAllByPostIdIn(@NonNull Set<Integer> postIds);
} }

View File

@ -1,8 +1,15 @@
package run.halo.app.service; package run.halo.app.service;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.model.dto.PostMetaDTO;
import run.halo.app.model.entity.PostMeta; import run.halo.app.model.entity.PostMeta;
import run.halo.app.service.base.BaseMetaService; import run.halo.app.service.base.BaseMetaService;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* Post meta service interface. * Post meta service interface.
* *
@ -11,4 +18,17 @@ import run.halo.app.service.base.BaseMetaService;
* @date 2019-08-04 * @date 2019-08-04
*/ */
public interface PostMetaService extends BaseMetaService<PostMeta> { public interface PostMetaService extends BaseMetaService<PostMeta> {
List<PostMeta> createOrUpdateByPostId(Integer id, Set<PostMeta> postMetas);
List<PostMeta> listPostMetasBy(Integer id);
List<PostMeta> removeByPostId(Integer postId);
Map<Integer, List<PostMeta>> listPostMetaListMap(Set<Integer> postIds);
@NonNull
PostMetaDTO convertTo(@NonNull PostMeta postMeta);
@NonNull
List<PostMetaDTO> convertTo(@Nullable List<PostMeta> postMetaList);
} }

View File

@ -4,6 +4,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostQuery; import run.halo.app.model.params.PostQuery;
import run.halo.app.model.vo.ArchiveMonthVO; import run.halo.app.model.vo.ArchiveMonthVO;
@ -44,6 +45,19 @@ public interface PostService extends BasePostService<Post> {
@NonNull @NonNull
Page<Post> pageBy(@NonNull String keyword, @NonNull Pageable pageable); Page<Post> pageBy(@NonNull String keyword, @NonNull Pageable pageable);
/**
* Creates post by post param.
*
* @param post post must not be null
* @param tagIds tag id set
* @param categoryIds category id set
* @param postMetas post metas
* @param autoSave autoSave
* @return post created
*/
@NonNull
PostDetailVO createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> postMetas, boolean autoSave);
/** /**
* Creates post by post param. * Creates post by post param.
* *
@ -66,7 +80,7 @@ public interface PostService extends BasePostService<Post> {
* @return updated post * @return updated post
*/ */
@NonNull @NonNull
PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave); PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> postMetas, boolean autoSave);
/** /**
* Gets post by post status and url. * Gets post by post status and url.

View File

@ -212,7 +212,7 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
@Override @Override
public List<PostCategory> removeByPostId(Integer postId) { public List<PostCategory> removeByPostId(Integer postId) {
Assert.notNull(postId, "PoremoveByIdst id must not be null"); Assert.notNull(postId, "Post id must not be null");
return postCategoryRepository.deleteByPostId(postId); return postCategoryRepository.deleteByPostId(postId);
} }

View File

@ -2,11 +2,18 @@ package run.halo.app.service.impl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import run.halo.app.exception.NotFoundException; import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.PostMetaDTO;
import run.halo.app.model.entity.PostMeta; import run.halo.app.model.entity.PostMeta;
import run.halo.app.repository.PostMetaRepository; import run.halo.app.repository.PostMetaRepository;
import run.halo.app.repository.PostRepository; import run.halo.app.repository.PostRepository;
import run.halo.app.service.PostMetaService; import run.halo.app.service.PostMetaService;
import run.halo.app.utils.ServiceUtils;
import java.util.*;
import java.util.stream.Collectors;
/** /**
* Post meta service implementation class. * Post meta service implementation class.
@ -35,4 +42,74 @@ public class PostMetaServiceImpl extends BaseMetaServiceImpl<PostMeta> implement
postRepository.findById(postId) postRepository.findById(postId)
.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(postId)); .orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(postId));
} }
@Override
public List<PostMeta> createOrUpdateByPostId(Integer postId, Set<PostMeta> postMetas) {
Assert.notNull(postId, "Post id must not be null");
if (CollectionUtils.isEmpty(postMetas)) {
return Collections.emptyList();
}
// firstly remove post metas by post id
removeByPostId(postId);
// Save post metas
postMetas.forEach(postMeta -> {
postMeta.setPostId(postId);
postMetaRepository.save(postMeta);
});
return new ArrayList<>(postMetas);
}
@Override
public List<PostMeta> listPostMetasBy(Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return postMetaRepository.findAllByPostId(postId);
}
@Override
public List<PostMeta> removeByPostId(Integer postId) {
Assert.notNull(postId, "Post id must not be null of removeByPostId");
return postMetaRepository.deleteByPostId(postId);
}
@Override
public Map<Integer, List<PostMeta>> listPostMetaListMap(Set<Integer> postIds) {
if (CollectionUtils.isEmpty(postIds)) {
return Collections.emptyMap();
}
// Find all post metas
List<PostMeta> postMetas = postMetaRepository.findAllByPostIdIn(postIds);
// Convert to post meta map
Map<Long, PostMeta> postMetaMap = ServiceUtils.convertToMap(postMetas, PostMeta::getId);
// Create category list map
Map<Integer, List<PostMeta>> postMetaListMap = new HashMap<>();
// Foreach and collect
postMetas.forEach(postMeta -> postMetaListMap.computeIfAbsent(postMeta.getPostId(), postId -> new LinkedList<>())
.add(postMetaMap.get(postMeta.getId())));
return postMetaListMap;
}
@Override
public PostMetaDTO convertTo(PostMeta postMeta) {
Assert.notNull(postMeta, "Category must not be null");
return new PostMetaDTO().convertFrom(postMeta);
}
@Override
public List<PostMetaDTO> convertTo(List<PostMeta> postMetaList) {
if (CollectionUtils.isEmpty(postMetaList)) {
return Collections.emptyList();
}
return postMetaList.stream()
.map(this::convertTo)
.collect(Collectors.toList());
}
} }

View File

@ -18,6 +18,7 @@ import org.springframework.util.Assert;
import run.halo.app.event.logger.LogEvent; import run.halo.app.event.logger.LogEvent;
import run.halo.app.event.post.PostVisitEvent; import run.halo.app.event.post.PostVisitEvent;
import run.halo.app.model.dto.CategoryDTO; import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.dto.PostMetaDTO;
import run.halo.app.model.dto.TagDTO; import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.entity.*; import run.halo.app.model.entity.*;
import run.halo.app.model.enums.LogType; import run.halo.app.model.enums.LogType;
@ -67,6 +68,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
private final ApplicationEventPublisher eventPublisher; private final ApplicationEventPublisher eventPublisher;
private final PostMetaService postMetaService;
public PostServiceImpl(PostRepository postRepository, public PostServiceImpl(PostRepository postRepository,
TagService tagService, TagService tagService,
CategoryService categoryService, CategoryService categoryService,
@ -74,7 +77,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
PostCategoryService postCategoryService, PostCategoryService postCategoryService,
PostCommentService postCommentService, PostCommentService postCommentService,
ApplicationEventPublisher eventPublisher, ApplicationEventPublisher eventPublisher,
OptionService optionService) { OptionService optionService,
PostMetaService postMetaService) {
super(postRepository, optionService); super(postRepository, optionService);
this.postRepository = postRepository; this.postRepository = postRepository;
this.tagService = tagService; this.tagService = tagService;
@ -83,6 +87,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
this.postCategoryService = postCategoryService; this.postCategoryService = postCategoryService;
this.postCommentService = postCommentService; this.postCommentService = postCommentService;
this.eventPublisher = eventPublisher; this.eventPublisher = eventPublisher;
this.postMetaService = postMetaService;
} }
@Override @Override
@ -109,8 +114,19 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
@Override @Override
@Transactional @Transactional
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> postMetas, boolean autoSave) {
PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, postMetas);
if (!autoSave) {
// Log the creation
LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle());
eventPublisher.publishEvent(logEvent);
}
return createdPost;
}
@Override
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave) { public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave) {
PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds); PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, null);
if (!autoSave) { if (!autoSave) {
// Log the creation // Log the creation
LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle()); LogEvent logEvent = new LogEvent(this, createdPost.getId().toString(), LogType.POST_PUBLISHED, createdPost.getTitle());
@ -121,10 +137,10 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
@Override @Override
@Transactional @Transactional
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave) { public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> postMetas, boolean autoSave) {
// Set edit time // Set edit time
postToUpdate.setEditTime(DateUtils.now()); postToUpdate.setEditTime(DateUtils.now());
PostDetailVO updatedPost = createOrUpdate(postToUpdate, tagIds, categoryIds); PostDetailVO updatedPost = createOrUpdate(postToUpdate, tagIds, categoryIds, postMetas);
if (!autoSave) { if (!autoSave) {
// Log the creation // Log the creation
LogEvent logEvent = new LogEvent(this, updatedPost.getId().toString(), LogType.POST_EDITED, updatedPost.getTitle()); LogEvent logEvent = new LogEvent(this, updatedPost.getId().toString(), LogType.POST_EDITED, updatedPost.getTitle());
@ -227,6 +243,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
Set<Integer> tagIds = new HashSet<>(); Set<Integer> tagIds = new HashSet<>();
Set<Integer> categoryIds = new HashSet<>(); Set<Integer> categoryIds = new HashSet<>();
if (frontMatter.size() > 0) { if (frontMatter.size() > 0) {
for (String key : frontMatter.keySet()) { for (String key : frontMatter.keySet()) {
elementValue = frontMatter.get(key); elementValue = frontMatter.get(key);
@ -309,6 +326,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
public String exportMarkdown(Post post) { public String exportMarkdown(Post post) {
Assert.notNull(post, "Post must not be null"); Assert.notNull(post, "Post must not be null");
StrBuilder content = new StrBuilder("---\n"); StrBuilder content = new StrBuilder("---\n");
content.append("type: ").append("post").append("\n"); content.append("type: ").append("post").append("\n");
@ -338,6 +356,15 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
} }
List<PostMeta> postMetas = postMetaService.listPostMetasBy(post.getId());
if (postMetas.size() > 0) {
content.append("postMetas:").append("\n");
for (PostMeta postMeta : postMetas) {
content.append(" - ").append(postMeta.getKey()).append(" : ").append(postMeta.getValue()).append("\n");
}
}
content.append("---\n\n"); content.append("---\n\n");
content.append(post.getOriginalContent()); content.append(post.getOriginalContent());
return content.toString(); return content.toString();
@ -349,9 +376,10 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
List<Tag> tags = postTagService.listTagsBy(post.getId()); List<Tag> tags = postTagService.listTagsBy(post.getId());
// List categories // List categories
List<Category> categories = postCategoryService.listCategoriesBy(post.getId()); List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
// List postMetas
List<PostMeta> postMetas = postMetaService.listPostMetasBy(post.getId());
// Convert to detail vo // Convert to detail vo
return convertTo(post, tags, categories); return convertTo(post, tags, categories, postMetas);
} }
@Override @Override
@ -370,6 +398,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
log.debug("Removed post categories: [{}]", postCategories); log.debug("Removed post categories: [{}]", postCategories);
List<PostMeta> postMetas = postMetaService.removeByPostId(postId);
log.debug("Removed post metas: [{}]", postMetas);
Post deletedPost = super.removeById(postId); Post deletedPost = super.removeById(postId);
// Log it // Log it
@ -395,6 +426,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
// Get comment count // Get comment count
Map<Integer, Long> commentCountMap = postCommentService.countByPostIds(postIds); Map<Integer, Long> commentCountMap = postCommentService.countByPostIds(postIds);
// Get post meta list map
Map<Integer, List<PostMeta>> postMetaListMap = postMetaService.listPostMetaListMap(postIds);
return postPage.map(post -> { return postPage.map(post -> {
PostListVO postListVO = new PostListVO().convertFrom(post); PostListVO postListVO = new PostListVO().convertFrom(post);
@ -420,6 +454,14 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
.map(category -> (CategoryDTO) new CategoryDTO().convertFrom(category)) .map(category -> (CategoryDTO) new CategoryDTO().convertFrom(category))
.collect(Collectors.toList())); .collect(Collectors.toList()));
// Set post metas
postListVO.setPostMetas(Optional.ofNullable(postMetaListMap.get(post.getId()))
.orElseGet(LinkedList::new)
.stream()
.filter(Objects::nonNull)
.map(postMeta -> (PostMetaDTO) new PostMetaDTO().convertFrom(postMeta))
.collect(Collectors.toList()));
// Set comment count // Set comment count
postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L)); postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
@ -433,10 +475,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
* @param post post must not be null * @param post post must not be null
* @param tags tags * @param tags tags
* @param categories categories * @param categories categories
* @param postMetaList postMetas
* @return post detail vo * @return post detail vo
*/ */
@NonNull @NonNull
private PostDetailVO convertTo(@NonNull Post post, @Nullable List<Tag> tags, @Nullable List<Category> categories) { private PostDetailVO convertTo(@NonNull Post post, @Nullable List<Tag> tags, @Nullable List<Category> categories, List<PostMeta> postMetaList) {
Assert.notNull(post, "Post must not be null"); Assert.notNull(post, "Post must not be null");
// Convert to base detail vo // Convert to base detail vo
@ -445,6 +488,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
// Extract ids // Extract ids
Set<Integer> tagIds = ServiceUtils.fetchProperty(tags, Tag::getId); Set<Integer> tagIds = ServiceUtils.fetchProperty(tags, Tag::getId);
Set<Integer> categoryIds = ServiceUtils.fetchProperty(categories, Category::getId); Set<Integer> categoryIds = ServiceUtils.fetchProperty(categories, Category::getId);
Set<Long> postMetaIds = ServiceUtils.fetchProperty(postMetaList, PostMeta::getId);
// Get post tag ids // Get post tag ids
postDetailVO.setTagIds(tagIds); postDetailVO.setTagIds(tagIds);
@ -454,6 +498,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
postDetailVO.setCategoryIds(categoryIds); postDetailVO.setCategoryIds(categoryIds);
postDetailVO.setCategories(categoryService.convertTo(categories)); postDetailVO.setCategories(categoryService.convertTo(categories));
// Get post meta ids
postDetailVO.setPostMetaIds(postMetaIds);
postDetailVO.setPostMetas(postMetaService.convertTo(postMetaList));
return postDetailVO; return postDetailVO;
} }
@ -499,7 +546,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
}; };
} }
private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds) { private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> postMetas) {
Assert.notNull(post, "Post param must not be null"); Assert.notNull(post, "Post param must not be null");
// Create or update post // Create or update post
@ -525,7 +572,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
log.debug("Created post categories: [{}]", postCategories); log.debug("Created post categories: [{}]", postCategories);
// Create post meta data
List<PostMeta> postMetaList = postMetaService.createOrUpdateByPostId(post.getId(), postMetas);
log.debug("Created post postMetas: [{}]", postMetaList);
// Convert to post detail vo // Convert to post detail vo
return convertTo(post, tags, categories); return convertTo(post, tags, categories, postMetaList);
} }
} }

View File

@ -56,3 +56,4 @@ logging:
halo: halo:
download-timeout: 5m download-timeout: 5m
doc-disabled: false