From aa882406d0f7cccc9e3cee587e911ef8c8dfc3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Tue, 26 May 2020 12:34:35 +0800 Subject: [PATCH] =?UTF-8?q?Service,dao=E5=B1=82,=E8=A7=86=E5=9B=BE,?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E4=BF=AE=E6=94=B9=20=20=20=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- newBlog.sql | 64 +++-- .../blog/controller/CommentController.java | 60 ++--- .../cn/celess/blog/enmu/ResponseEnum.java | 3 +- .../blog/entity/model/CommentModel.java | 26 +- .../blog/entity/request/CommentReq.java | 7 +- .../cn/celess/blog/mapper/CommentMapper.java | 2 + .../celess/blog/service/CommentService.java | 47 ++-- .../serviceimpl/CommentServiceImpl.java | 155 +++++------ .../java/cn/celess/blog/util/ModalTrans.java | 24 ++ src/main/resources/mapper/CommentMapper.xml | 12 +- .../controller/CommentControllerTest.java | 245 +++--------------- 11 files changed, 219 insertions(+), 426 deletions(-) diff --git a/newBlog.sql b/newBlog.sql index bfaaafd..fa4cf14 100644 --- a/newBlog.sql +++ b/newBlog.sql @@ -137,30 +137,42 @@ where article.a_id = article_tag.a_id and category.is_category = true and article.a_author_id = user.u_id; -CREATE VIEW commentView - (commentId, pagePath, content, date, status, pid, - fromAuthorId, fromAuthorEmail, fromAuthorDisplayName, fromAuthorAvatar, toAuthorId, - toAuthorEmail, toAuthorDisplayName, toAuthorAvatar, isDelete - ) -as -select comment.co_id as commentId, - comment.co_page_path as pagePath, - comment.co_content as content, - comment.co_date as date, - comment.co_status as status, - comment.co_pid as pid, - comment.co_from_author_id as fromAuthorId, - userFrom.u_email as fromAuthorEmail, - userFrom.u_display_name as fromAuthorDisplayName, - userFrom.u_avatar as fromAuthorAvatar, - comment.co_to_author_id as toAuthorId, - userTo.u_email as toAuthorEmail, - userTo.u_display_name as toAuthorDisplayName, - userTo.u_avatar as toAuthorAvatar, - comment.is_delete as isDelete -from comment, - user as userFrom, - user as userTo -where comment.co_from_author_id = userFrom.u_id - and comment.co_to_author_id = userTo.u_id; + +CREATE VIEW commentView + (commentId, pagePath, content, date, status, pid, toAuthorId, toAuthorEmail, toAuthorDisplayName, + toAuthorAvatar, fromAuthorId, fromAuthorEmail, fromAuthorDisplayName, + fromAuthorAvatar, isDelete) +as +select cuT.co_id as commentId, + cuT.co_page_path as pagePath, + cuT.co_content as content, + cuT.co_date as date, + cuT.co_status as status, + cuT.co_pid as pid, + cuT.co_to_author_id as toAuthorId, + cuT.toEmail as toAuthorEmail, + cuT.toDisplayName as toAuthorDisplayName, + cuT.toAvatar as toAuthorAvatar, + userFrom.u_id as fromAuthorId, + userFrom.u_email as fromAuthorEmail, + userFrom.u_display_name as fromAuthorDisplayName, + userFrom.u_avatar as fromAuthorAvatar, + cuT.is_delete as isDelete +from (select comment.co_id, + comment.co_page_path, + comment.co_content, + comment.co_date, + comment.co_status, + comment.co_pid, + comment.co_from_author_id, + comment.co_to_author_id, + userTo.u_email as toEmail, + userTo.u_display_name as toDisplayName, + userTo.u_avatar as toAvatar, + comment.is_delete + from comment + left join user userTo on (comment.co_to_author_id = userTo.u_id) + ) as cuT, + user as userFrom +where cuT.co_from_author_id = userFrom.u_id; diff --git a/src/main/java/cn/celess/blog/controller/CommentController.java b/src/main/java/cn/celess/blog/controller/CommentController.java index dff5808..8234490 100644 --- a/src/main/java/cn/celess/blog/controller/CommentController.java +++ b/src/main/java/cn/celess/blog/controller/CommentController.java @@ -40,58 +40,40 @@ public class CommentController { } /** - * 获取所有的一级评论 + * 获取所有的评论 * - * @param articleId 文章id - * @param count 单页数据量 - * @param page 页码 + * @param pagePath pagePath + * @param count 单页数据量 + * @param page 页码 * @return Response */ @GetMapping("/comments") - public Response commentsOfArticle(@RequestParam("articleId") long articleId, + public Response commentsOfArticle(@RequestParam("pagePath") String pagePath, + @RequestParam(value = "pid", required = false, defaultValue = "-1") long pid, @RequestParam(value = "count", required = false, defaultValue = "10") int count, @RequestParam(value = "page", required = false, defaultValue = "1") int page) { - return ResponseUtil.success(commentService.retrievePageByArticle(articleId, -1, page, count)); + return ResponseUtil.success(commentService.retrievePageByPageAndPid(pagePath, pid, page, count)); } /** * 通过pid获取数据 * - * @param pid - * @return + * @param pagePath pagePath + * @param count count + * @param page page + * @return Response */ - @GetMapping("/comment/pid/{pid}") - public Response retrievePage(@PathVariable("pid") long pid) { - return ResponseUtil.success(commentService.retrievePageByPid(pid)); + @GetMapping("/comment/pagePath/{pagePath}") + public Response retrievePage(@PathVariable("pagePath") String pagePath, + @RequestParam(value = "count", required = false, defaultValue = "10") int count, + @RequestParam(value = "page", required = false, defaultValue = "1") int page) { + return ResponseUtil.success(commentService.retrievePage(pagePath, page, count)); } - /** - * 获取所以的一级留言 - * - * @param count - * @param page - * @return - */ - @GetMapping("/leaveMsg") - public Response retrievePageOfLeaveMsg(@RequestParam(value = "count", required = false, defaultValue = "10") int count, - @RequestParam(value = "page", required = false, defaultValue = "1") int page) { - return ResponseUtil.success(commentService.retrievePageByTypeAndPid(false, -1, page, count)); + @GetMapping("/user/comment/pagePath/{pagePath}") + public Response userComment(@PathVariable("pagePath") String pagePath, + @RequestParam(value = "count", required = false, defaultValue = "10") int count, + @RequestParam(value = "page", required = false, defaultValue = "1") int page) { + return ResponseUtil.success(commentService.retrievePageByAuthor(pagePath, page, count)); } - - @GetMapping("/admin/comment/type/{type}") - public Response retrievePageAdmin( - @PathVariable("type") int isComment, - @RequestParam(value = "count", required = false, defaultValue = "10") int count, - @RequestParam(value = "page", required = false, defaultValue = "1") int page) { - return ResponseUtil.success(commentService.retrievePageByType(1 == isComment, page, count)); - } - - @GetMapping("/user/comment/type/{type}") - public Response retrievePageByAuthor( - @PathVariable(value = "type") int isComment, - @RequestParam(value = "count", required = false, defaultValue = "10") int count, - @RequestParam(value = "page", required = false, defaultValue = "1") int page) { - return ResponseUtil.success(commentService.retrievePageByAuthor(1 == isComment, page, count)); - } - } diff --git a/src/main/java/cn/celess/blog/enmu/ResponseEnum.java b/src/main/java/cn/celess/blog/enmu/ResponseEnum.java index e26c48a..d61c5bf 100644 --- a/src/main/java/cn/celess/blog/enmu/ResponseEnum.java +++ b/src/main/java/cn/celess/blog/enmu/ResponseEnum.java @@ -11,6 +11,8 @@ public enum ResponseEnum { FAILURE(-1, "失败"), ERROR(-2, "错误"), + DATA_IS_DELETED(1000, "数据已被删除"), + //文章类 ARTICLE_NOT_EXIST(2010, "文章不存在"), ARTICLE_HAS_EXIST(2020, "文章已存在"), @@ -68,7 +70,6 @@ public enum ResponseEnum { PARAMETERS_QQ_ERROR(8540, "QQ格式错误"), PARAMETERS_PWD_ERROR(8550, "密码格式错误"), VERIFY_OUT(8400, "已经验证过了"); - private int code; private String msg; diff --git a/src/main/java/cn/celess/blog/entity/model/CommentModel.java b/src/main/java/cn/celess/blog/entity/model/CommentModel.java index 1fa68d6..b4541b6 100644 --- a/src/main/java/cn/celess/blog/entity/model/CommentModel.java +++ b/src/main/java/cn/celess/blog/entity/model/CommentModel.java @@ -1,5 +1,6 @@ package cn.celess.blog.entity.model; +import cn.celess.blog.entity.User; import lombok.Getter; import lombok.Setter; @@ -14,46 +15,31 @@ import java.util.List; public class CommentModel { private long id; - /** - * 是评论还是留言 0:评论 其他(1):留言 - */ - private boolean isComment; + private UserModel fromUser; - private String authorName; - - private String authorAvatarImgUrl; + private UserModel toUser; /** * 内容 */ private String content; - /** - * 文章ID - */ - private long articleID; - /** * 文章标题 */ - private String articleTitle; + private String pagePath; /** * 发布日期 */ private String date; - /** - * 回应着ID 默认为顶级回复 - */ - private String responseId = ""; - /** * 评论的父ID */ - private long pid = -1; + private Long pid; private List respComment; - + private int status; } diff --git a/src/main/java/cn/celess/blog/entity/request/CommentReq.java b/src/main/java/cn/celess/blog/entity/request/CommentReq.java index a310c30..c322326 100644 --- a/src/main/java/cn/celess/blog/entity/request/CommentReq.java +++ b/src/main/java/cn/celess/blog/entity/request/CommentReq.java @@ -9,9 +9,8 @@ import lombok.Data; @Data public class CommentReq { private Long id; - private Boolean comment; private String content; - private Long pid; - private Long articleID; - private String responseId; + private long pid = -1; + private String pagePath; + private long toUserId = -1; } diff --git a/src/main/java/cn/celess/blog/mapper/CommentMapper.java b/src/main/java/cn/celess/blog/mapper/CommentMapper.java index c79cd84..7169479 100644 --- a/src/main/java/cn/celess/blog/mapper/CommentMapper.java +++ b/src/main/java/cn/celess/blog/mapper/CommentMapper.java @@ -34,6 +34,8 @@ public interface CommentMapper { List findAllByPagePath(String pagePath); + List findAllByPagePathAndFromUser(String pagePath,long userId); + List findAllByPagePathAndPid(String pagePath, long pid); long countByPagePath(String pagePath); diff --git a/src/main/java/cn/celess/blog/service/CommentService.java b/src/main/java/cn/celess/blog/service/CommentService.java index d8a6841..3d5d560 100644 --- a/src/main/java/cn/celess/blog/service/CommentService.java +++ b/src/main/java/cn/celess/blog/service/CommentService.java @@ -1,6 +1,7 @@ package cn.celess.blog.service; import cn.celess.blog.entity.model.CommentModel; +import cn.celess.blog.entity.model.PageData; import cn.celess.blog.entity.request.CommentReq; import com.github.pagehelper.PageInfo; import org.springframework.stereotype.Service; @@ -40,17 +41,17 @@ public interface CommentService { /** * 分页获取数据 * - * @param isComment true:评论 false:留言 - * @param count 单页数据量 - * @param page 数据页 + * @param pagePath pagePath + * @param count 单页数据量 + * @param page 数据页 * @return 分页数据 */ - PageInfo retrievePage(Boolean isComment, int page, int count); + PageData retrievePage(String pagePath, int page, int count); /** * 通过pid获取数据 * - * @param pid 父id + * @param pid 父id * @return 分页数据 */ List retrievePageByPid(long pid); @@ -59,43 +60,33 @@ public interface CommentService { /** * 根据评论者获取数据 * - * @param isComment true:评论 false:留言 - * @param count 单页数据量 - * @param page 数据页 + * @param pagePath pagePath + * @param count 单页数据量 + * @param page 数据页 * @return 分页数据 */ - PageInfo retrievePageByAuthor(Boolean isComment, int page, int count); + PageData retrievePageByAuthor(String pagePath, int page, int count); - /** - * 根据文章获取数据 - * - * @param articleID 文章id - * @param pid 父id - * @param count 单页数据量 - * @param page 数据页 - * @return 分页数据 - */ - PageInfo retrievePageByArticle(long articleID, long pid, int page, int count); /** * 根据数据的type和pid获取数据 * - * @param isComment true:评论 false:留言 - * @param pid 父id - * @param count 单页数据量 - * @param page 数据页 + * @param pagePath pagePath + * @param pid 父id + * @param count 单页数据量 + * @param page 数据页 * @return 分页数据 */ - PageInfo retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count); + PageData retrievePageByPageAndPid(String pagePath, long pid, int page, int count); /** * 根据type获取数据 * - * @param isComment true:评论 false:留言 - * @param count 单页数据量 - * @param page 数据页 + * @param pagePath pagePath + * @param count 单页数据量 + * @param page 数据页 * @return 分页数据 */ - PageInfo retrievePageByType(Boolean isComment, int page, int count); + PageData retrievePageByPage(String pagePath, int page, int count); } diff --git a/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java b/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java index af9cedb..a8b6d2b 100644 --- a/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java +++ b/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java @@ -2,18 +2,22 @@ package cn.celess.blog.service.serviceimpl; import cn.celess.blog.enmu.ResponseEnum; import cn.celess.blog.entity.Comment; +import cn.celess.blog.entity.User; import cn.celess.blog.entity.model.CommentModel; +import cn.celess.blog.entity.model.PageData; import cn.celess.blog.entity.request.CommentReq; import cn.celess.blog.exception.MyException; import cn.celess.blog.mapper.ArticleMapper; import cn.celess.blog.mapper.CommentMapper; +import cn.celess.blog.mapper.UserMapper; import cn.celess.blog.service.CommentService; import cn.celess.blog.service.UserService; import cn.celess.blog.util.DateFormatUtil; +import cn.celess.blog.util.ModalTrans; import cn.celess.blog.util.RedisUserUtil; -import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,7 +35,7 @@ public class CommentServiceImpl implements CommentService { @Autowired CommentMapper commentMapper; @Autowired - UserService userService; + UserMapper userMapper; @Autowired ArticleMapper articleMapper; @Autowired @@ -41,52 +45,43 @@ public class CommentServiceImpl implements CommentService { @Override public CommentModel create(CommentReq reqBody) { - if (reqBody == null) { throw new MyException(ResponseEnum.PARAMETERS_ERROR); } - long authorID = redisUserUtil.get().getId(); + User user = redisUserUtil.get(); Comment pComment = null; - if (reqBody.getPid() != null && reqBody.getPid() != -1) { + if (reqBody.getPid() != -1) { pComment = commentMapper.findCommentById(reqBody.getPid()); } - if (reqBody.getPid() == null) { - reqBody.setPid(-1L); - } //不是一级评论 if (reqBody.getPid() != -1 && pComment == null) { //父评论不存在 throw new MyException(ResponseEnum.PARAMETERS_ERROR); } Comment comment = new Comment(); - comment.setAuthorID(authorID); - comment.setType(reqBody.getComment()); - if (reqBody.getComment()) { - //若为评论 - if (reqBody.getArticleID() <= 0) { - throw new MyException(ResponseEnum.PARAMETERS_ERROR); - } - comment.setArticleID(reqBody.getArticleID()); - } else { - comment.setArticleID(-1L); + comment.setFromUser(user); + User userTo = new User(); + userTo.setId(-1L); + if (reqBody.getToUserId() != -1) { + userTo = userMapper.findById(reqBody.getToUserId()); + comment.setToUser(userTo); } - comment.setContent(reqBody.getContent()); - comment.setPid(reqBody.getPid()); - comment.setDate(new Date()); - comment.setResponseId(""); + comment.setToUser(userTo); + userMapper.findById(reqBody.getToUserId()); + BeanUtils.copyProperties(reqBody, comment); commentMapper.insert(comment); - if (reqBody.getPid() != -1) { - commentMapper.updateResponder(pComment.getResponseId() + comment.getId() + ",", reqBody.getPid()); - } - return trans(comment); + return ModalTrans.comment(commentMapper.findCommentById(comment.getId())); } @Override public boolean delete(long id) { - boolean b = commentMapper.existsById(id); - if (!b) { + Comment b = commentMapper.findCommentById(id); + if (b == null ) { throw new MyException(ResponseEnum.COMMENT_NOT_EXIST); } + if(b.isDelete()){ + throw new MyException(ResponseEnum.DATA_IS_DELETED); + } commentMapper.delete(id); return true; } @@ -101,86 +96,54 @@ public class CommentServiceImpl implements CommentService { commentMapper.updateContent(reqBody.getContent(), reqBody.getId()); comment.setContent(reqBody.getContent()); } - if (!comment.getResponseId().equals(reqBody.getResponseId())) { - commentMapper.updateResponder(reqBody.getResponseId(), reqBody.getId()); - comment.setResponseId(reqBody.getResponseId()); - } - return trans(comment); + return ModalTrans.comment(comment); } - @Override - public PageInfo retrievePage(Boolean isComment, int page, int count) { + public PageData retrievePage(String pagePath, int page, int count) { PageHelper.startPage(page, count); - List commentList = commentMapper.findAllByType(isComment); - return pageTrans(commentList); + List list = commentMapper.findAllByPagePath(pagePath); + return pageTrans(list); } @Override public List retrievePageByPid(long pid) { - List commentList = commentMapper.findAllByPId(pid); + List allByPagePath = commentMapper.findAllByPid(pid); + List commentModels = new ArrayList<>(); + allByPagePath.forEach(comment -> commentModels.add(ModalTrans.comment(comment))); + return commentModels; + } + + @Override + public PageData retrievePageByAuthor(String pagePath, int page, int count) { + User user = redisUserUtil.get(); + PageHelper.startPage(page, count); + List list = commentMapper.findAllByPagePathAndFromUser(pagePath, user.getId()); + return pageTrans(list); + } + + @Override + public PageData retrievePageByPageAndPid(String pagePath, long pid, int page, int count) { + PageHelper.startPage(page, count); + List list = commentMapper.findAllByPagePathAndPid(pagePath, pid); + return pageTrans(list); + } + + @Override + public PageData retrievePageByPage(String pagePath, int page, int count) { + PageHelper.startPage(page, count); + List list = commentMapper.findAllByPagePathAndPid(pagePath, -1); + return pageTrans(list); + } + + private PageData pageTrans(List commentList) { + PageInfo p = PageInfo.of(commentList); List modelList = new ArrayList<>(); - commentList.forEach(m -> modelList.add(trans(m))); - return modelList; - } - - @Override - public PageInfo retrievePageByArticle(long articleID, long pid, int page, int count) { - PageHelper.startPage(page, count); - List commentList = commentMapper.findAllByArticleIDAndPId(articleID, pid); - return pageTrans(commentList); - } - - @Override - public PageInfo retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count) { - PageHelper.startPage(page, count); - List commentList = commentMapper.findCommentsByTypeAndPId(isComment, pid); - return pageTrans(commentList); - } - - @Override - public PageInfo retrievePageByAuthor(Boolean isComment, int page, int count) { - PageHelper.startPage(page, count); - List commentList = commentMapper.findAllByAuthorIDAndType(redisUserUtil.get().getId(), isComment); - return pageTrans(commentList); - } - - - @Override - public PageInfo retrievePageByType(Boolean isComment, int page, int count) { - PageHelper.startPage(page, count); - List commentList = commentMapper.findAllByType(isComment); - return pageTrans(commentList); - } - - private CommentModel trans(Comment comment) { - CommentModel commentModel = new CommentModel(); - commentModel.setId(comment.getId()); - commentModel.setComment(comment.getType()); - commentModel.setContent(comment.getContent()); - commentModel.setArticleID(comment.getArticleID()); - commentModel.setDate(DateFormatUtil.get(comment.getDate())); - commentModel.setResponseId(comment.getResponseId()); - commentModel.setPid(comment.getPid()); - commentModel.setAuthorName(userService.getNameById(comment.getAuthorID())); - commentModel.setAuthorAvatarImgUrl("http://cdn.celess.cn/" + userService.getAvatarImg(comment.getAuthorID())); - - if (comment.getType() && commentModel.getArticleID() > 0) { - commentModel.setArticleTitle(articleMapper.getTitleById(comment.getArticleID())); - } - return commentModel; - } - - private PageInfo pageTrans(List commentList) { - PageInfo p = new PageInfo(commentList); - List modelList = new ArrayList<>(); - commentList.forEach(l -> { - CommentModel model = trans(l); + CommentModel model = ModalTrans.comment(l); model.setRespComment(this.retrievePageByPid(model.getId())); modelList.add(model); }); - p.setList(modelList); - return p; + return new PageData(p, modelList); } } diff --git a/src/main/java/cn/celess/blog/util/ModalTrans.java b/src/main/java/cn/celess/blog/util/ModalTrans.java index a5cbdd8..9b58ab7 100644 --- a/src/main/java/cn/celess/blog/util/ModalTrans.java +++ b/src/main/java/cn/celess/blog/util/ModalTrans.java @@ -38,6 +38,9 @@ public class ModalTrans { public static UserModel user(User user) { + if (user == null || user.getId() == -1) { + return null; + } UserModel userModel = new UserModel(); BeanUtils.copyProperties(user, userModel); userModel.setAvatarImgUrl(user.getAvatarImgUrl() == null || user.getAvatarImgUrl().length() == 0 ? @@ -49,6 +52,9 @@ public class ModalTrans { } public static CategoryModel category(Category category) { + if (category == null) { + return null; + } CategoryModel model = new CategoryModel(); BeanUtils.copyProperties(category, model); return model; @@ -56,6 +62,9 @@ public class ModalTrans { public static TagModel tag(Tag tag) { + if (tag == null) { + return null; + } TagModel model = new TagModel(); BeanUtils.copyProperties(tag, model); return model; @@ -63,10 +72,25 @@ public class ModalTrans { public static WebUpdateModel webUpdate(WebUpdate update) { + if (update == null) { + return null; + } WebUpdateModel model = new WebUpdateModel(); model.setId(update.getId()); model.setInfo(update.getUpdateInfo()); model.setTime(DateFormatUtil.get(update.getUpdateTime())); return model; } + + public static CommentModel comment(Comment comment) { + if (comment == null) { + return null; + } + CommentModel model = new CommentModel(); + BeanUtils.copyProperties(comment, model); + model.setFromUser(user(comment.getFromUser())); + model.setToUser(user(comment.getToUser())); + model.setDate(DateFormatUtil.get(comment.getDate())); + return model; + } } diff --git a/src/main/resources/mapper/CommentMapper.xml b/src/main/resources/mapper/CommentMapper.xml index 6c7026e..042c078 100644 --- a/src/main/resources/mapper/CommentMapper.xml +++ b/src/main/resources/mapper/CommentMapper.xml @@ -68,7 +68,7 @@ + + select count(*) - from article where is_delete = false; + from article + where is_delete = false; diff --git a/src/test/java/cn/celess/blog/controller/CommentControllerTest.java b/src/test/java/cn/celess/blog/controller/CommentControllerTest.java index 24b07d1..19df58b 100644 --- a/src/test/java/cn/celess/blog/controller/CommentControllerTest.java +++ b/src/test/java/cn/celess/blog/controller/CommentControllerTest.java @@ -3,24 +3,18 @@ package cn.celess.blog.controller; import cn.celess.blog.BaseTest; import cn.celess.blog.entity.Article; import cn.celess.blog.entity.Comment; +import cn.celess.blog.entity.User; import cn.celess.blog.entity.model.CommentModel; import cn.celess.blog.entity.request.CommentReq; import cn.celess.blog.mapper.ArticleMapper; import cn.celess.blog.mapper.CommentMapper; import cn.celess.blog.mapper.UserMapper; -import com.github.pagehelper.PageInfo; -import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.result.MockMvcResultHandlers; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; import java.util.UUID; -import java.util.concurrent.atomic.AtomicReference; import static org.junit.Assert.*; import static cn.celess.blog.enmu.ResponseEnum.*; @@ -30,19 +24,19 @@ public class CommentControllerTest extends BaseTest { @Autowired ArticleMapper articleMapper; @Autowired + UserMapper userMapper; + @Autowired CommentMapper commentMapper; @Test public void addOne() throws Exception { + Article article = articleMapper.getLastestArticle(); CommentReq commentReq = new CommentReq(); - // 测试留言 - commentReq.setArticleID(null); - commentReq.setComment(false); + commentReq.setPagePath("/article/" + article.getId()); commentReq.setContent(UUID.randomUUID().toString()); commentReq.setPid(-1L); - commentReq.setResponseId(null); + commentReq.setToUserId(-1L); String token = userLogin(); - CommentModel PC = null; mockMvc.perform(post("/user/comment/create") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(JSONObject.fromObject(commentReq).toString()) @@ -52,22 +46,19 @@ public class CommentControllerTest extends BaseTest { assertEquals(SUCCESS.getCode(), object.getInt(Code)); CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); assertNotEquals(0, model.getId()); - assertEquals(commentReq.getPid().longValue(), model.getPid()); - assertEquals(-1, model.getPid()); - assertEquals(commentReq.getComment(), model.isComment()); + assertEquals(commentReq.getPid(), model.getPid().longValue()); + assertEquals(-1, model.getPid().longValue()); assertEquals(commentReq.getContent(), model.getContent()); assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); + assertNotNull(model.getFromUser()); + assertNull(model.getToUser()); }); - Article article = articleMapper.getLastestArticle(); - // 测试评论 - commentReq.setArticleID(article.getId()); - commentReq.setComment(true); + + commentReq.setPagePath("/article/" + article.getId()); commentReq.setContent(UUID.randomUUID().toString()); commentReq.setPid(-1L); - commentReq.setResponseId(null); + commentReq.setToUserId(2); mockMvc.perform(post("/user/comment/create") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(JSONObject.fromObject(commentReq).toString()) @@ -78,58 +69,50 @@ public class CommentControllerTest extends BaseTest { CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); // 响应数据的完整性 assertNotEquals(0, model.getId()); - assertEquals(commentReq.getPid().longValue(), model.getPid()); - assertEquals(-1, model.getPid()); - assertEquals(commentReq.getComment(), model.isComment()); + assertEquals(commentReq.getPid(), model.getPid().longValue()); + assertEquals(-1, model.getPid().longValue()); assertEquals(commentReq.getContent(), model.getContent()); - assertEquals(commentReq.getArticleID().longValue(), model.getArticleID()); + assertEquals(commentReq.getPagePath(), "/article/" + article.getId()); assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); + assertNotNull(model.getFromUser()); + assertNotNull(model.getToUser()); }); // 测试二级回复 Comment lastestComment = commentMapper.getLastestComment(); - commentReq.setArticleID(lastestComment.getArticleID()); - commentReq.setComment(lastestComment.getType()); + commentReq.setPagePath("/article/" + article.getId()); commentReq.setContent(UUID.randomUUID().toString()); commentReq.setPid(lastestComment.getId()); - commentReq.setResponseId(null); mockMvc.perform(post("/user/comment/create") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(JSONObject.fromObject(commentReq).toString()) .header("Authorization", token) - ).andDo(MockMvcResultHandlers.print()).andDo(result -> { + ).andDo(result -> { JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); assertEquals(SUCCESS.getCode(), object.getInt(Code)); CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); // 重新获取父评论信息 Comment pCommon = commentMapper.findCommentById(lastestComment.getId()); - assertEquals(pCommon.getId().longValue(), model.getPid()); - // 判断父评论中是否有写入当前新增的评论的id - String[] ids = pCommon.getResponseId().split(","); - boolean contain = false; - for (String id : ids) { - if (!id.isEmpty() && Long.parseLong(id) == model.getId()) { - contain = true; - break; - } - } - assertTrue(contain); + assertEquals(pCommon.getId(), model.getPid()); }); } @Test public void deleteTest() throws Exception { // 准备数据 - Comment c = new Comment(); - c.setArticleID(-1L); - c.setType(true); - c.setAuthorID(2L); - c.setDate(new Date()); - c.setPid(-1L); - commentMapper.insert(c); - Comment comment = commentMapper.getLastestComment(); + User from = userMapper.findByEmail("zh56462271@qq.com"); + assertNotNull(from); + User to = userMapper.findByEmail("a@celess.cn"); + 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); + comment = commentMapper.findCommentById(comment.getId()); // 接口测试 long id = comment.getId(); assertNotEquals(0, id); @@ -141,7 +124,7 @@ public class CommentControllerTest extends BaseTest { }); mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> { JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(COMMENT_NOT_EXIST.getCode(), object.getInt(Code)); + assertEquals(DATA_IS_DELETED.getCode(), object.getInt(Code)); }); } @@ -150,12 +133,8 @@ public class CommentControllerTest extends BaseTest { Comment comment = commentMapper.getLastestComment(); CommentReq commentReq = new CommentReq(); commentReq.setId(comment.getId()); - commentReq.setPid(comment.getPid()); commentReq.setContent(UUID.randomUUID().toString()); - commentReq.setArticleID(comment.getArticleID()); // 不合法数据 setResponseId - commentReq.setResponseId("xxxx"); - commentReq.setComment(comment.getType()); mockMvc.perform(put("/user/comment/update") .content(JSONObject.fromObject(commentReq).toString()) .contentType(MediaType.APPLICATION_JSON_UTF8) @@ -165,161 +144,7 @@ public class CommentControllerTest extends BaseTest { assertEquals(SUCCESS.getCode(), object.getInt(Code)); CommentModel c = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); assertEquals(commentReq.getContent(), c.getContent()); - assertEquals(commentReq.getResponseId(), c.getResponseId()); - assertNotNull(c.getAuthorAvatarImgUrl()); - assertNotNull(c.getAuthorName()); - assertNotNull(c.getDate()); - assertNotEquals(0, c.getPid()); - assertNotEquals(0, c.getArticleID()); }); } - @Test - public void commentsOfArticle() throws Exception { - mockMvc.perform(get("/comments?articleId=3&page=1&count=10")).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class); - assertNotEquals(0, pageInfo.getStartRow()); - assertNotEquals(0, pageInfo.getEndRow()); - assertEquals(1, pageInfo.getPageNum()); - assertEquals(10, pageInfo.getPageSize()); - pageInfo.getList().forEach(o -> { - CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); - assertEquals(3, model.getArticleID()); - assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); - assertNotNull(model.getArticleTitle()); - assertNotNull(model.getContent()); - assertNotNull(model.getResponseId()); - }); - }); - } - - @Test - public void retrievePage() throws Exception { - long pid = -1; - mockMvc.perform(get("/comment/pid/" + pid)).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - JSONArray jsonArray = object.getJSONArray(Result); - assertNotEquals(0, jsonArray.size()); - jsonArray.forEach(o -> { - CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); - assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); - assertNotNull(model.getContent()); - assertNotNull(model.getResponseId()); - }); - }); - } - - @Test - public void retrievePageOfLeaveMsg() throws Exception { - mockMvc.perform(get("/leaveMsg?page=1&count=10")).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class); - assertNotEquals(0, pageInfo.getStartRow()); - assertNotEquals(0, pageInfo.getEndRow()); - assertEquals(1, pageInfo.getPageNum()); - assertEquals(10, pageInfo.getPageSize()); - pageInfo.getList().forEach(o -> { - CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); - assertEquals(-1, model.getArticleID()); - assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); - assertNotNull(model.getContent()); - assertNotNull(model.getResponseId()); - assertFalse(model.isComment()); - }); - }); - } - - @Test - public void retrievePageAdmin() throws Exception { - mockMvc.perform(get("/admin/comment/type/1?page=1&count=10").header("Authorization", adminLogin())).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class); - assertNotEquals(0, pageInfo.getStartRow()); - assertNotEquals(0, pageInfo.getEndRow()); - assertEquals(1, pageInfo.getPageNum()); - assertEquals(10, pageInfo.getPageSize()); - pageInfo.getList().forEach(o -> { - CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); - assertNotEquals(-1, model.getArticleID()); - assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); - assertNotNull(model.getContent()); - assertNotNull(model.getResponseId()); - assertTrue(model.isComment()); - }); - }); - mockMvc.perform(get("/admin/comment/type/0?page=1&count=10").header("Authorization", adminLogin())).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class); - assertNotEquals(0, pageInfo.getStartRow()); - assertNotEquals(0, pageInfo.getEndRow()); - assertEquals(1, pageInfo.getPageNum()); - assertEquals(10, pageInfo.getPageSize()); - pageInfo.getList().forEach(o -> { - CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); - assertEquals(-1, model.getArticleID()); - assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); - assertNotNull(model.getContent()); - assertNotNull(model.getResponseId()); - assertFalse(model.isComment()); - }); - }); - } - - @Test - public void retrievePageByAuthor() throws Exception { - mockMvc.perform(get("/user/comment/type/1?page=1&count=10").header("Authorization", userLogin())).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class); - assertNotEquals(0, pageInfo.getStartRow()); - assertNotEquals(0, pageInfo.getEndRow()); - assertEquals(1, pageInfo.getPageNum()); - assertEquals(10, pageInfo.getPageSize()); - pageInfo.getList().forEach(o -> { - CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); - assertNotEquals(-1, model.getArticleID()); - assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); - assertNotNull(model.getContent()); - assertNotNull(model.getResponseId()); - assertTrue(model.isComment()); - }); - }); - mockMvc.perform(get("/user/comment/type/0?page=1&count=10").header("Authorization", userLogin())).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class); - assertNotEquals(0, pageInfo.getStartRow()); - assertNotEquals(0, pageInfo.getEndRow()); - assertEquals(1, pageInfo.getPageNum()); - assertEquals(10, pageInfo.getPageSize()); - pageInfo.getList().forEach(o -> { - CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); - assertEquals(-1, model.getArticleID()); - assertNotNull(model.getDate()); - assertNotNull(model.getAuthorName()); - assertNotNull(model.getAuthorAvatarImgUrl()); - assertNotNull(model.getContent()); - assertNotNull(model.getResponseId()); - assertFalse(model.isComment()); - }); - }); - } } \ No newline at end of file