Bullock Community Platform Project Show Comments/Add Comments

Dao

@Mapper
public interface CommentMapper {
    /**
     * Query comments by entity Query by post/comment/...
     * @param entityType
     * @param entityId
     * @param limit      Line Number Limit
     * @return
     */
    List<CommentMapper> selectCommentsByEntity(int entityType ,int entityId,int limit);

    /**
     * Query number of comments based on entity
     * @param entityType
     * @param entityId
     * @return
     */
    int selectCountByEntity(int entityType ,int entityId);
}

Service

@Service
public class CommentService {
    
    @Autowired
    private CommentMapper commentMapper;
    
    public List<Comment> findCommentsByEntity(int entityType,int entityId,int offset,int limit){
        return commentMapper.selectCommentsByEntity(entityType,entityId,offset,limit);
    }
    
    public int findCommentCount(int entityType,int entityId){
        return commentMapper.selectCountByEntity(entityType,entityId);
    }
}

Controller

Define entity type constants in CommunityConstant.java:

/**
     * Entity Type: Post
     */
    int ENTITY_TYPE_POST = 1;

    /**
     * Entity Type: Comments
     */
    int ENTITY_TYPE_COMMENT = 2;
/**
     *Query post details
     *
     * Page Parameter marshalling conditions for receiving paging
     * If an entity type exists in the parameter, MVC will save the entity type in Model and get the Page from Model on the page
     */
    @RequestMapping(path = "/detail/{discussPostId}" ,method = RequestMethod.GET)
    public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page){
        //Find posts
        DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
        model.addAttribute("post",post);
        //Query Author
        User user = userService.findUserById(post.getUserId());
        model.addAttribute("user",user);

        //Comment Paging Information
        page.setLimit(5); // 5 comments per page
        //set up path
        page.setPath("/discuss/detail/"+discussPostId);
        //Total number of responses
        page.setRows(post.getCommentCount());

        //Define Comments: All Comment Responses to a Post: Comments for each Comment

        //Get all the comments on this post
        List<Comment> commentList = commentService.findCommentsByEntity(ENTITY_TYPE_POST,post.getId(),page.getOffset(),page.getLimit());

        //Find the user information for each comment Vo:ViewObject to display
        //All comments on a post Vo:
        List<Map<String,Object>> commentVoList = new ArrayList<>();

        if(commentVoList != null){
            //For each comment
            for(Comment comment:commentList){
                //All the followers of a comment Vo:
                Map<String,Object> commentVo = new HashMap<>();
                //Show author information, comments, and postings to the page:

                //comment
                commentVo.put("comment",comment);
                //Author Information
                commentVo.put("user",userService.findUserById(comment.getUserId()));

                //Query follower Comment list
                List<Comment> replyList = commentService.findCommentsByEntity(ENTITY_TYPE_COMMENT,comment.getId(),0,Integer.MAX_VALUE);

                //Following VO List
                List<Map<String,Object>> replyVoList = new ArrayList<>();
                if(replyList != null){
                    for(Comment reply : replyList){
                        Map<String,Object> replyVo = new HashMap<>();
                        //Reply
                        replyVo.put("reply",reply);
                        //author
                        replyVo.put("user",userService.findUserById(reply.getUserId()));
                        //Goal to Reply
                        User target = reply.getTargetId() == 0 ? null :userService.findUserById(reply.getTargetId());
                        replyVo.put("target",target);

                        replyVoList.add(replyVo);
                    }
                }
                commentVo.put("replys",replyVoList);

                //Number of replies
                int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT,comment.getId());
                commentVo.put("replyCount",replyCount);

                commentVoList.add(commentVo);
            }
        }

        model.addAttribute("comments",commentVoList);

        return "/site/discuss-detail";
    }

Contents of Comment table:

Logic:
1. According to the parameters passed in: post ID, query post entity and sender information, pass in model
2. Set paging information on Page objects (number of comments per page, query path, total replies)
3. Query all comments on the post and save them in the List
4,

Walk through this List for each comment:
    1. Add comments and author information to commentVo
    2. Find a list of comments on posts through Targeted and create a replyVoList to respond to each post:
            1. Create replyVo
            2. Add text information
            3. Find user information, join
            4. User information to join the reply (page will show: reply xxx)
            5. Add ReplyVo to List
    3. Add posting information to commentVo
    4. Add the number of replies to commentVo
    5. Add commentVo to commentVoList

Add a comment

DAO

Add comment data

/**
     * Insert Comment
     * @param comment
     * @return
     */
    int insertComment(Comment comment);
<insert id="insertComment" parameterType="Comment">
        insert into comment(<include refid="insertFields"></include>)
        values(#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime})
</insert>

Number of comments to modify a post
Update the number of comments field for the corresponding entry in the post table after adding comments (this data is redundant for query convenience)

/**
     * Number of Update Comments
     * @param id
     * @param commentcount
     * @return
     */
    int updateCommentCount(int id,int commentcount);
<update id="updateCommentCount">
        update discuss_post set comment_count = #{commentCount} where id = #{id}
</update>

Service

Handle business adding comments
Add comments before updating the number of comments on a post: Include two steps, update two tables, so declare a transaction:
1. Escape and desensitize the text contents of Comment objects and insert comments
2. Query the number of comments based on two fields of Comment object: reply subject id and reply subject content.Updating CommentCount field of replied object

@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public int addComment(Comment comment){
        if(comment == null){
            throw new IllegalArgumentException("Parameter cannot be empty");
        }
        //Filter comment sensitive words, HTML escape, insert comment table
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        int rows = commentMapper.insertComment(comment);

        //Update the number of comments on posts
        if(comment.getEntityType()==ENTITY_TYPE_POST){
            //Query the number of comments based on the type of entity being commented on, ID
            int count = commentMapper.selectCountByEntity(comment.getEntityType(),comment.getEntityId());
            discussPostService.updateCommentCount(comment.getEntityId(),count);
        }
        return rows;
    }

Controller

Handle requests to add comment data
//Here you need to get the replied post ID through the URI to redirect the reply page later for comments

@RequestMapping(path = "/add/{discussPostId}",method = RequestMethod.POST)
    public String addMethod(@PathVariable("discussPostId") int discussPostId, Comment comment){
        //Set commenter id, status, date
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        //Insert Comment
        commentService.addComment(comment);
        
        //Redirect to the commented post details page
        return "redirect:/discuss/detail/"+discussPostId;
    }
Thirty-eight original articles were published, 1 was praised, and 740 were visited
Private letter follow

Tags: Java

Posted on Sat, 14 Mar 2020 22:10:56 -0400 by pkallberg21