Refactor CommentService

pull/98/head
johnniang 2019-02-19 22:36:25 +08:00
parent cf4c491797
commit 50a3e94347
7 changed files with 25 additions and 69 deletions

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.service;
import cc.ryanc.halo.model.domain.Comment; import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -16,14 +17,7 @@ import java.util.Optional;
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2018/1/22 * @date : 2018/1/22
*/ */
public interface CommentService { public interface CommentService extends CrudService<Comment, Long> {
/**
*
*
* @param comment comment
*/
void save(Comment comment);
/** /**
* *
@ -31,6 +25,7 @@ public interface CommentService {
* @param commentId commentId * @param commentId commentId
* @return Optional * @return Optional
*/ */
@Deprecated
Optional<Comment> remove(Long commentId); Optional<Comment> remove(Long commentId);
/** /**
@ -50,13 +45,6 @@ public interface CommentService {
*/ */
List<Comment> findAll(Integer status); List<Comment> findAll(Integer status);
/**
*
*
* @return List
*/
List<Comment> findAll();
/** /**
* *
* *
@ -66,14 +54,6 @@ public interface CommentService {
*/ */
Comment updateCommentStatus(Long commentId, Integer status); Comment updateCommentStatus(Long commentId, Integer status);
/**
*
*
* @param commentId commentId
* @return Optional
*/
Optional<Comment> findCommentById(Long commentId);
/** /**
* *
* *
@ -126,13 +106,6 @@ public interface CommentService {
*/ */
Integer getCountByStatus(Integer status); Integer getCountByStatus(Integer status);
/**
*
*
* @return Long
*/
Long getCount();
/** /**
* *
* *

View File

@ -4,7 +4,7 @@ import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.repository.CommentRepository; import cc.ryanc.halo.repository.CommentRepository;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired; import cc.ryanc.halo.service.base.AbstractCrudService;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
@ -24,14 +24,18 @@ import java.util.Optional;
* @date : 2018/1/22 * @date : 2018/1/22
*/ */
@Service @Service
public class CommentServiceImpl implements CommentService { public class CommentServiceImpl extends AbstractCrudService<Comment, Long> implements CommentService {
private static final String COMMENTS_CACHE_NAME = "comments"; private static final String COMMENTS_CACHE_NAME = "comments";
private static final String POSTS_CACHE_NAME = "posts"; private static final String POSTS_CACHE_NAME = "posts";
@Autowired private final CommentRepository commentRepository;
private CommentRepository commentRepository;
public CommentServiceImpl(CommentRepository commentRepository) {
super(commentRepository);
this.commentRepository = commentRepository;
}
/** /**
* *
@ -40,8 +44,8 @@ public class CommentServiceImpl implements CommentService {
*/ */
@Override @Override
@CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true) @CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
public void save(Comment comment) { public Comment create(Comment comment) {
commentRepository.save(comment); return super.create(comment);
} }
/** /**
@ -53,7 +57,7 @@ public class CommentServiceImpl implements CommentService {
@Override @Override
@CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true) @CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
public Optional<Comment> remove(Long commentId) { public Optional<Comment> remove(Long commentId) {
final Optional<Comment> comment = this.findCommentById(commentId); final Optional<Comment> comment = this.fetchById(commentId);
commentRepository.delete(comment.orElse(null)); commentRepository.delete(comment.orElse(null));
return comment; return comment;
} }
@ -88,7 +92,7 @@ public class CommentServiceImpl implements CommentService {
*/ */
@Override @Override
@Cacheable(value = COMMENTS_CACHE_NAME, key = "'comment'") @Cacheable(value = COMMENTS_CACHE_NAME, key = "'comment'")
public List<Comment> findAll() { public List<Comment> listAll() {
return commentRepository.findAll(); return commentRepository.findAll();
} }
@ -102,22 +106,11 @@ public class CommentServiceImpl implements CommentService {
@Override @Override
@CacheEvict(value = COMMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true) @CacheEvict(value = COMMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Comment updateCommentStatus(Long commentId, Integer status) { public Comment updateCommentStatus(Long commentId, Integer status) {
final Optional<Comment> comment = findCommentById(commentId); final Optional<Comment> comment = fetchById(commentId);
comment.get().setCommentStatus(status); comment.get().setCommentStatus(status);
return commentRepository.save(comment.get()); return commentRepository.save(comment.get());
} }
/**
*
*
* @param commentId commentId
* @return Optional
*/
@Override
public Optional<Comment> findCommentById(Long commentId) {
return commentRepository.findById(commentId);
}
/** /**
* *
* *
@ -189,16 +182,6 @@ public class CommentServiceImpl implements CommentService {
return commentRepository.countAllByCommentStatus(status); return commentRepository.countAllByCommentStatus(status);
} }
/**
*
*
* @return Long
*/
@Override
public Long getCount() {
return commentRepository.count();
}
/** /**
* *
* *

View File

@ -84,7 +84,7 @@ public class AdminController extends BaseController {
public String index(Model model) { public String index(Model model) {
//查询评论的条数 //查询评论的条数
final Long commentCount = commentService.getCount(); final Long commentCount = commentService.count();
model.addAttribute("commentCount", commentCount); model.addAttribute("commentCount", commentCount);
//查询最新的文章 //查询最新的文章

View File

@ -158,11 +158,11 @@ public class CommentController extends BaseController {
final User user = (User) session.getAttribute(USER_SESSION_KEY); final User user = (User) session.getAttribute(USER_SESSION_KEY);
//被回复的评论 //被回复的评论
final Comment lastComment = commentService.findCommentById(commentId).orElse(new Comment()); final Comment lastComment = commentService.fetchById(commentId).orElse(new Comment());
//修改被回复的评论的状态 //修改被回复的评论的状态
lastComment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode()); lastComment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode());
commentService.save(lastComment); commentService.create(lastComment);
//保存评论 //保存评论
final Comment comment = new Comment(); final Comment comment = new Comment();
@ -185,7 +185,7 @@ public class CommentController extends BaseController {
comment.setCommentParent(commentId); comment.setCommentParent(commentId);
comment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode()); comment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode());
comment.setIsAdmin(1); comment.setIsAdmin(1);
commentService.save(comment); commentService.create(comment);
//邮件通知 //邮件通知
new EmailToAuthor(comment, lastComment, post, user, commentContent).start(); new EmailToAuthor(comment, lastComment, post, user, commentContent).start();

View File

@ -76,7 +76,7 @@ public class ApiCommentController {
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail())); comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail()));
} }
if (comment.getCommentParent() > 0) { if (comment.getCommentParent() > 0) {
lastComment = commentService.findCommentById(comment.getCommentParent()).orElse(new Comment()); lastComment = commentService.fetchById(comment.getCommentParent()).orElse(new Comment());
final StrBuilder buildContent = new StrBuilder("<a href='#comment-id-"); final StrBuilder buildContent = new StrBuilder("<a href='#comment-id-");
buildContent.append(lastComment.getCommentId()); buildContent.append(lastComment.getCommentId());
buildContent.append("'>@"); buildContent.append("'>@");
@ -91,7 +91,7 @@ public class ApiCommentController {
if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) { if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl())); comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl()));
} }
commentService.save(comment); commentService.create(comment);
if (StrUtil.equals(OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) { if (StrUtil.equals(OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), "你的评论已经提交,待博主审核之后可显示。"); return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), "你的评论已经提交,待博主审核之后可显示。");
} else { } else {

View File

@ -154,7 +154,7 @@ public class InstallController {
comment.setCommentStatus(0); comment.setCommentStatus(0);
comment.setCommentAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36"); comment.setCommentAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36");
comment.setIsAdmin(0); comment.setIsAdmin(0);
commentService.save(comment); commentService.create(comment);
final Map<String, String> options = new HashMap<>(); final Map<String, String> options = new HashMap<>();
options.put(BlogPropertiesEnum.IS_INSTALL.getProp(), TrueFalseEnum.TRUE.getDesc()); options.put(BlogPropertiesEnum.IS_INSTALL.getProp(), TrueFalseEnum.TRUE.getDesc());

View File

@ -90,7 +90,7 @@ public class FrontCommentController {
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail())); comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail()));
} }
if (comment.getCommentParent() > 0) { if (comment.getCommentParent() > 0) {
lastComment = commentService.findCommentById(comment.getCommentParent()).orElse(new Comment()); lastComment = commentService.fetchById(comment.getCommentParent()).orElse(new Comment());
final StrBuilder buildContent = new StrBuilder("<a href='#comment-id-"); final StrBuilder buildContent = new StrBuilder("<a href='#comment-id-");
buildContent.append(lastComment.getCommentId()); buildContent.append(lastComment.getCommentId());
buildContent.append("'>@"); buildContent.append("'>@");
@ -105,7 +105,7 @@ public class FrontCommentController {
if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) { if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl())); comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl()));
} }
commentService.save(comment); commentService.create(comment);
if (comment.getCommentParent() > 0) { if (comment.getCommentParent() > 0) {
new EmailToParent(comment, lastComment, post).start(); new EmailToParent(comment, lastComment, post).start();
new EmailToAdmin(comment, post).start(); new EmailToAdmin(comment, post).start();