Skip to content

Commit

Permalink
move insert comment biz logic to service, fix controller, import
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 6, 2023
1 parent be67751 commit 27b5c9f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand All @@ -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";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface CommentService {

List<Comment> getCommentsByPostId(Long postId);

void insertComment(Comment comment);
Boolean insertComment(String userName, Long postId, String commentContent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,9 +24,21 @@ public List<Comment> 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;
}
}

}

0 comments on commit 27b5c9f

Please sign in to comment.