mirror of https://github.com/halo-dev/halo
Refactor post service and controller
parent
c3985cf9c5
commit
64397753be
|
@ -5,15 +5,15 @@ import org.springframework.data.domain.Page;
|
|||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import run.halo.app.model.dto.post.PostMinimalDTO;
|
||||
import run.halo.app.model.dto.post.PostSimpleDTO;
|
||||
import run.halo.app.model.dto.post.BasePostMinimalDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
import run.halo.app.model.entity.Post;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.params.PostParam;
|
||||
import run.halo.app.model.params.PostQuery;
|
||||
import run.halo.app.model.vo.PostDetailVO;
|
||||
import run.halo.app.model.vo.PostListVO;
|
||||
import run.halo.app.service.*;
|
||||
import run.halo.app.service.PostService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
@ -32,16 +32,8 @@ public class PostController {
|
|||
|
||||
private final PostService postService;
|
||||
|
||||
private final PostCategoryService postCategoryService;
|
||||
|
||||
private final PostTagService postTagService;
|
||||
|
||||
public PostController(PostService postService,
|
||||
PostCategoryService postCategoryService,
|
||||
PostTagService postTagService) {
|
||||
public PostController(PostService postService) {
|
||||
this.postService = postService;
|
||||
this.postCategoryService = postCategoryService;
|
||||
this.postTagService = postTagService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
|
@ -54,29 +46,33 @@ public class PostController {
|
|||
|
||||
@GetMapping("latest")
|
||||
@ApiOperation("Pages latest post")
|
||||
public List<PostMinimalDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
|
||||
return postService.pageLatestOfMinimal(top).getContent();
|
||||
public List<BasePostMinimalDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
|
||||
return postService.convertToMinimal(postService.pageLatest(top).getContent());
|
||||
}
|
||||
|
||||
@GetMapping("status/{status}")
|
||||
@ApiOperation("Gets a page of post by post status")
|
||||
public Page<? extends PostSimpleDTO> pageByStatus(@PathVariable(name = "status") PostStatus status,
|
||||
@RequestParam(value = "more", required = false, defaultValue = "false") Boolean more,
|
||||
@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
||||
public Page<? extends BasePostSimpleDTO> pageByStatus(@PathVariable(name = "status") PostStatus status,
|
||||
@RequestParam(value = "more", required = false, defaultValue = "false") Boolean more,
|
||||
@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
||||
Page<Post> posts = postService.pageBy(status, pageable);
|
||||
|
||||
if (more) {
|
||||
return postService.pageListVoBy(status, pageable);
|
||||
return postService.convertToListVo(posts);
|
||||
}
|
||||
return postService.pageSimpleDtoByStatus(status, pageable);
|
||||
|
||||
return postService.convertToSimple(posts);
|
||||
}
|
||||
|
||||
@GetMapping("{postId:\\d+}")
|
||||
public PostDetailVO getBy(@PathVariable("postId") Integer postId) {
|
||||
return postService.getDetailVoBy(postId);
|
||||
Post post = postService.getById(postId);
|
||||
return postService.convertToDetailVo(post);
|
||||
}
|
||||
|
||||
@PostMapping("{postId:\\d+}/likes")
|
||||
@PutMapping("{postId:\\d+}/likes")
|
||||
@ApiOperation("Likes a post")
|
||||
public void like(@PathVariable("postId") Integer postId) {
|
||||
public void likes(@PathVariable("postId") Integer postId) {
|
||||
postService.increaseLike(postId);
|
||||
}
|
||||
|
||||
|
@ -99,7 +95,7 @@ public class PostController {
|
|||
return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds());
|
||||
}
|
||||
|
||||
@PutMapping("{postId:\\d+}/{status}")
|
||||
@PutMapping("{postId:\\d+}/status/{status}")
|
||||
public void updateStatusBy(
|
||||
@PathVariable("postId") Integer postId,
|
||||
@PathVariable("status") PostStatus status) {
|
||||
|
@ -116,8 +112,6 @@ public class PostController {
|
|||
public void deletePermanently(@PathVariable("postId") Integer postId) {
|
||||
// Remove it
|
||||
postService.removeById(postId);
|
||||
postCategoryService.removeByPostId(postId);
|
||||
postTagService.removeByPostId(postId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.data.domain.Pageable;
|
|||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import run.halo.app.model.dto.post.SheetDetailDTO;
|
||||
import run.halo.app.model.dto.post.SheetListDTO;
|
||||
import run.halo.app.model.vo.SheetListVO;
|
||||
import run.halo.app.model.entity.Sheet;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.params.SheetParam;
|
||||
|
@ -41,7 +41,7 @@ public class SheetController {
|
|||
|
||||
@GetMapping
|
||||
@ApiOperation("Gets a page of sheet")
|
||||
public Page<SheetListDTO> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
||||
public Page<SheetListVO> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
||||
Page<Sheet> sheetPage = sheetService.pageBy(pageable);
|
||||
return sheetService.convertToListDto(sheetPage);
|
||||
}
|
||||
|
|
|
@ -85,12 +85,14 @@ public class ContentArchiveController {
|
|||
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
|
||||
Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort);
|
||||
|
||||
Page<PostListVO> posts = postService.pageListVoBy(PostStatus.PUBLISHED, pageable);
|
||||
int[] pageRainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
|
||||
Page<Post> postPage = postService.pageBy(PostStatus.PUBLISHED, pageable);
|
||||
Page<PostListVO> postListVos = postService.convertToListVo(postPage);
|
||||
int[] pageRainbow = PageUtil.rainbow(page, postListVos.getTotalPages(), 3);
|
||||
|
||||
model.addAttribute("is_archives", true);
|
||||
model.addAttribute("pageRainbow", pageRainbow);
|
||||
model.addAttribute("posts", posts);
|
||||
model.addAttribute("posts", postListVos);
|
||||
|
||||
return themeService.render("archives");
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.springframework.ui.Model;
|
|||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import run.halo.app.model.entity.Post;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.vo.PostListVO;
|
||||
import run.halo.app.service.OptionService;
|
||||
|
@ -75,7 +76,9 @@ public class ContentIndexController {
|
|||
int pageSize = optionService.getPostPageSize();
|
||||
Pageable pageable = PageRequest.of(page >= 1 ? page - 1 : page, pageSize, sort);
|
||||
|
||||
Page<PostListVO> posts = postService.pageListVoBy(PostStatus.PUBLISHED, pageable);
|
||||
Page<Post> postPage = postService.pageBy(PostStatus.PUBLISHED, pageable);
|
||||
Page<PostListVO> posts = postService.convertToListVo(postPage);
|
||||
|
||||
int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
|
||||
|
||||
model.addAttribute("is_index", true);
|
||||
|
|
|
@ -8,13 +8,14 @@ import org.springframework.data.domain.Sort;
|
|||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.data.web.SortDefault;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import run.halo.app.model.dto.post.PostDetailDTO;
|
||||
import run.halo.app.model.dto.post.PostSimpleDTO;
|
||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
import run.halo.app.model.entity.Post;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.vo.BaseCommentVO;
|
||||
import run.halo.app.model.vo.BaseCommentWithParentVO;
|
||||
import run.halo.app.service.PostCommentService;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.service.PostCommentService;
|
||||
import run.halo.app.service.PostService;
|
||||
|
||||
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||
|
@ -45,28 +46,29 @@ public class PostController {
|
|||
|
||||
@GetMapping
|
||||
@ApiOperation("Lists posts")
|
||||
public Page<PostSimpleDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
|
||||
return postService.pageSimpleDtoByStatus(PostStatus.PUBLISHED, pageable);
|
||||
public Page<BasePostSimpleDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
|
||||
Page<Post> postPage = postService.pageBy(PostStatus.PUBLISHED, pageable);
|
||||
return postService.convertToSimple(postPage);
|
||||
}
|
||||
|
||||
@GetMapping("{postId:\\d+}")
|
||||
@ApiOperation("Gets a post")
|
||||
public PostDetailDTO getBy(@PathVariable("postId") Integer postId,
|
||||
@RequestParam(value = "formatDisabled", required = false, defaultValue = "true") Boolean formatDisabled,
|
||||
@RequestParam(value = "sourceDisabled", required = false, defaultValue = "false") Boolean sourceDisabled) {
|
||||
PostDetailDTO postDetail = new PostDetailDTO().convertFrom(postService.getById(postId));
|
||||
public BasePostDetailDTO 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));
|
||||
|
||||
if (formatDisabled) {
|
||||
// Clear the format content
|
||||
postDetail.setFormatContent(null);
|
||||
detailDTO.setFormatContent(null);
|
||||
}
|
||||
|
||||
if (sourceDisabled) {
|
||||
// Clear the original content
|
||||
postDetail.setOriginalContent(null);
|
||||
detailDTO.setOriginalContent(null);
|
||||
}
|
||||
|
||||
return postDetail;
|
||||
return detailDTO;
|
||||
}
|
||||
|
||||
@GetMapping("{postId:\\d+}/comments/tree_view")
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package run.halo.app.model.dto.post;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Base post detail output dto.
|
||||
*
|
||||
* @author johnniang
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class BasePostDetailDTO extends BasePostSimpleDTO {
|
||||
|
||||
private String originalContent;
|
||||
|
||||
private String formatContent;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package run.halo.app.model.dto.post;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import run.halo.app.model.dto.base.OutputConverter;
|
||||
import run.halo.app.model.entity.BasePost;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.enums.PostType;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Base post minimal output dto.
|
||||
*
|
||||
* @author johnniang
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
public class BasePostMinimalDTO implements OutputConverter<BasePostMinimalDTO, BasePost> {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String title;
|
||||
|
||||
private PostStatus status;
|
||||
|
||||
private String url;
|
||||
|
||||
private PostType type;
|
||||
|
||||
private Date updateTime;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private Date editTime;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package run.halo.app.model.dto.post;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import run.halo.app.model.enums.PostCreateFrom;
|
||||
import run.halo.app.model.enums.PostType;
|
||||
|
||||
/**
|
||||
* Base page simple output dto.
|
||||
*
|
||||
* @author johnniang
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class BasePostSimpleDTO extends BasePostMinimalDTO {
|
||||
|
||||
private PostType type;
|
||||
|
||||
private String summary;
|
||||
|
||||
private String thumbnail;
|
||||
|
||||
private Long visits = 0L;
|
||||
|
||||
private Boolean disallowComment;
|
||||
|
||||
private String template;
|
||||
|
||||
private Integer topPriority = 0;
|
||||
|
||||
private PostCreateFrom createFrom;
|
||||
|
||||
private Long likes = 0L;
|
||||
}
|
|
@ -12,6 +12,7 @@ import lombok.ToString;
|
|||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Deprecated
|
||||
public class PostDetailDTO extends PostSimpleDTO {
|
||||
|
||||
private String originalContent;
|
||||
|
|
|
@ -18,6 +18,7 @@ import java.util.Date;
|
|||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@Deprecated
|
||||
public class PostMinimalDTO implements OutputConverter<PostMinimalDTO, BasePost> {
|
||||
|
||||
private Integer id;
|
||||
|
|
|
@ -14,6 +14,7 @@ import run.halo.app.model.enums.PostType;
|
|||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Deprecated
|
||||
public class PostSimpleDTO extends PostMinimalDTO {
|
||||
|
||||
private PostType type;
|
||||
|
|
|
@ -6,6 +6,7 @@ package run.halo.app.model.dto.post;
|
|||
* @author johnniang
|
||||
* @date 19-4-24
|
||||
*/
|
||||
@Deprecated
|
||||
public class SheetDetailDTO extends PostDetailDTO {
|
||||
|
||||
}
|
||||
|
|
|
@ -6,5 +6,6 @@ package run.halo.app.model.dto.post;
|
|||
* @author johnniang
|
||||
* @date 19-4-24
|
||||
*/
|
||||
@Deprecated
|
||||
public class SheetMinimalDTO extends PostMinimalDTO {
|
||||
}
|
||||
|
|
|
@ -6,5 +6,6 @@ package run.halo.app.model.dto.post;
|
|||
* @author johnniang
|
||||
* @date 19-4-24
|
||||
*/
|
||||
@Deprecated
|
||||
public class SheetSimpleDTO extends PostSimpleDTO {
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package run.halo.app.model.vo;
|
||||
|
||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.model.dto.post.PostDetailDTO;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
@ -14,7 +15,7 @@ import java.util.Set;
|
|||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class PostDetailVO extends PostDetailDTO {
|
||||
public class PostDetailVO extends BasePostDetailDTO {
|
||||
|
||||
private Set<Integer> tagIds;
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package run.halo.app.model.vo;
|
||||
|
||||
import run.halo.app.model.dto.CategoryDTO;
|
||||
import run.halo.app.model.dto.TagDTO;
|
||||
import run.halo.app.model.dto.post.PostSimpleDTO;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import run.halo.app.model.dto.CategoryDTO;
|
||||
import run.halo.app.model.dto.TagDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -16,7 +16,7 @@ import java.util.List;
|
|||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class PostListVO extends PostSimpleDTO {
|
||||
public class PostListVO extends BasePostSimpleDTO {
|
||||
|
||||
private Long commentCount;
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package run.halo.app.model.dto.post;
|
||||
package run.halo.app.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
|
||||
/**
|
||||
* Sheet list dto.
|
||||
|
@ -13,7 +14,7 @@ import lombok.ToString;
|
|||
@Data
|
||||
@ToString(callSuper = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SheetListDTO extends SheetSimpleDTO {
|
||||
public class SheetListVO extends BasePostSimpleDTO {
|
||||
|
||||
private Long commentCount;
|
||||
}
|
|
@ -26,25 +26,6 @@ import java.util.Set;
|
|||
*/
|
||||
public interface PostService extends BasePostService<Post> {
|
||||
|
||||
/**
|
||||
* Lists latest posts of minimal.
|
||||
*
|
||||
* @param top top number must not be less than 0
|
||||
* @return latest posts of minimal
|
||||
*/
|
||||
@NonNull
|
||||
Page<PostMinimalDTO> pageLatestOfMinimal(int top);
|
||||
|
||||
|
||||
/**
|
||||
* Lists latest posts of simple .
|
||||
*
|
||||
* @param top top number must not be less than 0
|
||||
* @return latest posts of simple
|
||||
*/
|
||||
@NonNull
|
||||
Page<PostSimpleDTO> pageLatestOfSimple(int top);
|
||||
|
||||
/**
|
||||
* Pages posts.
|
||||
*
|
||||
|
@ -65,26 +46,6 @@ public interface PostService extends BasePostService<Post> {
|
|||
@NonNull
|
||||
Page<Post> pageBy(@NonNull String keyword, @NonNull Pageable pageable);
|
||||
|
||||
/**
|
||||
* Lists simple output dto by status.
|
||||
*
|
||||
* @param status post status must not be null
|
||||
* @param pageable page info must not be null
|
||||
* @return Page<PostSimpleDTO>
|
||||
*/
|
||||
@NonNull
|
||||
Page<PostSimpleDTO> pageSimpleDtoByStatus(@NonNull PostStatus status, @NonNull Pageable pageable);
|
||||
|
||||
/**
|
||||
* Lists page list vo by status and pageable.
|
||||
*
|
||||
* @param status post status must not be null
|
||||
* @param pageable page info must not be null
|
||||
* @return a page of page list vo
|
||||
*/
|
||||
@NonNull
|
||||
Page<PostListVO> pageListVoBy(@NonNull PostStatus status, @NonNull Pageable pageable);
|
||||
|
||||
/**
|
||||
* Creates post by post param.
|
||||
*
|
||||
|
@ -120,15 +81,6 @@ public interface PostService extends BasePostService<Post> {
|
|||
@Override
|
||||
Post getBy(@NonNull PostStatus status, @NonNull String url);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Lists year archives.
|
||||
*
|
||||
|
@ -146,13 +98,13 @@ public interface PostService extends BasePostService<Post> {
|
|||
List<ArchiveMonthVO> listMonthArchives();
|
||||
|
||||
/**
|
||||
* Converts to a page of post simple output dto.
|
||||
* Converts to detail vo.
|
||||
*
|
||||
* @param postPage post page must not be null
|
||||
* @return a page of post simple output dto
|
||||
* @param post post must not be null
|
||||
* @return post detail vo
|
||||
*/
|
||||
@NonNull
|
||||
Page<PostSimpleDTO> convertToSimpleDto(@NonNull Page<Post> postPage);
|
||||
PostDetailVO convertToDetailVo(@NonNull Post post);
|
||||
|
||||
/**
|
||||
* Converts to a page of post list vo.
|
||||
|
|
|
@ -3,7 +3,7 @@ package run.halo.app.service;
|
|||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.lang.NonNull;
|
||||
import run.halo.app.model.dto.post.SheetDetailDTO;
|
||||
import run.halo.app.model.dto.post.SheetListDTO;
|
||||
import run.halo.app.model.vo.SheetListVO;
|
||||
import run.halo.app.model.entity.Sheet;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.service.base.BasePostService;
|
||||
|
@ -54,7 +54,7 @@ public interface SheetService extends BasePostService<Sheet> {
|
|||
* @return a page of sheet list dto
|
||||
*/
|
||||
@NonNull
|
||||
Page<SheetListDTO> convertToListDto(@NonNull Page<Sheet> sheetPage);
|
||||
Page<SheetListVO> convertToListDto(@NonNull Page<Sheet> sheetPage);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -3,7 +3,11 @@ package run.halo.app.service.base;
|
|||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.model.dto.post.BasePostMinimalDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
import run.halo.app.model.entity.BasePost;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
|
||||
|
@ -188,4 +192,24 @@ public interface BasePostService<POST extends BasePost> extends CrudService<POST
|
|||
@NonNull
|
||||
POST filterIfEncrypt(@NonNull POST post);
|
||||
|
||||
@NonNull
|
||||
BasePostMinimalDTO convertToMinimal(@NonNull POST post);
|
||||
|
||||
@NonNull
|
||||
List<BasePostMinimalDTO> convertToMinimal(@Nullable List<POST> posts);
|
||||
|
||||
@NonNull
|
||||
Page<BasePostMinimalDTO> convertToMinimal(@NonNull Page<POST> postPage);
|
||||
|
||||
@NonNull
|
||||
BasePostSimpleDTO convertToSimple(@NonNull POST post);
|
||||
|
||||
@NonNull
|
||||
List<BasePostSimpleDTO> convertToSimple(@Nullable List<POST> posts);
|
||||
|
||||
@NonNull
|
||||
Page<BasePostSimpleDTO> convertToSimple(@NonNull Page<POST> postPage);
|
||||
|
||||
@NonNull
|
||||
BasePostDetailDTO convertToDetail(@NonNull POST post);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,9 @@ import org.springframework.util.CollectionUtils;
|
|||
import run.halo.app.exception.AlreadyExistsException;
|
||||
import run.halo.app.exception.BadRequestException;
|
||||
import run.halo.app.exception.NotFoundException;
|
||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.model.dto.post.BasePostMinimalDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
import run.halo.app.model.entity.BasePost;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.repository.base.BasePostRepository;
|
||||
|
@ -21,9 +24,11 @@ import run.halo.app.utils.DateUtils;
|
|||
import run.halo.app.utils.MarkdownUtils;
|
||||
import run.halo.app.utils.ServiceUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.springframework.data.domain.Sort.Direction.ASC;
|
||||
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||
|
@ -219,6 +224,63 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
|
|||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BasePostMinimalDTO convertToMinimal(POST post) {
|
||||
Assert.notNull(post, "Post must not be null");
|
||||
|
||||
return new BasePostMinimalDTO().convertFrom(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BasePostMinimalDTO> convertToMinimal(List<POST> posts) {
|
||||
if (CollectionUtils.isEmpty(posts)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return posts.stream()
|
||||
.map(this::convertToMinimal)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<BasePostMinimalDTO> convertToMinimal(Page<POST> postPage) {
|
||||
Assert.notNull(postPage, "Post page must not be null");
|
||||
|
||||
return postPage.map(this::convertToMinimal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BasePostSimpleDTO convertToSimple(POST post) {
|
||||
Assert.notNull(post, "Post must not be null");
|
||||
|
||||
return new BasePostSimpleDTO().convertFrom(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BasePostSimpleDTO> convertToSimple(List<POST> posts) {
|
||||
if (CollectionUtils.isEmpty(posts)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return posts.stream()
|
||||
.map(this::convertToSimple)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<BasePostSimpleDTO> convertToSimple(Page<POST> postPage) {
|
||||
Assert.notNull(postPage, "Post page must not be null");
|
||||
|
||||
return postPage.map(this::convertToSimple);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BasePostDetailDTO convertToDetail(POST post) {
|
||||
Assert.notNull(post, "Post must not be null");
|
||||
|
||||
return new BasePostDetailDTO().convertFrom(post);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the url is exist.
|
||||
*
|
||||
|
|
|
@ -81,16 +81,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PostMinimalDTO> pageLatestOfMinimal(int top) {
|
||||
return pageLatest(top).map(post -> new PostMinimalDTO().convertFrom(post));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PostSimpleDTO> pageLatestOfSimple(int top) {
|
||||
return pageLatest(top).map(post -> new PostSimpleDTO().convertFrom(post));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Post> pageBy(PostQuery postQuery, Pageable pageable) {
|
||||
Assert.notNull(postQuery, "Post query must not be null");
|
||||
|
@ -107,6 +97,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
|
||||
PostQuery postQuery = new PostQuery();
|
||||
postQuery.setKeyword(keyword);
|
||||
postQuery.setStatus(PostStatus.PUBLISHED);
|
||||
|
||||
// Build specification and find all
|
||||
return postRepository.findAll(buildSpecByQuery(postQuery), pageable);
|
||||
|
@ -154,25 +145,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* List by status.
|
||||
*
|
||||
* @param status status
|
||||
* @param pageable pageable
|
||||
* @return Page<PostSimpleDTO>
|
||||
*/
|
||||
@Override
|
||||
public Page<PostSimpleDTO> pageSimpleDtoByStatus(PostStatus status, Pageable pageable) {
|
||||
return pageBy(status, pageable).map(post -> new PostSimpleDTO().convertFrom(post));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PostListVO> pageListVoBy(PostStatus status, Pageable pageable) {
|
||||
Page<Post> postPage = pageBy(status, pageable);
|
||||
|
||||
return convertToListVo(postPage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
||||
return createOrUpdate(postToCreate, tagIds, categoryIds);
|
||||
|
@ -248,16 +220,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostDetailVO getDetailVoBy(Integer postId) {
|
||||
Assert.notNull(postId, "post id must not be null");
|
||||
|
||||
Post post = getById(postId);
|
||||
|
||||
// Convert to post detail vo
|
||||
return convertTo(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ArchiveYearVO> listYearArchives() {
|
||||
// Get all posts
|
||||
|
@ -323,6 +285,13 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
return archives;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostDetailVO convertToDetailVo(Post post) {
|
||||
return convertTo(post,
|
||||
() -> postTagService.listTagIdsByPostId(post.getId()),
|
||||
() -> postCategoryService.listCategoryIdsByPostId(post.getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post removeById(Integer postId) {
|
||||
Assert.notNull(postId, "Post id must not be null");
|
||||
|
@ -347,13 +316,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
return deletedPost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PostSimpleDTO> convertToSimpleDto(@NonNull Page<Post> postPage) {
|
||||
Assert.notNull(postPage, "Post page must not be null");
|
||||
|
||||
return postPage.map(post -> new PostSimpleDTO().convertFrom(post));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PostListVO> convertToListVo(Page<Post> postPage) {
|
||||
Assert.notNull(postPage, "Post page must not be null");
|
||||
|
@ -418,19 +380,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts 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()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts to post detail vo.
|
||||
*
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.util.Assert;
|
||||
import run.halo.app.event.post.SheetVisitEvent;
|
||||
import run.halo.app.model.dto.post.SheetDetailDTO;
|
||||
import run.halo.app.model.dto.post.SheetListDTO;
|
||||
import run.halo.app.model.vo.SheetListVO;
|
||||
import run.halo.app.model.entity.Sheet;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.repository.SheetRepository;
|
||||
|
@ -88,7 +88,7 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
}
|
||||
|
||||
@Override
|
||||
public Page<SheetListDTO> convertToListDto(Page<Sheet> sheetPage) {
|
||||
public Page<SheetListVO> convertToListDto(Page<Sheet> sheetPage) {
|
||||
Assert.notNull(sheetPage, "Sheet page must not be null");
|
||||
|
||||
// Get all sheet id
|
||||
|
@ -100,9 +100,9 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
Map<Integer, Long> sheetCommentCountMap = sheetCommentService.countByPostIds(sheetIds);
|
||||
|
||||
return sheetPage.map(sheet -> {
|
||||
SheetListDTO sheetListDTO = new SheetListDTO().convertFrom(sheet);
|
||||
sheetListDTO.setCommentCount(sheetCommentCountMap.getOrDefault(sheet.getId(), 0L));
|
||||
return sheetListDTO;
|
||||
SheetListVO sheetListVO = new SheetListVO().convertFrom(sheet);
|
||||
sheetListVO.setCommentCount(sheetCommentCountMap.getOrDefault(sheet.getId(), 0L));
|
||||
return sheetListVO;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue