赞
踩
帖子详情
在帖子标题上增加访问帖子的链接
4、帖子详情/detail/{discussPostId}:在帖子标题处,添加href标签,进入帖子详情,restful风格,后台可以根据@PathVariable("discussPostId")直接获取帖子id,进行查询,加到model中,返回帖子页面
5、版主可设置置顶加精
6、管理员可以进行删除和统计日活
7、使用卡夫卡进行系统通知
--------------------------------------------------------------------------------------------------------------------------------
1.CommentMapper:
- import com.nowcoder.community.entity.Comment;
- import org.springframework.stereotype.Repository;
-
- import java.util.List;
-
- @Repository
- public interface CommentMapper {
- List selectCommentByEntity(int entityType,int entityId,int offset,int limit);
-
- int selectCountByEntity(int entityType,int entityId);
-
- int insertComment(Comment comment);
- }
2.编写CommentMapper.xml:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.nowcoder.community.mapper.CommentMapper">
- <select id="selectCommentByEntity" resultType="comment">
- select * from comment where status=0 and entity_type=#{entityType} and entity_id=#{entityId} order by create_time asc limit #{offset},#{limit};
- </select>
-
- <select id="selectCountByEntity" resultType="int">
- select count(id) from comment where status=0 and entity_type=#{entityType} and entity_id=#{entityId};
- </select>
-
- <insert id="insertComment" keyProperty="id" parameterType="Comment">
- insert into comment(user_id, entity_type, entity_id, target_id, content, status, create_time) VALUES (#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime});
- </insert>
- </mapper>
3.CommentService:
- import com.nowcoder.community.entity.Comment;
- import com.nowcoder.community.mapper.CommentMapper;
- import com.nowcoder.community.util.CommunityConstant;
- import com.nowcoder.community.util.SensitiveFilter;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Isolation;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.util.HtmlUtils;
-
- import java.util.List;
-
- @Service
- public class CommentServiceImpl implements CommentService, CommunityConstant {
-
- @Autowired
- private CommentMapper commentMapper;
-
- @Autowired
- private SensitiveFilter sensitiveFilter;
-
- @Autowired
- private DiscussPostServiceImpl discussPostService;
-
- @Override
- public List selectCommentByEntity(int entityType, int entityId, int offset, int limit) {
- return commentMapper.selectCommentByEntity(entityType, entityId, offset, limit);
- }
-
- @Override
- public int selectCountByEntity(int entityType, int entityId) {
- return commentMapper.selectCountByEntity(entityType, entityId);
- }
-
- @Override
- public int insertComment(Comment comment) {
- return commentMapper.insertComment(comment);
- }
4.DiscussPostController:
- import com.nowcoder.community.entity.Comment;
- import com.nowcoder.community.entity.DiscussPost;
- import com.nowcoder.community.entity.Page;
- import com.nowcoder.community.entity.User;
- import com.nowcoder.community.service.CommentServiceImpl;
- import com.nowcoder.community.service.DiscussPostServiceImpl;
- import com.nowcoder.community.service.UserServiceImpl;
- import com.nowcoder.community.util.CommunityConstant;
- import com.nowcoder.community.util.CommunityUtil;
- import com.nowcoder.community.util.HostHolder;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.*;
-
- @Controller
- @RequestMapping("/discuss")
- public class DiscussPostController implements CommunityConstant {
- @Autowired
- private DiscussPostServiceImpl discussPostService;
-
- @Autowired
- private HostHolder hostHolder;
-
- @Autowired
- private UserServiceImpl userService;
-
- @Autowired
- private CommentServiceImpl commentService;
-
- @PostMapping("/add")
- @ResponseBody
- public String addDiscussPost(String title,String content){
- User user = hostHolder.getUser();
- if (user==null){
- return CommunityUtil.getJSONString(403,"你还没有登陆");
- }
-
- DiscussPost discussPost = new DiscussPost();
- discussPost.setUserId(user.getId());
- discussPost.setTitle(title);
- discussPost.setContent(content);
- discussPost.setCreateTime(new Date());
-
- discussPostService.addDiscussPost(discussPost);
-
- return CommunityUtil.getJSONString(0,"发布成功");
- }
-
- @GetMapping("/detail/{discussPostId}")
- public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page){
- //帖子
- DiscussPost post = discussPostService.selectDiscussPostById(discussPostId);
- model.addAttribute("post",post);
-
- //作者
- User user = userService.selectById(post.getUserId());
- model.addAttribute("user",user);
-
- //评论分页信息
- page.setPath("/discuss/detail/"+discussPostId);
- page.setLimit(5);
- page.setRows(post.getCommentCount());
-
- //评论:给帖子的评论
- //回复:给评论的评论
-
- //评论列表
- List commentList = commentService.selectCommentByEntity(ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
- //评论VO列表
- List<Map<String,Object>> commentVoList=new ArrayList<>();
- if (commentList!=null){
- for (Comment comment : commentList) {
- //评论VO
- Map commentVo=new HashMap<>();
- //评论
- commentVo.put("comment",comment);
- //作者
- commentVo.put("user",userService.selectById(comment.getUserId()));
-
- //回复列表
- List replayList = commentService.selectCommentByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
- //回复VO列表
- List<Map<String,Object>> replayVoList=new ArrayList<>();
- if (replayList!=null){
- for (Comment replay : replayList) {
- Map replayVo=new HashMap<>();
- //回复
- replayVo.put("replay",replay);
- //作者
- replayVo.put("user",userService.selectById(replay.getUserId()));
- //回复目标
- User target=replay.getTargetId()==0?null:userService.selectById(replay.getTargetId());
- replayVo.put("target",target);
-
- replayVoList.add(replayVo);
- }
- }
- commentVo.put("replays",replayVoList);
-
- //回复数量
- int replayCount = commentService.selectCountByEntity(ENTITY_TYPE_COMMENT, comment.getId());
- commentVo.put("replayCount",replayCount);
-
- commentVoList.add(commentVo);
- }
- }
- model.addAttribute("comments",commentVoList);
-
- return "/site/discuss-detail";
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。