Complete post getting api

pull/137/head
johnniang 2019-03-21 23:47:52 +08:00
parent af4c31b676
commit 7935ccd728
8 changed files with 159 additions and 16 deletions

View File

@ -46,11 +46,6 @@ public class PostSimpleOutputDTO extends PostMinimalOutputDTO {
*/
private Boolean disallowComment;
/**
*
*/
private String password;
/**
*
*/

View File

@ -134,6 +134,7 @@ public class Post extends BaseEntity {
public void prePersist() {
super.prePersist();
id = null;
editTime = getCreateTime();
}
}

View File

@ -5,7 +5,6 @@ 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;
@ -50,8 +49,17 @@ public interface PostCategoryRepository extends BaseRepository<PostCategory, Int
* Finds all post categories by post id.
*
* @param postId post id must not be null
* @return a list of post categories
* @return a list of post category
*/
@NonNull
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);
}

View File

@ -55,4 +55,31 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
*/
@NonNull
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);
}

View File

@ -66,4 +66,31 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
*/
@NonNull
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);
}

View File

@ -133,4 +133,25 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
// Create them
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);
}
}

View File

@ -14,6 +14,7 @@ import cc.ryanc.halo.model.vo.PostListVO;
import cc.ryanc.halo.repository.PostRepository;
import cc.ryanc.halo.service.*;
import cc.ryanc.halo.service.base.AbstractCrudService;
import cc.ryanc.halo.utils.DateUtils;
import cc.ryanc.halo.utils.MarkdownUtils;
import cc.ryanc.halo.utils.ServiceUtils;
import lombok.extern.slf4j.Slf4j;
@ -25,10 +26,12 @@ import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
@ -175,6 +178,9 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
@Override
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);
}
@ -211,14 +217,10 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
log.debug("Created post categories: [{}]", postCategories);
// Build post detail vo
PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post);
postDetailVO.setTagIds(ServiceUtils.fetchProperty(postTags, PostTag::getTagId));
postDetailVO.setCategoryIds(ServiceUtils.fetchProperty(postCategories, PostCategory::getCategoryId));
return postDetailVO;
// Convert to post detail vo
return convertTo(post,
() -> ServiceUtils.fetchProperty(postTags, PostTag::getTagId),
() -> ServiceUtils.fetchProperty(postCategories, PostCategory::getCategoryId));
}
/**
@ -235,6 +237,47 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
@Override
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;
}
}

View File

@ -149,4 +149,25 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
// Return post tags
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);
}
}