From 50a3e94347d1b1891a21541369e7cc732570e24a Mon Sep 17 00:00:00 2001 From: johnniang <1340692778@qq.com> Date: Tue, 19 Feb 2019 22:36:25 +0800 Subject: [PATCH] Refactor CommentService --- .../cc/ryanc/halo/service/CommentService.java | 33 ++------------ .../halo/service/impl/CommentServiceImpl.java | 43 ++++++------------- .../web/controller/admin/AdminController.java | 2 +- .../controller/admin/CommentController.java | 6 +-- .../controller/api/ApiCommentController.java | 4 +- .../controller/core/InstallController.java | 2 +- .../front/FrontCommentController.java | 4 +- 7 files changed, 25 insertions(+), 69 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/service/CommentService.java b/src/main/java/cc/ryanc/halo/service/CommentService.java index 0314ca294..723aad7d3 100644 --- a/src/main/java/cc/ryanc/halo/service/CommentService.java +++ b/src/main/java/cc/ryanc/halo/service/CommentService.java @@ -2,6 +2,7 @@ package cc.ryanc.halo.service; import cc.ryanc.halo.model.domain.Comment; 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.Pageable; @@ -16,14 +17,7 @@ import java.util.Optional; * @author : RYAN0UP * @date : 2018/1/22 */ -public interface CommentService { - - /** - * 新增评论 - * - * @param comment comment - */ - void save(Comment comment); +public interface CommentService extends CrudService { /** * 删除评论 @@ -31,6 +25,7 @@ public interface CommentService { * @param commentId commentId * @return Optional */ + @Deprecated Optional remove(Long commentId); /** @@ -50,13 +45,6 @@ public interface CommentService { */ List findAll(Integer status); - /** - * 查询所有评论,不分页 - * - * @return List - */ - List findAll(); - /** * 更改评论的状态 * @@ -66,14 +54,6 @@ public interface CommentService { */ Comment updateCommentStatus(Long commentId, Integer status); - /** - * 根据评论编号查询评论 - * - * @param commentId commentId - * @return Optional - */ - Optional findCommentById(Long commentId); - /** * 根据文章查询评论 * @@ -126,13 +106,6 @@ public interface CommentService { */ Integer getCountByStatus(Integer status); - /** - * 查询评论总数 - * - * @return Long - */ - Long getCount(); - /** * 获取最近的评论 * diff --git a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java index 8f4a7f145..8aae694eb 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java @@ -4,7 +4,7 @@ import cc.ryanc.halo.model.domain.Comment; import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.repository.CommentRepository; 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.CachePut; import org.springframework.cache.annotation.Cacheable; @@ -24,14 +24,18 @@ import java.util.Optional; * @date : 2018/1/22 */ @Service -public class CommentServiceImpl implements CommentService { +public class CommentServiceImpl extends AbstractCrudService implements CommentService { private static final String COMMENTS_CACHE_NAME = "comments"; private static final String POSTS_CACHE_NAME = "posts"; - @Autowired - private CommentRepository commentRepository; + private final CommentRepository commentRepository; + + public CommentServiceImpl(CommentRepository commentRepository) { + super(commentRepository); + this.commentRepository = commentRepository; + } /** * 新增评论 @@ -40,8 +44,8 @@ public class CommentServiceImpl implements CommentService { */ @Override @CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true) - public void save(Comment comment) { - commentRepository.save(comment); + public Comment create(Comment comment) { + return super.create(comment); } /** @@ -53,7 +57,7 @@ public class CommentServiceImpl implements CommentService { @Override @CacheEvict(value = {COMMENTS_CACHE_NAME, POSTS_CACHE_NAME}, allEntries = true, beforeInvocation = true) public Optional remove(Long commentId) { - final Optional comment = this.findCommentById(commentId); + final Optional comment = this.fetchById(commentId); commentRepository.delete(comment.orElse(null)); return comment; } @@ -88,7 +92,7 @@ public class CommentServiceImpl implements CommentService { */ @Override @Cacheable(value = COMMENTS_CACHE_NAME, key = "'comment'") - public List findAll() { + public List listAll() { return commentRepository.findAll(); } @@ -102,22 +106,11 @@ public class CommentServiceImpl implements CommentService { @Override @CacheEvict(value = COMMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Comment updateCommentStatus(Long commentId, Integer status) { - final Optional comment = findCommentById(commentId); + final Optional comment = fetchById(commentId); comment.get().setCommentStatus(status); return commentRepository.save(comment.get()); } - /** - * 根据评论编号查询评论 - * - * @param commentId commentId - * @return Optional - */ - @Override - public Optional findCommentById(Long commentId) { - return commentRepository.findById(commentId); - } - /** * 根据文章查询评论 * @@ -189,16 +182,6 @@ public class CommentServiceImpl implements CommentService { return commentRepository.countAllByCommentStatus(status); } - /** - * 查询评论总数 - * - * @return Long - */ - @Override - public Long getCount() { - return commentRepository.count(); - } - /** * 获取最近的评论 * diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/AdminController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/AdminController.java index 8b1c0f542..8da497d7f 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/AdminController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/AdminController.java @@ -84,7 +84,7 @@ public class AdminController extends BaseController { public String index(Model model) { //查询评论的条数 - final Long commentCount = commentService.getCount(); + final Long commentCount = commentService.count(); model.addAttribute("commentCount", commentCount); //查询最新的文章 diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java index e7234923f..ca9ca1043 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java @@ -158,11 +158,11 @@ public class CommentController extends BaseController { 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()); - commentService.save(lastComment); + commentService.create(lastComment); //保存评论 final Comment comment = new Comment(); @@ -185,7 +185,7 @@ public class CommentController extends BaseController { comment.setCommentParent(commentId); comment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode()); comment.setIsAdmin(1); - commentService.save(comment); + commentService.create(comment); //邮件通知 new EmailToAuthor(comment, lastComment, post, user, commentContent).start(); diff --git a/src/main/java/cc/ryanc/halo/web/controller/api/ApiCommentController.java b/src/main/java/cc/ryanc/halo/web/controller/api/ApiCommentController.java index 777ae440a..89ee62ed1 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/api/ApiCommentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/api/ApiCommentController.java @@ -76,7 +76,7 @@ public class ApiCommentController { comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail())); } 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("@"); @@ -91,7 +91,7 @@ public class ApiCommentController { if (StrUtil.isNotEmpty(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) { return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), "你的评论已经提交,待博主审核之后可显示。"); } else { diff --git a/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java b/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java index 4075ae973..e335246ba 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java @@ -154,7 +154,7 @@ public class InstallController { 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.setIsAdmin(0); - commentService.save(comment); + commentService.create(comment); final Map options = new HashMap<>(); options.put(BlogPropertiesEnum.IS_INSTALL.getProp(), TrueFalseEnum.TRUE.getDesc()); diff --git a/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java b/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java index 6472de2b0..5876bc7c0 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java @@ -90,7 +90,7 @@ public class FrontCommentController { comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail())); } 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("@"); @@ -105,7 +105,7 @@ public class FrontCommentController { if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) { comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl())); } - commentService.save(comment); + commentService.create(comment); if (comment.getCommentParent() > 0) { new EmailToParent(comment, lastComment, post).start(); new EmailToAdmin(comment, post).start();