3.6 添加评论

目录

数据层

CommentMapper

DiscussPostMapper

业务层

DiscussPostService

CommentService

表现层

CommentController

discuss-detail.html

给帖子评论(entityid为postid)

给评论回复(entityid为commentid)

给回复回复(回复给某个人,entityid为commentid, 但需要额外传递targetId(userid))


 

 在一个业务里面涉及到了两个数据库操作,为了数据的完整性,需要使用事务管理

数据层

CommentMapper

    int insertComment(Comment comment)
    <sql id="insertFields">
        user_id, entity_type, entity_id, target_id, content, status, create_time
    </sql>
    <insert id="insertComment" parameterType="Comment">
        insert into comment(<include refid="insertFields"></include>)
        values (#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime})
    </insert>

DiscussPostMapper

    int updateCommentCount(int id, int commentCount);
    <update id="updateCommentCount">
        update discuss_post set comment_count = #{commentCount} where id = #{id}
    </update>

业务层

DiscussPostService

    public int updateCommentCount(int id, int commentCount){
        DiscussPost discussPost = discussPostMapper.selectDiscussPostById(id);
        return discussPostMapper.updateCommentCount(discussPost.getId(), commentCount);
    }

CommentService

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public int addComment(Comment comment){
        if(comment == null){
            throw  new IllegalArgumentException("参数不能为空");
        }
        //添加评论
        //先过滤标签,再过滤敏感词
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        int rows = commentMapper.insertComment(comment);

        //更新帖子评论数量,注意是帖子,如果是评论的话不需要进行修改
        if(comment.getEntityType() ==ENTITY_TYPE_POST){
            int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
            discussPostService.updateCommentCount(comment.getEntityId(),count + 1);
        }
        return rows;
    }

表现层

CommentController

@RequestMapping(path="/add/{discussPostId}", method = RequestMethod.POST)
public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
    //补全comment的数据
    comment.setUserId(hostHolder.getUser().getId());
    comment.setStatus(0);
    comment.setCreateTime(new Date());
    commentService.addComment(comment);
    //重定向到帖子详情页面
    return "redirect:/discuss/detail/" + discussPostId;
}

discuss-detail.html

给帖子评论(entityid为postid)

<div class="container mt-3">
	<form class="replyform" method="post" th:action="@{|/comment/add/${post.id}|}">
		<p class="mt-3">
			<a name="replyform"></a>
			<textarea placeholder="在这里畅所欲言你的看法吧!" name="content"></textarea>
			<input type="hidden" name="entityType" value="1">
			<input type="hidden" name="entityId" th:value="${post.id}">
		</p>
		<p class="text-right">
			<button type="submit" class="btn btn-primary btn-sm">&nbsp;&nbsp;回&nbsp;&nbsp;帖&nbsp;&nbsp;</button>
		</p>
	</form>
</div>

需要隐含的传递两个参数值,实体类型和实体id(利用隐藏框)

给评论回复(entityid为commentid)

<!-- 回复输入框 -->
<li class="pb-3 pt-3">
	<form method="post" th:action="@{|/comment/add/${post.id}|}">
		<div>
			<input type="text" class="input-size" name="content" placeholder="请输入你的观点"/>
			<input type="hidden" name="entityType" value="2">
			<input type="hidden" name="entityId" th:value="${cvo.comment.id}">
		</div>
		<div class="text-right mt-2">
			<button type="submit" class="btn btn-primary btn-sm">&nbsp;&nbsp;回&nbsp;&nbsp;复&nbsp;&nbsp;</button>
		</div>
	</form>
</li>

给回复回复(回复给某个人,entityid为commentid, 但需要额外传递targetId(userid))

<div th:id="|huifu-${rvoStat.count}|" class="mt-4 collapse">
	<form method="post" th:action="@{|/comment/add/${post.id}|}">
		<div>
			<input type="text" class="input-size" name="content" th:placeholder="@{|回复${rvo.user.username}|}"/>
			<input type="hidden" name="entityType" value="2">
			<input type="hidden" name="entityId" th:value="${cvo.comment.id}">
			<input type="hidden" name="targetId" th:value="${rvo.user.id}">
		</div>
		<div class="text-right mt-2">
			<button type="submit" class="btn btn-primary btn-sm" >&nbsp;&nbsp;回&nbsp;&nbsp;复&nbsp;&nbsp;</button>
		</div>
	</form>
</div>

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值