mirror of https://github.com/halo-dev/halo
Refactor post creation and update api
parent
7853bd679c
commit
af4c31b676
|
@ -0,0 +1,22 @@
|
||||||
|
package cc.ryanc.halo.model.vo;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.dto.post.PostDetailOutputDTO;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post vo.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
* @date 3/21/19
|
||||||
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
public class PostDetailVO extends PostDetailOutputDTO {
|
||||||
|
|
||||||
|
private Set<Integer> tagIds;
|
||||||
|
|
||||||
|
private Set<Integer> categoryIds;
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import cc.ryanc.halo.model.dto.CategoryOutputDTO;
|
||||||
import cc.ryanc.halo.model.dto.TagOutputDTO;
|
import cc.ryanc.halo.model.dto.TagOutputDTO;
|
||||||
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ import java.util.List;
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @date 3/19/19
|
* @date 3/19/19
|
||||||
*/
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
public class PostListVO extends PostSimpleOutputDTO {
|
public class PostListVO extends PostSimpleOutputDTO {
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import cc.ryanc.halo.model.entity.Post;
|
||||||
import cc.ryanc.halo.model.entity.Tag;
|
import cc.ryanc.halo.model.entity.Tag;
|
||||||
import cc.ryanc.halo.model.enums.PostStatus;
|
import cc.ryanc.halo.model.enums.PostStatus;
|
||||||
import cc.ryanc.halo.model.enums.PostType;
|
import cc.ryanc.halo.model.enums.PostType;
|
||||||
|
import cc.ryanc.halo.model.vo.PostDetailVO;
|
||||||
import cc.ryanc.halo.model.vo.PostListVO;
|
import cc.ryanc.halo.model.vo.PostListVO;
|
||||||
import cc.ryanc.halo.service.base.CrudService;
|
import cc.ryanc.halo.service.base.CrudService;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
@ -124,7 +125,7 @@ public interface PostService extends CrudService<Post, Integer> {
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
@Transactional
|
@Transactional
|
||||||
Post createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds);
|
PostDetailVO createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates post by post, tag id set and category id set.
|
* Updates post by post, tag id set and category id set.
|
||||||
|
@ -136,7 +137,7 @@ public interface PostService extends CrudService<Post, Integer> {
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
@Transactional
|
@Transactional
|
||||||
Post updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds);
|
PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get post by url.
|
* Get post by url.
|
||||||
|
@ -146,4 +147,13 @@ public interface PostService extends CrudService<Post, Integer> {
|
||||||
* @return Post
|
* @return Post
|
||||||
*/
|
*/
|
||||||
Post getByUrl(@NonNull String url, @NonNull PostType type);
|
Post getByUrl(@NonNull String url, @NonNull PostType type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get post detail vo by post id.
|
||||||
|
*
|
||||||
|
* @param postId post id must not be null
|
||||||
|
* @return post detail vo
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
PostDetailVO getDetailVoBy(@NonNull Integer postId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,15 +23,6 @@ public interface TagService extends CrudService<Tag, Integer> {
|
||||||
*/
|
*/
|
||||||
void remove(Integer id);
|
void remove(Integer id);
|
||||||
|
|
||||||
/**
|
|
||||||
* Lists all tag dtos.
|
|
||||||
*
|
|
||||||
* @param sort sort info must not be null
|
|
||||||
* @return a list of tag dto
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
List<TagOutputDTO> listDtos(@NonNull Sort sort);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get tag by slug name
|
* Get tag by slug name
|
||||||
*
|
*
|
||||||
|
@ -39,4 +30,13 @@ public interface TagService extends CrudService<Tag, Integer> {
|
||||||
* @return Tag
|
* @return Tag
|
||||||
*/
|
*/
|
||||||
Tag getBySlugName(@NonNull String slugName);
|
Tag getBySlugName(@NonNull String slugName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts to tag output dtos.
|
||||||
|
*
|
||||||
|
* @param tags tag list
|
||||||
|
* @return a list of tag output dto
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
List<TagOutputDTO> convertTo(List<Tag> tags);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.*;
|
import cc.ryanc.halo.model.entity.*;
|
||||||
import cc.ryanc.halo.model.enums.PostStatus;
|
import cc.ryanc.halo.model.enums.PostStatus;
|
||||||
import cc.ryanc.halo.model.enums.PostType;
|
import cc.ryanc.halo.model.enums.PostType;
|
||||||
|
import cc.ryanc.halo.model.vo.PostDetailVO;
|
||||||
import cc.ryanc.halo.model.vo.PostListVO;
|
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.*;
|
||||||
|
@ -168,16 +169,16 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Post createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
||||||
return createOrUpdate(postToCreate, tagIds, categoryIds, this::create);
|
return createOrUpdate(postToCreate, tagIds, categoryIds, this::create);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Post updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
||||||
return createOrUpdate(postToUpdate, tagIds, categoryIds, this::update);
|
return createOrUpdate(postToUpdate, tagIds, categoryIds, this::update);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Post createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, @NonNull Function<Post, Post> postOperation) {
|
private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, @NonNull Function<Post, Post> postOperation) {
|
||||||
Assert.notNull(post, "Post param must not be null");
|
Assert.notNull(post, "Post param must not be null");
|
||||||
Assert.notNull(postOperation, "Post operation must not be null");
|
Assert.notNull(postOperation, "Post operation must not be null");
|
||||||
|
|
||||||
|
@ -210,7 +211,14 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
|
|
||||||
log.debug("Created post categories: [{}]", postCategories);
|
log.debug("Created post categories: [{}]", postCategories);
|
||||||
|
|
||||||
return post;
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -224,4 +232,9 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
public Post getByUrl(String url, PostType type) {
|
public Post getByUrl(String url, PostType type) {
|
||||||
return postRepository.getByUrlAndType(url, type).orElseThrow(() -> new NotFoundException("The post does not exist").setErrorData(url));
|
return postRepository.getByUrlAndType(url, type).orElseThrow(() -> new NotFoundException("The post does not exist").setErrorData(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PostDetailVO getDetailVoBy(Integer postId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,10 @@ import cc.ryanc.halo.repository.TagRepository;
|
||||||
import cc.ryanc.halo.service.TagService;
|
import cc.ryanc.halo.service.TagService;
|
||||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.domain.Sort;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -42,13 +42,6 @@ public class TagServiceImpl extends AbstractCrudService<Tag, Integer> implements
|
||||||
// TODO 删除标签,以及对应的文章关系
|
// TODO 删除标签,以及对应的文章关系
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<TagOutputDTO> listDtos(Sort sort) {
|
|
||||||
Assert.notNull(sort, "Sort info must not be null");
|
|
||||||
|
|
||||||
return listAll(sort).stream().map(tag -> (TagOutputDTO) new TagOutputDTO().convertFrom(tag)).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Tag create(Tag tag) {
|
public Tag create(Tag tag) {
|
||||||
// Check if the tag is exist
|
// Check if the tag is exist
|
||||||
|
@ -75,4 +68,11 @@ public class TagServiceImpl extends AbstractCrudService<Tag, Integer> implements
|
||||||
public Tag getBySlugName(String slugName) {
|
public Tag getBySlugName(String slugName) {
|
||||||
return tagRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The tag does not exist").setErrorData(slugName));
|
return tagRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The tag does not exist").setErrorData(slugName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TagOutputDTO> convertTo(List<Tag> tags) {
|
||||||
|
return CollectionUtils.isEmpty(tags) ?
|
||||||
|
Collections.emptyList() :
|
||||||
|
tags.stream().map(tag -> (TagOutputDTO) new TagOutputDTO().convertFrom(tag)).collect(Collectors.toList());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package cc.ryanc.halo.web.controller.admin.api;
|
package cc.ryanc.halo.web.controller.admin.api;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.dto.post.PostDetailOutputDTO;
|
|
||||||
import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO;
|
import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO;
|
||||||
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
|
||||||
import cc.ryanc.halo.model.entity.Post;
|
import cc.ryanc.halo.model.entity.Post;
|
||||||
import cc.ryanc.halo.model.enums.PostStatus;
|
import cc.ryanc.halo.model.enums.PostStatus;
|
||||||
import cc.ryanc.halo.model.enums.PostType;
|
import cc.ryanc.halo.model.enums.PostType;
|
||||||
import cc.ryanc.halo.model.params.PostParam;
|
import cc.ryanc.halo.model.params.PostParam;
|
||||||
|
import cc.ryanc.halo.model.vo.PostDetailVO;
|
||||||
import cc.ryanc.halo.service.PostService;
|
import cc.ryanc.halo.service.PostService;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
@ -48,27 +48,28 @@ public class PostController {
|
||||||
return postService.pageSimpleDtoByStatus(status, PostType.POST, pageable);
|
return postService.pageSimpleDtoByStatus(status, PostType.POST, pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("{postId:\\d+}")
|
||||||
|
public PostDetailVO getBy(@PathVariable("postId") Integer postId) {
|
||||||
|
return postService.getDetailVoBy(postId);
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public PostDetailOutputDTO createBy(@Valid @RequestBody PostParam postParam) {
|
public PostDetailVO createBy(@Valid @RequestBody PostParam postParam) {
|
||||||
// Convert to
|
// Convert to
|
||||||
Post post = postParam.convertTo();
|
Post post = postParam.convertTo();
|
||||||
|
|
||||||
Post createdPost = postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds());
|
return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds());
|
||||||
|
|
||||||
return new PostDetailOutputDTO().convertFrom(createdPost);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("{postId:\\d+}")
|
@PutMapping("{postId:\\d+}")
|
||||||
public PostDetailOutputDTO updateBy(@Valid @RequestBody PostParam postParam,
|
public PostDetailVO updateBy(@Valid @RequestBody PostParam postParam,
|
||||||
@PathVariable("postId") Integer postId) {
|
@PathVariable("postId") Integer postId) {
|
||||||
// Get the post info
|
// Get the post info
|
||||||
Post postToUpdate = postService.getById(postId);
|
Post postToUpdate = postService.getById(postId);
|
||||||
|
|
||||||
postParam.update(postToUpdate);
|
postParam.update(postToUpdate);
|
||||||
|
|
||||||
Post updatedPost = postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds());
|
return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds());
|
||||||
|
|
||||||
return new PostDetailOutputDTO().convertFrom(updatedPost);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class TagController {
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<TagOutputDTO> listTags(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort) {
|
public List<TagOutputDTO> listTags(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort) {
|
||||||
return tagService.listDtos(sort);
|
return tagService.convertTo(tagService.listAll(sort));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
|
|
Loading…
Reference in New Issue