diff --git a/springBootBlog/src/main/java/com/yen/mdblog/controller/CommentController.java b/springBootBlog/src/main/java/com/yen/mdblog/controller/CommentController.java index 026acfda6..c5719a8f0 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/controller/CommentController.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/controller/CommentController.java @@ -1,6 +1,5 @@ package com.yen.mdblog.controller; -import com.yen.mdblog.entity.Po.Comment; import com.yen.mdblog.entity.Vo.CreateComment; import com.yen.mdblog.service.CommentService; import lombok.extern.log4j.Log4j2; @@ -10,7 +9,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.security.Principal; -import java.util.Date; @Controller @RequestMapping("/comment") @@ -23,15 +21,15 @@ public class CommentController { @RequestMapping(value="/create", method= RequestMethod.POST) public String createComment(CreateComment request, Model model, Principal principal){ - Comment comment = new Comment(); - comment.setUserName(principal.getName()); - comment.setPostId(request.getPostId()); - comment.setCommentContent(request.getCommentContent()); - comment.setCreateTime(new Date()); - log.info("(CommentController) create new comment = " + comment.toString()); - commentService.insertComment(comment); - model.addAttribute("user", principal.getName()); - return "comment/comment_success"; + String userName = principal.getName(); + Long postId = request.getPostId(); + String commentContent = request.getCommentContent(); + Boolean createCommentSuccess = commentService.insertComment(userName, postId, commentContent); + if (createCommentSuccess) { + return "comment/comment_success"; + }else{ + return "create comment failed"; + } } } diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/CommentService.java b/springBootBlog/src/main/java/com/yen/mdblog/service/CommentService.java index 9345b9d9f..9aa0936de 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/service/CommentService.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/CommentService.java @@ -8,5 +8,5 @@ public interface CommentService { List getCommentsByPostId(Long postId); - void insertComment(Comment comment); + Boolean insertComment(String userName, Long postId, String commentContent); } diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/CommentServiceImpl.java b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/CommentServiceImpl.java index a98220752..cfe9ca09b 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/CommentServiceImpl.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/CommentServiceImpl.java @@ -3,12 +3,15 @@ import com.yen.mdblog.entity.Po.Comment; import com.yen.mdblog.mapper.CommentMapper; import com.yen.mdblog.service.CommentService; +import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.List; @Service +@Log4j2 public class CommentServiceImpl implements CommentService { @Autowired @@ -21,9 +24,21 @@ public List getCommentsByPostId(Long postId) { } @Override - public void insertComment(Comment comment) { - - commentMapper.insertComment(comment); + public Boolean insertComment(String userName, Long postId, String commentContent) { + + try{ + Comment comment = new Comment(); + comment.setUserName(userName); + comment.setPostId(postId); + comment.setCommentContent(commentContent); + comment.setCreateTime(new Date()); + log.info("create comment OK"); + commentMapper.insertComment(comment); + return true; + }catch (Exception e){ + log.warn("create comment failed : " + e); + return false; + } } }