调整数据库字段,优化部分接口 #1

Merged
xiaohai2271 merged 33 commits from dev into master 2020-05-27 16:45:03 +08:00
7 changed files with 35 additions and 22 deletions
Showing only changes of commit 2ef9073650 - Show all commits

View File

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

View File

@@ -1,15 +1,23 @@
package cn.celess.blog.enmu;
import lombok.Getter;
/**
* @Author: 小海
* @Date: 2020-05-25 08:58
* @Desc:
*/
@Getter
public enum CommentStatusEnum {
// 正常
NORMAL(0, "正常");
NORMAL(0, "正常"),
DELETED(3, "已删除");
private final int code;
private final 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 boolean delete;
// private boolean delete;
}

View File

@@ -36,7 +36,7 @@ public interface CommentMapper {
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);

View File

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

View File

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

View File

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