mirror of https://github.com/halo-dev/halo
Optimize content archive controller
parent
135586b4ec
commit
384286782f
|
@ -10,6 +10,7 @@ import run.halo.app.model.enums.PostStatus;
|
||||||
import run.halo.app.repository.base.BasePostRepository;
|
import run.halo.app.repository.base.BasePostRepository;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,8 +28,11 @@ public interface PostRepository extends BasePostRepository<Post>, JpaSpecificati
|
||||||
Long countLike();
|
Long countLike();
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
Page<Post> findAllByStatusAndCreateTimeBefore(PostStatus status, Date createTime, @NonNull Pageable pageable);
|
Page<Post> findAllByStatusAndCreateTimeBefore(@NonNull PostStatus status, @NonNull Date createTime, @NonNull Pageable pageable);
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
Page<Post> findAllByStatusAndCreateTimeAfter(PostStatus status, Date createTime, @NonNull Pageable pageable);
|
Page<Post> findAllByStatusAndCreateTimeAfter(@NonNull PostStatus status, @NonNull Date createTime, @NonNull Pageable pageable);
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
Optional<Post> getByUrlAndStatus(@NonNull String url, @NonNull PostStatus status);
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,6 +139,16 @@ public interface PostService extends CrudService<Post, Integer> {
|
||||||
@NonNull
|
@NonNull
|
||||||
Post getByUrl(@NonNull String url);
|
Post getByUrl(@NonNull String url);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets post by post status and url.
|
||||||
|
*
|
||||||
|
* @param status post status must not be null
|
||||||
|
* @param url post url must not be blank
|
||||||
|
* @return post info
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Post getBy(@NonNull PostStatus status, @NonNull String url);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get post detail vo by post id.
|
* Get post detail vo by post id.
|
||||||
*
|
*
|
||||||
|
@ -196,22 +206,6 @@ public interface PostService extends CrudService<Post, Integer> {
|
||||||
@NonNull
|
@NonNull
|
||||||
Page<PostListVO> convertToListVo(@NonNull Page<Post> postPage);
|
Page<PostListVO> convertToListVo(@NonNull Page<Post> postPage);
|
||||||
|
|
||||||
/**
|
|
||||||
* Get pre post by current post date.
|
|
||||||
* @param date date
|
|
||||||
* @return post
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
Post getPrePostOfNullable(@NonNull Date date);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get next post by current post date.
|
|
||||||
* @param date date
|
|
||||||
* @return post
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
Post getNextPostOfNullable(@NonNull Date date);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists all posts by post status.
|
* Lists all posts by post status.
|
||||||
*
|
*
|
||||||
|
|
|
@ -258,6 +258,16 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
return postRepository.getByUrl(url).orElseThrow(() -> new NotFoundException("The post does not exist").setErrorData(url));
|
return postRepository.getByUrl(url).orElseThrow(() -> new NotFoundException("The post does not exist").setErrorData(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Post getBy(PostStatus status, String url) {
|
||||||
|
Assert.notNull(status, "Post status must not be null");
|
||||||
|
Assert.hasText(url, "Post url must not be blank");
|
||||||
|
|
||||||
|
Optional<Post> postOptional = postRepository.getByUrlAndStatus(url, status);
|
||||||
|
|
||||||
|
return postOptional.orElseThrow(() -> new NotFoundException("The post with status " + status + " and url " + url + "was not existed").setErrorData(url));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PostDetailVO getDetailVoBy(Integer postId) {
|
public PostDetailVO getDetailVoBy(Integer postId) {
|
||||||
Assert.notNull(postId, "post id must not be null");
|
Assert.notNull(postId, "post id must not be null");
|
||||||
|
@ -363,16 +373,6 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
return super.removeById(postId);
|
return super.removeById(postId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Post getPrePostOfNullable(Date date) {
|
|
||||||
return getPrePost(date).orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Post getNextPostOfNullable(Date date) {
|
|
||||||
return getNextPost(date).orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<PostSimpleOutputDTO> convertToSimpleDto(@NonNull Page<Post> postPage) {
|
public Page<PostSimpleOutputDTO> convertToSimpleDto(@NonNull Page<Post> postPage) {
|
||||||
Assert.notNull(postPage, "Post page must not be null");
|
Assert.notNull(postPage, "Post page must not be null");
|
||||||
|
@ -469,7 +469,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.of(nextPostPage.getContent().get(nextPostPage.getContent().size()-1));
|
return Optional.of(nextPostPage.getContent().get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -23,7 +23,7 @@ import java.util.List;
|
||||||
* @date : 2019-03-17
|
* @date : 2019-03-17
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(value = "/archives")
|
@RequestMapping(value = "archives")
|
||||||
public class ContentArchiveController {
|
public class ContentArchiveController {
|
||||||
|
|
||||||
private final PostService postService;
|
private final PostService postService;
|
||||||
|
@ -57,33 +57,21 @@ public class ContentArchiveController {
|
||||||
* @param model model
|
* @param model model
|
||||||
* @return template path: theme/{theme}/post.ftl
|
* @return template path: theme/{theme}/post.ftl
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "{url}")
|
@GetMapping("{url}")
|
||||||
public String post(@PathVariable String url,
|
public String post(@PathVariable("url") String url,
|
||||||
@RequestParam(value = "cp", defaultValue = "1") Integer cp,
|
@RequestParam(value = "cp", defaultValue = "1") Integer cp,
|
||||||
HttpServletRequest request,
|
HttpServletRequest request,
|
||||||
Model model) {
|
Model model) {
|
||||||
final Post post = postService.getByUrl(url);
|
Post post = postService.getBy(PostStatus.PUBLISHED, url);
|
||||||
if (null == post || !post.getStatus().equals(PostStatus.PUBLISHED)) {
|
|
||||||
return "redirect:/404";
|
|
||||||
}
|
|
||||||
|
|
||||||
final Date publishTime = post.getCreateTime();
|
|
||||||
final Post nextPost = postService.getNextPostOfNullable(publishTime);
|
|
||||||
final Post prePost = postService.getPrePostOfNullable(publishTime);
|
|
||||||
|
|
||||||
if (null != prePost) {
|
|
||||||
model.addAttribute("prePost", prePost);
|
|
||||||
}
|
|
||||||
if (null != nextPost) {
|
|
||||||
model.addAttribute("nextPost", nextPost);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
postService.getNextPost(post.getCreateTime()).ifPresent(nextPost -> model.addAttribute("nextPost", nextPost));
|
||||||
|
postService.getPrePost(post.getCreateTime()).ifPresent(prePost -> model.addAttribute("prePost", prePost));
|
||||||
|
|
||||||
List<Category> categories = postCategoryService.listCategoryBy(post.getId());
|
List<Category> categories = postCategoryService.listCategoryBy(post.getId());
|
||||||
List<Tag> tags = postTagService.listTagsBy(post.getId());
|
List<Tag> tags = postTagService.listTagsBy(post.getId());
|
||||||
|
|
||||||
model.addAttribute("is_post", true);
|
model.addAttribute("is_post", true);
|
||||||
model.addAttribute("post",post);
|
model.addAttribute("post", post);
|
||||||
model.addAttribute("categories", categories);
|
model.addAttribute("categories", categories);
|
||||||
model.addAttribute("tags", tags);
|
model.addAttribute("tags", tags);
|
||||||
return themeService.render("post");
|
return themeService.render("post");
|
||||||
|
|
Loading…
Reference in New Issue