当前位置:   article > 正文

牛客网项目---2.3.查看帖子详情以及显示评论

帖子详情

帖子详情

在帖子标题上增加访问帖子的链接

4帖子详情/detail/{discussPostId}:在帖子标题处,添加href标签,进入帖子详情,restful风格,后台可以根据@PathVariable("discussPostId")直接获取帖子id,进行查询,加到model中,返回帖子页面

 

5、版主可设置置顶加精

 

6、管理员可以进行删除和统计日活

 

7、使用卡夫卡进行系统通知

 --------------------------------------------------------------------------------------------------------------------------------

1.CommentMapper

  1. import com.nowcoder.community.entity.Comment;
  2. import org.springframework.stereotype.Repository;
  3. import java.util.List;
  4. @Repository
  5. public interface CommentMapper {
  6. List selectCommentByEntity(int entityType,int entityId,int offset,int limit);
  7. int selectCountByEntity(int entityType,int entityId);
  8. int insertComment(Comment comment);
  9. }

2.编写CommentMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.nowcoder.community.mapper.CommentMapper">
  6. <select id="selectCommentByEntity" resultType="comment">
  7. select * from comment where status=0 and entity_type=#{entityType} and entity_id=#{entityId} order by create_time asc limit #{offset},#{limit};
  8. </select>
  9. <select id="selectCountByEntity" resultType="int">
  10. select count(id) from comment where status=0 and entity_type=#{entityType} and entity_id=#{entityId};
  11. </select>
  12. <insert id="insertComment" keyProperty="id" parameterType="Comment">
  13. insert into comment(user_id, entity_type, entity_id, target_id, content, status, create_time) VALUES (#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime});
  14. </insert>
  15. </mapper>

3.CommentService

  1. import com.nowcoder.community.entity.Comment;
  2. import com.nowcoder.community.mapper.CommentMapper;
  3. import com.nowcoder.community.util.CommunityConstant;
  4. import com.nowcoder.community.util.SensitiveFilter;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Isolation;
  8. import org.springframework.transaction.annotation.Propagation;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import org.springframework.web.util.HtmlUtils;
  11. import java.util.List;
  12. @Service
  13. public class CommentServiceImpl implements CommentService, CommunityConstant {
  14. @Autowired
  15. private CommentMapper commentMapper;
  16. @Autowired
  17. private SensitiveFilter sensitiveFilter;
  18. @Autowired
  19. private DiscussPostServiceImpl discussPostService;
  20. @Override
  21. public List selectCommentByEntity(int entityType, int entityId, int offset, int limit) {
  22. return commentMapper.selectCommentByEntity(entityType, entityId, offset, limit);
  23. }
  24. @Override
  25. public int selectCountByEntity(int entityType, int entityId) {
  26. return commentMapper.selectCountByEntity(entityType, entityId);
  27. }
  28. @Override
  29. public int insertComment(Comment comment) {
  30. return commentMapper.insertComment(comment);
  31. }

4.DiscussPostController

  1. import com.nowcoder.community.entity.Comment;
  2. import com.nowcoder.community.entity.DiscussPost;
  3. import com.nowcoder.community.entity.Page;
  4. import com.nowcoder.community.entity.User;
  5. import com.nowcoder.community.service.CommentServiceImpl;
  6. import com.nowcoder.community.service.DiscussPostServiceImpl;
  7. import com.nowcoder.community.service.UserServiceImpl;
  8. import com.nowcoder.community.util.CommunityConstant;
  9. import com.nowcoder.community.util.CommunityUtil;
  10. import com.nowcoder.community.util.HostHolder;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.web.bind.annotation.*;
  15. import java.util.*;
  16. @Controller
  17. @RequestMapping("/discuss")
  18. public class DiscussPostController implements CommunityConstant {
  19. @Autowired
  20. private DiscussPostServiceImpl discussPostService;
  21. @Autowired
  22. private HostHolder hostHolder;
  23. @Autowired
  24. private UserServiceImpl userService;
  25. @Autowired
  26. private CommentServiceImpl commentService;
  27. @PostMapping("/add")
  28. @ResponseBody
  29. public String addDiscussPost(String title,String content){
  30. User user = hostHolder.getUser();
  31. if (user==null){
  32. return CommunityUtil.getJSONString(403,"你还没有登陆");
  33. }
  34. DiscussPost discussPost = new DiscussPost();
  35. discussPost.setUserId(user.getId());
  36. discussPost.setTitle(title);
  37. discussPost.setContent(content);
  38. discussPost.setCreateTime(new Date());
  39. discussPostService.addDiscussPost(discussPost);
  40. return CommunityUtil.getJSONString(0,"发布成功");
  41. }
  42. @GetMapping("/detail/{discussPostId}")
  43. public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page){
  44. //帖子
  45. DiscussPost post = discussPostService.selectDiscussPostById(discussPostId);
  46. model.addAttribute("post",post);
  47. //作者
  48. User user = userService.selectById(post.getUserId());
  49. model.addAttribute("user",user);
  50. //评论分页信息
  51. page.setPath("/discuss/detail/"+discussPostId);
  52. page.setLimit(5);
  53. page.setRows(post.getCommentCount());
  54. //评论:给帖子的评论
  55. //回复:给评论的评论
  56. //评论列表
  57. List commentList = commentService.selectCommentByEntity(ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
  58. //评论VO列表
  59. List<Map<String,Object>> commentVoList=new ArrayList<>();
  60. if (commentList!=null){
  61. for (Comment comment : commentList) {
  62. //评论VO
  63. Map commentVo=new HashMap<>();
  64. //评论
  65. commentVo.put("comment",comment);
  66. //作者
  67. commentVo.put("user",userService.selectById(comment.getUserId()));
  68. //回复列表
  69. List replayList = commentService.selectCommentByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
  70. //回复VO列表
  71. List<Map<String,Object>> replayVoList=new ArrayList<>();
  72. if (replayList!=null){
  73. for (Comment replay : replayList) {
  74. Map replayVo=new HashMap<>();
  75. //回复
  76. replayVo.put("replay",replay);
  77. //作者
  78. replayVo.put("user",userService.selectById(replay.getUserId()));
  79. //回复目标
  80. User target=replay.getTargetId()==0?null:userService.selectById(replay.getTargetId());
  81. replayVo.put("target",target);
  82. replayVoList.add(replayVo);
  83. }
  84. }
  85. commentVo.put("replays",replayVoList);
  86. //回复数量
  87. int replayCount = commentService.selectCountByEntity(ENTITY_TYPE_COMMENT, comment.getId());
  88. commentVo.put("replayCount",replayCount);
  89. commentVoList.add(commentVo);
  90. }
  91. }
  92. model.addAttribute("comments",commentVoList);
  93. return "/site/discuss-detail";
  94. }
  95. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/572924
推荐阅读
相关标签
  

闽ICP备14008679号