From da934ff548c801138e7f347ae8c77a310e91682d Mon Sep 17 00:00:00 2001 From: ronger Date: Wed, 11 May 2022 19:50:28 +0800 Subject: [PATCH] =?UTF-8?q?:lock:=20=E8=AF=84=E8=AE=BA=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E9=89=B4=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/forest/service/CommentService.java | 3 +- .../service/impl/CommentServiceImpl.java | 33 +++++++++---------- .../web/api/comment/CommentController.java | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/rymcu/forest/service/CommentService.java b/src/main/java/com/rymcu/forest/service/CommentService.java index 92b2b9d..a6bc0ed 100644 --- a/src/main/java/com/rymcu/forest/service/CommentService.java +++ b/src/main/java/com/rymcu/forest/service/CommentService.java @@ -3,6 +3,7 @@ package com.rymcu.forest.service; import com.rymcu.forest.core.service.Service; import com.rymcu.forest.dto.CommentDTO; import com.rymcu.forest.entity.Comment; +import com.rymcu.forest.web.api.exception.BaseApiException; import javax.servlet.http.HttpServletRequest; import java.util.List; @@ -26,7 +27,7 @@ public interface CommentService extends Service { * @param request * @return */ - Map postComment(Comment comment, HttpServletRequest request); + Map postComment(Comment comment, HttpServletRequest request) throws BaseApiException; /** * 获取评论列表数据 diff --git a/src/main/java/com/rymcu/forest/service/impl/CommentServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/CommentServiceImpl.java index f3838be..111d52b 100644 --- a/src/main/java/com/rymcu/forest/service/impl/CommentServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/CommentServiceImpl.java @@ -9,10 +9,8 @@ import com.rymcu.forest.entity.Comment; import com.rymcu.forest.mapper.CommentMapper; import com.rymcu.forest.service.ArticleService; import com.rymcu.forest.service.CommentService; -import com.rymcu.forest.util.Html2TextUtil; -import com.rymcu.forest.util.NotificationUtils; -import com.rymcu.forest.util.Utils; -import com.rymcu.forest.util.XssUtils; +import com.rymcu.forest.util.*; +import com.rymcu.forest.web.api.exception.BaseApiException; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -65,23 +63,24 @@ public class CommentServiceImpl extends AbstractService implements Comm @Override @Transactional(rollbackFor = Exception.class) - public Map postComment(Comment comment, HttpServletRequest request) { + public Map postComment(Comment comment, HttpServletRequest request) throws BaseApiException { + comment.setCommentAuthorId(Objects.requireNonNull(UserUtils.getCurrentUserByToken()).getIdUser()); Map map = new HashMap(1); - if(comment.getCommentArticleId() == null){ - map.put("message","非法访问,文章主键异常!"); + if (comment.getCommentArticleId() == null) { + map.put("message", "非法访问,文章主键异常!"); return map; } - if(comment.getCommentAuthorId() == null){ - map.put("message","非法访问,用户未登录!"); + if (comment.getCommentAuthorId() == null) { + map.put("message", "非法访问,用户未登录!"); return map; } - if(StringUtils.isBlank(comment.getCommentContent())){ - map.put("message","回帖内容不能为空!"); + if (StringUtils.isBlank(comment.getCommentContent())) { + map.put("message", "回帖内容不能为空!"); return map; } Article article = articleService.findById(comment.getCommentArticleId().toString()); if (article == null) { - map.put("message","文章不存在!"); + map.put("message", "文章不存在!"); return map; } String ip = Utils.getIpAddress(request); @@ -95,23 +94,23 @@ public class CommentServiceImpl extends AbstractService implements Comm commentMapper.updateCommentSharpUrl(comment.getIdComment(), commentSharpUrl); String commentContent = comment.getCommentContent(); - if(StringUtils.isNotBlank(commentContent)){ + if (StringUtils.isNotBlank(commentContent)) { Integer length = commentContent.length(); - if(length > MAX_PREVIEW){ + if (length > MAX_PREVIEW) { length = 200; } - String commentPreviewContent = commentContent.substring(0,length); + String commentPreviewContent = commentContent.substring(0, length); commentContent = Html2TextUtil.getContent(commentPreviewContent); // 评论者不是作者本人则进行消息通知 if (!article.getArticleAuthorId().equals(comment.getCommentAuthorId())) { - NotificationUtils.saveNotification(article.getArticleAuthorId(),comment.getIdComment(), NotificationConstant.Comment, commentContent); + NotificationUtils.saveNotification(article.getArticleAuthorId(), comment.getIdComment(), NotificationConstant.Comment, commentContent); } // 判断是否是回复消息 if (comment.getCommentOriginalCommentId() != null && comment.getCommentOriginalCommentId() != 0) { Comment originalComment = commentMapper.selectByPrimaryKey(comment.getCommentOriginalCommentId()); // 回复消息时,评论者不是上级评论作者则进行消息通知 if (!comment.getCommentAuthorId().equals(originalComment.getCommentAuthorId())) { - NotificationUtils.saveNotification(originalComment.getCommentAuthorId(),comment.getIdComment(), NotificationConstant.Comment, commentContent); + NotificationUtils.saveNotification(originalComment.getCommentAuthorId(), comment.getIdComment(), NotificationConstant.Comment, commentContent); } } } diff --git a/src/main/java/com/rymcu/forest/web/api/comment/CommentController.java b/src/main/java/com/rymcu/forest/web/api/comment/CommentController.java index 7ca1ed4..56d8cb6 100644 --- a/src/main/java/com/rymcu/forest/web/api/comment/CommentController.java +++ b/src/main/java/com/rymcu/forest/web/api/comment/CommentController.java @@ -26,7 +26,7 @@ public class CommentController { private CommentService commentService; @PostMapping("/post") - public GlobalResult postComment(@RequestBody Comment comment, HttpServletRequest request) throws BaseApiException, UnsupportedEncodingException { + public GlobalResult postComment(@RequestBody Comment comment, HttpServletRequest request) throws BaseApiException { Map map = commentService.postComment(comment,request); return GlobalResultGenerator.genSuccessResult(map); }