mirror of https://github.com/halo-dev/halo
Complete post getting api
parent
af4c31b676
commit
7935ccd728
|
@ -46,11 +46,6 @@ public class PostSimpleOutputDTO extends PostMinimalOutputDTO {
|
||||||
*/
|
*/
|
||||||
private Boolean disallowComment;
|
private Boolean disallowComment;
|
||||||
|
|
||||||
/**
|
|
||||||
* 文章密码
|
|
||||||
*/
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义渲染模板名称
|
* 自定义渲染模板名称
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -134,6 +134,7 @@ public class Post extends BaseEntity {
|
||||||
public void prePersist() {
|
public void prePersist() {
|
||||||
super.prePersist();
|
super.prePersist();
|
||||||
id = null;
|
id = null;
|
||||||
|
editTime = getCreateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import cc.ryanc.halo.repository.base.BaseRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -50,8 +49,17 @@ public interface PostCategoryRepository extends BaseRepository<PostCategory, Int
|
||||||
* Finds all post categories by post id.
|
* Finds all post categories by post id.
|
||||||
*
|
*
|
||||||
* @param postId post id must not be null
|
* @param postId post id must not be null
|
||||||
* @return a list of post categories
|
* @return a list of post category
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
List<PostCategory> findAllByPostId(@NonNull Integer postId);
|
List<PostCategory> findAllByPostId(@NonNull Integer postId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds all post categories by category id.
|
||||||
|
*
|
||||||
|
* @param categoryId category id must not be null
|
||||||
|
* @return a list of post category
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
List<PostCategory> findAllByCategoryId(@NonNull Integer categoryId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,4 +55,31 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
List<PostCategory> mergeOrCreateByIfAbsent(@NonNull Integer postId, Set<Integer> categoryIds);
|
List<PostCategory> mergeOrCreateByIfAbsent(@NonNull Integer postId, Set<Integer> categoryIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists by post id.
|
||||||
|
*
|
||||||
|
* @param postId post id must not be null
|
||||||
|
* @return a list of post category
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
List<PostCategory> listByPostId(@NonNull Integer postId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists by category id.
|
||||||
|
*
|
||||||
|
* @param categoryId category id must not be null
|
||||||
|
* @return a list of post category
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
List<PostCategory> listByCategoryId(@NonNull Integer categoryId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List category id set by post id.
|
||||||
|
*
|
||||||
|
* @param postId post id must not be null
|
||||||
|
* @return a set of category id
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Set<Integer> listCategoryIdsByPostId(@NonNull Integer postId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,4 +66,31 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
List<PostTag> mergeOrCreateByIfAbsent(@NonNull Integer postId, Set<Integer> tagIds);
|
List<PostTag> mergeOrCreateByIfAbsent(@NonNull Integer postId, Set<Integer> tagIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists post tags by post id.
|
||||||
|
*
|
||||||
|
* @param postId post id must not be null
|
||||||
|
* @return a list of post tag
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
List<PostTag> listByPostId(@NonNull Integer postId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists post tags by tag id.
|
||||||
|
*
|
||||||
|
* @param tagId tag id must not be null
|
||||||
|
* @return a list of post tag
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
List<PostTag> listByTagId(@NonNull Integer tagId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists tag id set by post id.
|
||||||
|
*
|
||||||
|
* @param postId post id must not be null
|
||||||
|
* @return a set of tag id
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Set<Integer> listTagIdsByPostId(@NonNull Integer postId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,4 +133,25 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
|
||||||
// Create them
|
// Create them
|
||||||
return postCategories;
|
return postCategories;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PostCategory> listByPostId(Integer postId) {
|
||||||
|
Assert.notNull(postId, "Post id must not be null");
|
||||||
|
|
||||||
|
return postCategoryRepository.findAllByPostId(postId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PostCategory> listByCategoryId(Integer categoryId) {
|
||||||
|
Assert.notNull(categoryId, "Category id must not be null");
|
||||||
|
|
||||||
|
return postCategoryRepository.findAllByCategoryId(categoryId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> listCategoryIdsByPostId(Integer postId) {
|
||||||
|
Assert.notNull(postId, "Post id must not be null");
|
||||||
|
|
||||||
|
return postCategoryRepository.findAllCategoryIdsByPostId(postId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import cc.ryanc.halo.model.vo.PostListVO;
|
||||||
import cc.ryanc.halo.repository.PostRepository;
|
import cc.ryanc.halo.repository.PostRepository;
|
||||||
import cc.ryanc.halo.service.*;
|
import cc.ryanc.halo.service.*;
|
||||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||||
|
import cc.ryanc.halo.utils.DateUtils;
|
||||||
import cc.ryanc.halo.utils.MarkdownUtils;
|
import cc.ryanc.halo.utils.MarkdownUtils;
|
||||||
import cc.ryanc.halo.utils.ServiceUtils;
|
import cc.ryanc.halo.utils.ServiceUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -25,10 +26,12 @@ import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -175,6 +178,9 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
||||||
|
// Set edit time
|
||||||
|
postToUpdate.setEditTime(DateUtils.now());
|
||||||
|
|
||||||
return createOrUpdate(postToUpdate, tagIds, categoryIds, this::update);
|
return createOrUpdate(postToUpdate, tagIds, categoryIds, this::update);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,14 +217,10 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
|
|
||||||
log.debug("Created post categories: [{}]", postCategories);
|
log.debug("Created post categories: [{}]", postCategories);
|
||||||
|
|
||||||
// Build post detail vo
|
// Convert to post detail vo
|
||||||
PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post);
|
return convertTo(post,
|
||||||
|
() -> ServiceUtils.fetchProperty(postTags, PostTag::getTagId),
|
||||||
postDetailVO.setTagIds(ServiceUtils.fetchProperty(postTags, PostTag::getTagId));
|
() -> ServiceUtils.fetchProperty(postCategories, PostCategory::getCategoryId));
|
||||||
|
|
||||||
postDetailVO.setCategoryIds(ServiceUtils.fetchProperty(postCategories, PostCategory::getCategoryId));
|
|
||||||
|
|
||||||
return postDetailVO;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -235,6 +237,47 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PostDetailVO getDetailVoBy(Integer postId) {
|
public PostDetailVO getDetailVoBy(Integer postId) {
|
||||||
return null;
|
Assert.notNull(postId, "post id must not be null");
|
||||||
|
|
||||||
|
Post post = getById(postId);
|
||||||
|
|
||||||
|
// Convert to post detail vo
|
||||||
|
return convertTo(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert to post detail vo.
|
||||||
|
*
|
||||||
|
* @param post post must not be null
|
||||||
|
* @return post detail vo
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
private PostDetailVO convertTo(@NonNull Post post) {
|
||||||
|
return convertTo(post,
|
||||||
|
() -> postTagService.listTagIdsByPostId(post.getId()),
|
||||||
|
() -> postCategoryService.listCategoryIdsByPostId(post.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert to post detail vo.
|
||||||
|
*
|
||||||
|
* @param post post must not be null
|
||||||
|
* @param tagIdSetSupplier tag id set supplier
|
||||||
|
* @param categoryIdSetSupplier category id set supplier
|
||||||
|
* @return post detail vo
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
private PostDetailVO convertTo(@NonNull Post post, Supplier<Set<Integer>> tagIdSetSupplier, Supplier<Set<Integer>> categoryIdSetSupplier) {
|
||||||
|
Assert.notNull(post, "Post must not be null");
|
||||||
|
|
||||||
|
PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post);
|
||||||
|
|
||||||
|
// Get post tag ids
|
||||||
|
postDetailVO.setTagIds(tagIdSetSupplier == null ? Collections.emptySet() : tagIdSetSupplier.get());
|
||||||
|
|
||||||
|
// Get post category ids
|
||||||
|
postDetailVO.setCategoryIds(categoryIdSetSupplier == null ? Collections.emptySet() : categoryIdSetSupplier.get());
|
||||||
|
|
||||||
|
return postDetailVO;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,4 +149,25 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
|
||||||
// Return post tags
|
// Return post tags
|
||||||
return postTags;
|
return postTags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PostTag> listByPostId(Integer postId) {
|
||||||
|
Assert.notNull(postId, "Post id must not be null");
|
||||||
|
|
||||||
|
return postTagRepository.findAllByPostId(postId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PostTag> listByTagId(Integer tagId) {
|
||||||
|
Assert.notNull(tagId, "Tag id must not be null");
|
||||||
|
|
||||||
|
return postTagRepository.findAllByTagId(tagId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> listTagIdsByPostId(Integer postId) {
|
||||||
|
Assert.notNull(postId, "Post id must not be null");
|
||||||
|
|
||||||
|
return postTagRepository.findAllTagIdsByPostId(postId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue