diff --git a/src/main/java/cn/celess/blog/mapper/CommentMapper.java b/src/main/java/cn/celess/blog/mapper/CommentMapper.java index 4b0bd7f..141bc5f 100644 --- a/src/main/java/cn/celess/blog/mapper/CommentMapper.java +++ b/src/main/java/cn/celess/blog/mapper/CommentMapper.java @@ -8,8 +8,8 @@ import java.util.List; /** * @Author: 小海 - * @Date: 2019/06/30 16:19 - * @Description: + * @Date: 2019/06/30 16:19 + * @Description: */ @Mapper @Repository @@ -18,11 +18,9 @@ public interface CommentMapper { int updateContent(String content, long id); - int updateResponder(String responder, long id); - int delete(long id); - int deleteByArticleId(long articleId); + int deleteByPagePath(String pagePath); boolean existsById(long id); @@ -30,19 +28,13 @@ public interface CommentMapper { Comment getLastestComment(); - List findAllByAuthorIDAndType(long id, boolean isComment); + List findAllByFromUser(long id); - List findAllByPId(long pid); + List findAllByPid(long pid); - List findAllByArticleID(long articleId); + List findAllByPagePath(String pagePath); - List findAllByArticleIDAndPId(long articleID, long pid); + List findAllByPagePathAndPid(String pagePath, long pid); - List findCommentsByTypeAndPId(boolean isComment, long pid); - - List findAllByPId(int pid); - - List findAllByType(boolean isComment); - - long countByType(boolean isComment); + long countByPagePath(String pagePath); } diff --git a/src/main/resources/mapper/CommentMapper.xml b/src/main/resources/mapper/CommentMapper.xml index 94c9973..89037f1 100644 --- a/src/main/resources/mapper/CommentMapper.xml +++ b/src/main/resources/mapper/CommentMapper.xml @@ -3,21 +3,44 @@ - - - + + - + + + - - insert into comment (co_article_id, is_comment, author_id, co_content, co_date, co_pid) - VALUES (#{articleID}, #{type}, #{authorID}, #{content}, #{date}, #{pid}) - - SELECT LAST_INSERT_ID() AS id - + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into comment (co_page_path, co_content, co_date, co_pid, co_from_author_id, co_to_author_id, is_delete) + VALUES (#{pagePath}, #{content}, now(), #{pid}, #{fromUser.id}, #{toUser.id}, false) @@ -26,71 +49,62 @@ where co_id = #{id} - + update comment - set co_response_id =#{responder} + set is_delete = true where co_id = #{id} - - delete - from comment - where co_id = #{id} - - - delete - from comment - where co_article_id = #{articleId} - + + + update comment + set is_delete = true + where co_page_path = #{path} + + - select * - from comment - where co_id = #{id} + from commentView + where commentId = #{id} - select * - from comment - where author_id = #{id} - and is_comment = #{isComment} + from commentView + where fromAuthorId = #{id} - select * - from comment - where co_pid = #{pid} + from commentView + where pid = #{pid} - select * - from comment - where co_article_id = #{articleId} + from commentView + where pagePath = #{pagePath} - select * - from comment - where co_article_id = #{articleID} - and co_pid = #{pid} + from commentView + where pagePath = #{pagePath} + and pid = #{pid} - - - select count(*) - from comment - where is_comment = #{isComment} + from commentView + where pagePath = #{pagePath} - select * - from comment - order by co_id desc + from commentView + order by commentId desc limit 1 diff --git a/src/test/java/cn/celess/blog/mapper/CommentMapperTest.java b/src/test/java/cn/celess/blog/mapper/CommentMapperTest.java new file mode 100644 index 0000000..3c78167 --- /dev/null +++ b/src/test/java/cn/celess/blog/mapper/CommentMapperTest.java @@ -0,0 +1,132 @@ +package cn.celess.blog.mapper; + +import cn.celess.blog.BaseTest; +import cn.celess.blog.entity.Comment; +import cn.celess.blog.entity.User; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +import static org.junit.Assert.*; + +public class CommentMapperTest extends BaseTest { + + @Autowired + UserMapper userMapper; + @Autowired + CommentMapper commentMapper; + + @Test + public void insert() { + Comment comment = generateComment(); + assertNotNull(comment.getId()); + } + + @Test + public void updateContent() { + Comment comment = generateComment(); + comment.setContent(randomStr(10)); + assertEquals(1, commentMapper.updateContent(comment.getContent(), comment.getId())); + } + + @Test + public void delete() { + Comment comment = generateComment(); + assertEquals(1, commentMapper.delete(comment.getId())); + Comment commentById = commentMapper.findCommentById(comment.getId()); + assertTrue(commentById.isDelete()); + } + + @Test + public void deleteByPagePath() { + Comment comment = generateComment(); + assertTrue(commentMapper.deleteByPagePath(comment.getPagePath()) >= 1); + Comment commentById = commentMapper.findCommentById(comment.getId()); + assertTrue(commentById.isDelete()); + } + + @Test + public void existsById() { + Comment comment = generateComment(); + assertTrue(commentMapper.existsById(comment.getId())); + } + + @Test + public void findCommentById() { + Comment comment = generateComment(); + assertNotNull(commentMapper.findCommentById(comment.getId())); + } + + @Test + public void getLastestComment() { + Comment comment = generateComment(); + Comment lastestComment = commentMapper.getLastestComment(); + assertEquals(comment.getId(), lastestComment.getId()); + } + + @Test + public void findAllByFromUser() { + Comment comment = generateComment(); + List allByFromUser = commentMapper.findAllByFromUser(comment.getFromUser().getId()); + assertNotEquals(0, allByFromUser); + allByFromUser.forEach(comment1 -> assertEquals(comment.getFromUser().getId(), comment1.getFromUser().getId())); + } + + @Test + public void findAllByPid() { + Comment comment = generateComment(); + List allByPid = commentMapper.findAllByPid(comment.getPid()); + assertTrue(allByPid.size() >= 1); + } + + @Test + public void findAllByPagePath() { + Comment comment = generateComment(); + List allByPagePath = commentMapper.findAllByPagePath(comment.getPagePath()); + assertTrue(allByPagePath.size() >= 1); + allByPagePath.forEach(comment1 -> assertEquals(comment.getPagePath(), comment1.getPagePath())); + } + + @Test + public void findAllByPagePathAndPid() { + Comment comment = generateComment(); + List allByPagePathAndPid = commentMapper.findAllByPagePathAndPid(comment.getPagePath(), comment.getPid()); + assertTrue(allByPagePathAndPid.size() >= 1); + allByPagePathAndPid.forEach(comment1 -> { + assertEquals(comment.getPagePath(), comment1.getPagePath()); + assertEquals(comment.getPid(), comment1.getPid()); + }); + } + + @Test + public void testFindAllByPid() { + Comment comment = generateComment(); + List findAllByPid = commentMapper.findAllByPid(comment.getPid()); + assertTrue(findAllByPid.size() >= 1); + findAllByPid.forEach(comment1 -> assertEquals(comment.getPid(), comment1.getPid())); + } + + @Test + public void countByType() { + Comment comment = generateComment(); + long l = commentMapper.countByPagePath(comment.getPagePath()); + assertTrue(l >= 1); + } + + private Comment generateComment() { + User from = userMapper.findById(1); + assertNotNull(from); + User to = userMapper.findById(2); + assertNotNull(to); + + Comment comment = new Comment(); + comment.setContent(randomStr(8)); + comment.setFromUser(from); + comment.setToUser(to); + comment.setPagePath("/tags"); + comment.setPid(-1L); + commentMapper.insert(comment); + return comment; + } +} \ No newline at end of file