👽 代码优化

pull/21/head
ruibaby 2018-07-16 17:01:42 +08:00
parent c3deec86a7
commit 67418fd0cf
7 changed files with 42 additions and 21 deletions

View File

@ -46,7 +46,7 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
Page<Comment> findCommentsByPost(Post post, Pageable pageable);
/**
*
*
*
* @param post post
* @param pageable pageable
@ -55,6 +55,15 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
*/
Page<Comment> findCommentsByPostAndCommentStatus(Post post, Pageable pageable, Integer status);
/**
*
*
* @param post post
* @param status status
* @return List
*/
List<Comment> findCommentsByPostAndCommentStatus(Post post, Integer status);
/**
*
*

View File

@ -80,7 +80,7 @@ public interface CommentService {
Page<Comment> findCommentsByPost(Post post, Pageable pageable);
/**
*
*
*
* @param post post
* @param pageable pageable
@ -89,6 +89,15 @@ public interface CommentService {
*/
Page<Comment> findCommentsByPostAndCommentStatus(Post post, Pageable pageable, Integer status);
/**
*
*
* @param post post
* @param status status
* @return List
*/
List<Comment> findCommentsByPostAndCommentStatus(Post post, Integer status);
/**
*
*

View File

@ -137,6 +137,18 @@ public class CommentServiceImpl implements CommentService {
return commentRepository.findCommentsByPostAndCommentStatus(post, pageable, status);
}
/**
*
*
* @param post post
* @param status status
* @return List
*/
@Override
public List<Comment> findCommentsByPostAndCommentStatus(Post post, Integer status) {
return commentRepository.findCommentsByPostAndCommentStatus(post, status);
}
/**
*
*

View File

@ -118,12 +118,10 @@ public class FrontArchiveController extends BaseController {
if (null != afterPosts && afterPosts.size() > 0) {
model.addAttribute("afterPost", afterPosts.get(afterPosts.size() - 1));
}
Sort sort = new Sort(Sort.Direction.DESC, "commentDate");
Pageable pageable = PageRequest.of(0, 999, sort);
Page<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post, pageable, CommentStatus.PUBLISHED.getCode());
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getCode());
model.addAttribute("post", post);
model.addAttribute("comments", CommentUtil.getComments(comments.getContent()));
model.addAttribute("commentsCount", comments.getTotalElements());
model.addAttribute("comments", CommentUtil.getComments(comments));
model.addAttribute("commentsCount", comments.size());
postService.updatePostView(post);
return this.render("post");
}

View File

@ -11,10 +11,6 @@ import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.CommentUtil;
import cc.ryanc.halo.web.controller.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@ -70,16 +66,13 @@ public class FrontPageController extends BaseController {
@GetMapping(value = "/p/{postUrl}")
public String getPage(@PathVariable(value = "postUrl") String postUrl, Model model) {
Post post = postService.findByPostUrl(postUrl, PostType.POST_TYPE_PAGE.getDesc());
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
Pageable pageable = PageRequest.of(0,999,sort);
Page<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post,pageable,CommentStatus.PUBLISHED.getCode());
if(null==post){
if (null == post) {
return this.renderNotFound();
}
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getCode());
model.addAttribute("post", post);
model.addAttribute("comments", CommentUtil.getComments(comments.getContent()));
model.addAttribute("commentsCount", comments.getTotalElements());
model.addAttribute("comments", CommentUtil.getComments(comments));
model.addAttribute("commentsCount", comments.size());
postService.updatePostView(post);
return this.render("page");
}

View File

@ -258,7 +258,7 @@
<#macro childComments comments>
<ul class="native-list" style="margin-left: 30px; border-left: 1px solid #f1f1f1">
<#if comments?? && comments?size gt 0>
<#list comments as comment>
<#list comments?sort_by("commentDate") as comment>
<li class="native-list-one" id="comment-id-${comment.commentId?c}" style="margin-left: 5px;">
<img class="native-list-one-img" style="width: 2rem;height: 2rem;" src="//gravatar.loli.net/avatar/${comment.commentAuthorAvatarMd5?if_exists}?s=256&d=${options.native_comment_avatar?default('mm')}">
<section>
@ -287,7 +287,7 @@
</#macro>
<ul class="native-list">
<#if comments?? && comments?size gt 0>
<#list comments as comment>
<#list comments?sort_by("commentDate")?reverse as comment>
<li class="native-list-one" id="comment-id-${comment.commentId?c}">
<img class="native-list-one-img" src="//gravatar.loli.net/avatar/${comment.commentAuthorAvatarMd5?if_exists}?s=256&d=${options.native_comment_avatar?default('mm')}">
<section>

View File

@ -44,7 +44,7 @@
</h3>
</div>
<div class="post-content">
${post.postContent}
${post.postContent?if_exists}
</div>
<div class="post-footer">
<div class="meta">