调整评论的接口

This commit is contained in:
禾几海
2020-05-27 16:30:17 +08:00
parent 260ae53c8d
commit 2ef9073650
7 changed files with 35 additions and 22 deletions

View File

@@ -47,8 +47,7 @@ CREATE TABLE `comment`
`co_status` tinyint not null default 0 comment '评论的状态', `co_status` tinyint not null default 0 comment '评论的状态',
`co_pid` bigint not null default -1 comment '评论/留言的父id', `co_pid` bigint not null default -1 comment '评论/留言的父id',
`co_from_author_id` int not null comment '留言者id', `co_from_author_id` int not null comment '留言者id',
`co_to_author_id` int default null comment '父评论的作者id', `co_to_author_id` int default null comment '父评论的作者id'
`is_delete` boolean not null default false comment '该数据是否被删除'
) DEFAULT CHARSET = utf8mb4 ) DEFAULT CHARSET = utf8mb4
COLLATE utf8mb4_general_ci,comment '评论/留言表'; COLLATE utf8mb4_general_ci,comment '评论/留言表';

View File

@@ -1,15 +1,23 @@
package cn.celess.blog.enmu; package cn.celess.blog.enmu;
import lombok.Getter;
/** /**
* @Author: 小海 * @Author: 小海
* @Date: 2020-05-25 08:58 * @Date: 2020-05-25 08:58
* @Desc: * @Desc:
*/ */
@Getter
public enum CommentStatusEnum { public enum CommentStatusEnum {
// 正常 // 正常
NORMAL(0, "正常"); NORMAL(0, "正常"),
DELETED(3, "已删除");
private final int code;
private final String msg;
CommentStatusEnum(int code, String msg) { CommentStatusEnum(int code, String msg) {
this.code = code;
this.msg = msg;
} }
} }

View File

@@ -30,5 +30,5 @@ public class Comment {
*/ */
private Long pid; private Long pid;
private boolean delete; // private boolean delete;
} }

View File

