Skip to content

Commit 27b5c9f

Browse files
committed
move insert comment biz logic to service, fix controller, import
1 parent be67751 commit 27b5c9f

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.yen.mdblog.controller;
22

3-
import com.yen.mdblog.entity.Po.Comment;
43
import com.yen.mdblog.entity.Vo.CreateComment;
54
import com.yen.mdblog.service.CommentService;
65
import lombok.extern.log4j.Log4j2;
@@ -10,7 +9,6 @@
109
import org.springframework.web.bind.annotation.RequestMapping;
1110
import org.springframework.web.bind.annotation.RequestMethod;
1211
import java.security.Principal;
13-
import java.util.Date;
1412

1513
@Controller
1614
@RequestMapping("/comment")
@@ -23,15 +21,15 @@ public class CommentController {
2321
@RequestMapping(value="/create", method= RequestMethod.POST)
2422
public String createComment(CreateComment request, Model model, Principal principal){
2523

26-
Comment comment = new Comment();
27-
comment.setUserName(principal.getName());
28-
comment.setPostId(request.getPostId());
29-
comment.setCommentContent(request.getCommentContent());
30-
comment.setCreateTime(new Date());
31-
log.info("(CommentController) create new comment = " + comment.toString());
32-
commentService.insertComment(comment);
33-
model.addAttribute("user", principal.getName());
34-
return "comment/comment_success";
24+
String userName = principal.getName();
25+
Long postId = request.getPostId();
26+
String commentContent = request.getCommentContent();
27+
Boolean createCommentSuccess = commentService.insertComment(userName, postId, commentContent);
28+
if (createCommentSuccess) {
29+
return "comment/comment_success";
30+
}else{
31+
return "create comment failed";
32+
}
3533
}
3634

3735
}

springBootBlog/src/main/java/com/yen/mdblog/service/CommentService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ public interface CommentService {
88

99
List<Comment> getCommentsByPostId(Long postId);
1010

11-
void insertComment(Comment comment);
11+
Boolean insertComment(String userName, Long postId, String commentContent);
1212
}

springBootBlog/src/main/java/com/yen/mdblog/service/impl/CommentServiceImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import com.yen.mdblog.entity.Po.Comment;
44
import com.yen.mdblog.mapper.CommentMapper;
55
import com.yen.mdblog.service.CommentService;
6+
import lombok.extern.log4j.Log4j2;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Service;
89

10+
import java.util.Date;
911
import java.util.List;
1012

1113
@Service
14+
@Log4j2
1215
public class CommentServiceImpl implements CommentService {
1316

1417
@Autowired
@@ -21,9 +24,21 @@ public List<Comment> getCommentsByPostId(Long postId) {
2124
}
2225

2326
@Override
24-
public void insertComment(Comment comment) {
25-
26-
commentMapper.insertComment(comment);
27+
public Boolean insertComment(String userName, Long postId, String commentContent) {
28+
29+
try{
30+
Comment comment = new Comment();
31+
comment.setUserName(userName);
32+
comment.setPostId(postId);
33+
comment.setCommentContent(commentContent);
34+
comment.setCreateTime(new Date());
35+
log.info("create comment OK");
36+
commentMapper.insertComment(comment);
37+
return true;
38+
}catch (Exception e){
39+
log.warn("create comment failed : " + e);
40+
return false;
41+
}
2742
}
2843

2944
}

0 commit comments

Comments
 (0)