一、数据层
- 增加评论数据。
- 修改帖子的评论数量。
由Mybatis plus实现。
二、业务层
- 处理添加评论的业务:先增加评论、再更新帖子的评论数量。
DiscussPostService
接口
public interface DiscussPostService extends IService<DiscussPost> {
// ...
boolean updateCommentCount(int id, int commentCount);
}
DiscussPostServiceImpl
@Service
public class DiscussPostServiceImpl extends ServiceImpl<DiscussPostMapper, DiscussPost>
implements DiscussPostService{
// ...
@Override
public boolean updateCommentCount(int id, int commentCount) {
LambdaUpdateWrapper<DiscussPost> luw = new LambdaUpdateWrapper<>();
luw.set(DiscussPost::getCommentCount,commentCount)
.eq(DiscussPost::getId,id);
return this.update(luw);
}
}
CommentService
接口
public interface CommentService extends IService<Comment> {
// ...
boolean addComment(Comment comment);
}
CommentServiceImpl
@Override
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public boolean addComment(Comment comment) {
if (comment == null) {
throw new IllegalArgumentException("参数不能为空!");
}
// 添加评论
comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
comment.setContent(sensitiveFilter.filter(comment.getContent()));
boolean res = this.save(comment);
// 更新帖子评论数量
if (comment.getEntityType() == ENTITY_TYPE_POST) {
int count = this.baseMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
discussPostService.updateCommentCount(comment.getEntityId(), count);
}
return res;
}
三、表现层
- 处理添加评论数据的请求。
- 设置添加评论的表单。
CommentController
@Controller
@RequestMapping("/comment")
public class CommentController {
@Autowired
private CommentService commentService;
@Autowired
private HostHolder hostHolder;
@RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
comment.setUserId(hostHolder.getUser().getId());
comment.setStatus(0);
comment.setCreateTime(new Date());
commentService.addComment(comment);
return "redirect:/discuss/detail/" + discussPostId;
}
}
页面处理,略。
标签:comment,十三,int,添加,评论,boolean,public From: https://www.cnblogs.com/dalelee/p/16617712.html