@@ -36,7 +36,7 @@ public interface CommentMapper {
List<Comment> findAllByPagePathAndFromUser(String pagePath, long userId); List<Comment> findAllByPagePathAndFromUser(String pagePath, long userId);
List<Comment> findAllByPagePathAndPid(String pagePath, long pid); List<Comment> findAllByPagePathAndPidAndNormal(String pagePath, long pid);
long countByPagePath(String pagePath); long countByPagePath(String pagePath);

View File

@@ -1,5 +1,6 @@
package cn.celess.blog.service.serviceimpl; package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.CommentStatusEnum;
import cn.celess.blog.enmu.ResponseEnum; import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.Comment; import cn.celess.blog.entity.Comment;
import cn.celess.blog.entity.User; import cn.celess.blog.entity.User;
@@ -76,7 +77,7 @@ public class CommentServiceImpl implements CommentService {
if (b == null) { if (b == null) {
throw new MyException(ResponseEnum.COMMENT_NOT_EXIST); throw new MyException(ResponseEnum.COMMENT_NOT_EXIST);
} }
if (b.isDelete()) { if (b.getStatus() == CommentStatusEnum.DELETED.getCode()) {
throw new MyException(ResponseEnum.DATA_IS_DELETED); throw new MyException(ResponseEnum.DATA_IS_DELETED);
} }
commentMapper.delete(id); commentMapper.delete(id);
@@ -99,7 +100,7 @@ public class CommentServiceImpl implements CommentService {
@Override @Override
public PageData<CommentModel> retrievePage(String pagePath, int page, int count) { public PageData<CommentModel> retrievePage(String pagePath, int page, int count) {
PageHelper.startPage(page, count); PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, -1); List<Comment> list = commentMapper.findAllByPagePathAndPidAndNormal(pagePath, -1);
return pageTrans(list); return pageTrans(list);
} }
@@ -107,7 +108,12 @@ public class CommentServiceImpl implements CommentService {
public List<CommentModel> retrievePageByPid(long pid) { public List<CommentModel> retrievePageByPid(long pid) {
List<Comment> allByPagePath = commentMapper.findAllByPid(pid); List<Comment> allByPagePath = commentMapper.findAllByPid(pid);
List<CommentModel> commentModels = new ArrayList<>(); List<CommentModel> commentModels = new ArrayList<>();
allByPagePath.forEach(comment -> commentModels.add(ModalTrans.comment(comment))); allByPagePath.forEach(comment -> {
if (comment.getStatus() != CommentStatusEnum.DELETED.getCode()) {
commentModels.add(ModalTrans.comment(comment));
}
});
return commentModels; return commentModels;
} }

View File

@@ -10,7 +10,6 @@
<result column="co_pid" property="pid"/> <result column="co_pid" property="pid"/>
<result column="co_from_author_id" property="fromUser.id"/> <result column="co_from_author_id" property="fromUser.id"/>
<result column="co_to_author_id" property="toUser.id"/> <result column="co_to_author_id" property="toUser.id"/>
<result column="is_delete" property="delete"/>
</resultMap> </resultMap>
<resultMap id="commentViewResultMap" type="cn.celess.blog.entity.Comment"> <resultMap id="commentViewResultMap" type="cn.celess.blog.entity.Comment">
@@ -22,7 +21,6 @@
<result column="pid" property="pid"/> <result column="pid" property="pid"/>
<result column="fromAuthorId" property="fromUser.id"/> <result column="fromAuthorId" property="fromUser.id"/>
<result column="toAuthorId" property="toUser.id"/> <result column="toAuthorId" property="toUser.id"/>
<result column="isDelete" property="delete"/>
<association property="fromUser" column="fromAuthorId" javaType="cn.celess.blog.entity.User"> <association property="fromUser" column="fromAuthorId" javaType="cn.celess.blog.entity.User">
<id column="fromAuthorId" property="id"/> <id column="fromAuthorId" property="id"/>
<result column="fromAuthorEmail" property="email"/> <result column="fromAuthorEmail" property="email"/>
@@ -51,13 +49,13 @@
<update id="delete"> <update id="delete">
update comment update comment
set is_delete = true set co_status = 3
where co_id = #{id} where co_id = #{id}
</update> </update>
<update id="deleteByPagePath"> <update id="deleteByPagePath">
update comment update comment
set is_delete = true set co_status = 3
where co_page_path = #{path} where co_page_path = #{path}
</update> </update>
@@ -83,10 +81,11 @@
where pid = #{pid} where pid = #{pid}
</select> </select>
<select id="findAllByPagePath" resultMap="commentViewResultMap"> <select id="findAllByPagePath" resultMap="commentViewResultMap" parameterType="string">
select * select *
from commentView from commentView
<if test="pagePath!= null "> # 无奈之举
<if test="toString() != null ">
where pagePath = #{pagePath} where pagePath = #{pagePath}
</if> </if>
</select> </select>
@@ -95,20 +94,20 @@
select * select *
from commentView from commentView
where where
<if test="pagePath!= null "> <if test="pagePath != null ">
pagePath = #{pagePath} and pagePath = #{pagePath} and
</if> </if>
fromAuthorId = #{userId} fromAuthorId = #{userId}
</select> </select>
<select id="findAllByPagePathAndPid" resultMap="commentViewResultMap"> <select id="findAllByPagePathAndPidAndNormal" resultMap="commentViewResultMap">
select * select *
from commentView from commentView
where where
<if test="pagePath!= null "> <if test="pagePath != null ">
pagePath = #{pagePath} and pagePath = #{pagePath} and
</if> </if>
pid = #{pid} pid = #{pid} and status = 0
</select> </select>
<select id="countByPagePath" resultType="java.lang.Long"> <select id="countByPagePath" resultType="java.lang.Long">
@@ -127,7 +126,7 @@
<select id="count" resultType="long"> <select id="count" resultType="long">
select count(*) select count(*)
from comment from comment
where is_delete = false; where co_status = 0;
</select> </select>
</mapper> </mapper>

View File

@@ -1,6 +1,7 @@
package cn.celess.blog.mapper; package cn.celess.blog.mapper;
import cn.celess.blog.BaseTest; import cn.celess.blog.BaseTest;
import cn.celess.blog.enmu.CommentStatusEnum;
import cn.celess.blog.entity.Comment; import cn.celess.blog.entity.Comment;
import cn.celess.blog.entity.User; import cn.celess.blog.entity.User;
import org.junit.Test; import org.junit.Test;
@@ -35,7 +36,7 @@ public class CommentMapperTest extends BaseTest {
Comment comment = generateComment(); Comment comment = generateComment();
assertEquals(1, commentMapper.delete(comment.getId())); assertEquals(1, commentMapper.delete(comment.getId()));
Comment commentById = commentMapper.findCommentById(comment.getId()); Comment commentById = commentMapper.findCommentById(comment.getId());
assertTrue(commentById.isDelete()); assertEquals(commentById.getStatus(), CommentStatusEnum.DELETED.getCode());
} }
@Test @Test
@@ -43,7 +44,7 @@ public class CommentMapperTest extends BaseTest {
Comment comment = generateComment(); Comment comment = generateComment();
assertTrue(commentMapper.deleteByPagePath(comment.getPagePath()) >= 1); assertTrue(commentMapper.deleteByPagePath(comment.getPagePath()) >= 1);
Comment commentById = commentMapper.findCommentById(comment.getId()); Comment commentById = commentMapper.findCommentById(comment.getId());
assertTrue(commentById.isDelete()); assertEquals(commentById.getStatus(), CommentStatusEnum.DELETED.getCode());
} }
@Test @Test
@@ -91,7 +92,7 @@ public class CommentMapperTest extends BaseTest {
@Test @Test
public void findAllByPagePathAndPid() { public void findAllByPagePathAndPid() {
Comment comment = generateComment(); Comment comment = generateComment();
List<Comment> allByPagePathAndPid = commentMapper.findAllByPagePathAndPid(comment.getPagePath(), comment.getPid()); List<Comment> allByPagePathAndPid = commentMapper.findAllByPagePathAndPidAndNormal(comment.getPagePath(), comment.getPid());
assertTrue(allByPagePathAndPid.size() >= 1); assertTrue(allByPagePathAndPid.size() >= 1);
allByPagePathAndPid.forEach(comment1 -> { allByPagePathAndPid.forEach(comment1 -> {
assertEquals(comment.getPagePath(), comment1.getPagePath()); assertEquals(comment.getPagePath(), comment1.getPagePath());