Service,dao层,视图,模型修改 单元测试

This commit is contained in:
禾几海
2020-05-26 12:34:35 +08:00
parent fde9b8511c
commit aa882406d0
11 changed files with 219 additions and 426 deletions

View File

@@ -137,30 +137,42 @@ where article.a_id = article_tag.a_id
and category.is_category = true and category.is_category = true
and article.a_author_id = user.u_id; and article.a_author_id = user.u_id;
CREATE VIEW commentView
(commentId, pagePath, content, date, status, pid, CREATE VIEW commentView
fromAuthorId, fromAuthorEmail, fromAuthorDisplayName, fromAuthorAvatar, toAuthorId, (commentId, pagePath, content, date, status, pid, toAuthorId, toAuthorEmail, toAuthorDisplayName,
toAuthorEmail, toAuthorDisplayName, toAuthorAvatar, isDelete toAuthorAvatar, fromAuthorId, fromAuthorEmail, fromAuthorDisplayName,
) fromAuthorAvatar, isDelete)
as as
select comment.co_id as commentId, select cuT.co_id as commentId,
comment.co_page_path as pagePath, cuT.co_page_path as pagePath,
comment.co_content as content, cuT.co_content as content,
comment.co_date as date, cuT.co_date as date,
comment.co_status as status, cuT.co_status as status,
comment.co_pid as pid, cuT.co_pid as pid,
comment.co_from_author_id as fromAuthorId, cuT.co_to_author_id as toAuthorId,
userFrom.u_email as fromAuthorEmail, cuT.toEmail as toAuthorEmail,
userFrom.u_display_name as fromAuthorDisplayName, cuT.toDisplayName as toAuthorDisplayName,
userFrom.u_avatar as fromAuthorAvatar, cuT.toAvatar as toAuthorAvatar,
comment.co_to_author_id as toAuthorId, userFrom.u_id as fromAuthorId,
userTo.u_email as toAuthorEmail, userFrom.u_email as fromAuthorEmail,
userTo.u_display_name as toAuthorDisplayName, userFrom.u_display_name as fromAuthorDisplayName,
userTo.u_avatar as toAuthorAvatar, userFrom.u_avatar as fromAuthorAvatar,
comment.is_delete as isDelete cuT.is_delete as isDelete
from comment, from (select comment.co_id,
user as userFrom, comment.co_page_path,
user as userTo comment.co_content,
where comment.co_from_author_id = userFrom.u_id comment.co_date,
and comment.co_to_author_id = userTo.u_id; 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;

View File

@@ -40,58 +40,40 @@ public class CommentController {
} }
/** /**
* 获取所有的一级评论 * 获取所有的评论
* *
* @param articleId 文章id * @param pagePath pagePath
* @param count 单页数据量 * @param count 单页数据量
* @param page 页码 * @param page 页码
* @return Response * @return Response
*/ */
@GetMapping("/comments") @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 = "count", required = false, defaultValue = "10") int count,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) { @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获取数据 * 通过pid获取数据
* *
* @param pid * @param pagePath pagePath
* @return * @param count count
* @param page page
* @return Response
*/ */
@GetMapping("/comment/pid/{pid}") @GetMapping("/comment/pagePath/{pagePath}")
public Response retrievePage(@PathVariable("pid") long pid) { public Response retrievePage(@PathVariable("pagePath") String pagePath,
return ResponseUtil.success(commentService.retrievePageByPid(pid)); @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));
} }
/** @GetMapping("/user/comment/pagePath/{pagePath}")
* 获取所以的一级留言 public Response userComment(@PathVariable("pagePath") String pagePath,
* @RequestParam(value = "count", required = false, defaultValue = "10") int count,
* @param count @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
* @param page return ResponseUtil.success(commentService.retrievePageByAuthor(pagePath, page, count));
* @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("/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));
}
} }

View File

@@ -11,6 +11,8 @@ public enum ResponseEnum {
FAILURE(-1, "失败"), FAILURE(-1, "失败"),
ERROR(-2, "错误"), ERROR(-2, "错误"),
DATA_IS_DELETED(1000, "数据已被删除"),
//文章类 //文章类
ARTICLE_NOT_EXIST(2010, "文章不存在"), ARTICLE_NOT_EXIST(2010, "文章不存在"),
ARTICLE_HAS_EXIST(2020, "文章已存在"), ARTICLE_HAS_EXIST(2020, "文章已存在"),
@@ -68,7 +70,6 @@ public enum ResponseEnum {
PARAMETERS_QQ_ERROR(8540, "QQ格式错误"), PARAMETERS_QQ_ERROR(8540, "QQ格式错误"),
PARAMETERS_PWD_ERROR(8550, "密码格式错误"), PARAMETERS_PWD_ERROR(8550, "密码格式错误"),
VERIFY_OUT(8400, "已经验证过了"); VERIFY_OUT(8400, "已经验证过了");
private int code; private int code;
private String msg; private String msg;

View File

@@ -1,5 +1,6 @@
package cn.celess.blog.entity.model; package cn.celess.blog.entity.model;
import cn.celess.blog.entity.User;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@@ -14,46 +15,31 @@ import java.util.List;
public class CommentModel { public class CommentModel {
private long id; private long id;
/** private UserModel fromUser;
* 是评论还是留言 0:评论 其他1留言
*/
private boolean isComment;
private String authorName; private UserModel toUser;
private String authorAvatarImgUrl;
/** /**
* 内容 * 内容
*/ */
private String content; private String content;
/**
* 文章ID
*/
private long articleID;
/** /**
* 文章标题 * 文章标题
*/ */
private String articleTitle; private String pagePath;
/** /**
* 发布日期 * 发布日期
*/ */
private String date; private String date;
/**
* 回应着ID 默认为顶级回复
*/
private String responseId = "";
/** /**
* 评论的父ID * 评论的父ID
*/ */
private long pid = -1; private Long pid;
private List<CommentModel> respComment; private List<CommentModel> respComment;
private int status;
} }

View File

@@ -9,9 +9,8 @@ import lombok.Data;
@Data @Data
public class CommentReq { public class CommentReq {
private Long id; private Long id;
private Boolean comment;
private String content; private String content;
private Long pid; private long pid = -1;
private Long articleID; private String pagePath;
private String responseId; private long toUserId = -1;
} }

View File

@@ -34,6 +34,8 @@ public interface CommentMapper {
List<Comment> findAllByPagePath(String pagePath); List<Comment> findAllByPagePath(String pagePath);
List<Comment> findAllByPagePathAndFromUser(String pagePath,long userId);
List<Comment> findAllByPagePathAndPid(String pagePath, long pid); List<Comment> findAllByPagePathAndPid(String pagePath, long pid);
long countByPagePath(String pagePath); long countByPagePath(String pagePath);

View File

@@ -1,6 +1,7 @@
package cn.celess.blog.service; package cn.celess.blog.service;
import cn.celess.blog.entity.model.CommentModel; 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.entity.request.CommentReq;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -40,17 +41,17 @@ public interface CommentService {
/** /**
* 分页获取数据 * 分页获取数据
* *
* @param isComment true评论 false留言 * @param pagePath pagePath
* @param count 单页数据量 * @param count 单页数据量
* @param page 数据页 * @param page 数据页
* @return 分页数据 * @return 分页数据
*/ */
PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count); PageData<CommentModel> retrievePage(String pagePath, int page, int count);
/** /**
* 通过pid获取数据 * 通过pid获取数据
* *
* @param pid 父id * @param pid 父id
* @return 分页数据 * @return 分页数据
*/ */
List<CommentModel> retrievePageByPid(long pid); List<CommentModel> retrievePageByPid(long pid);
@@ -59,43 +60,33 @@ public interface CommentService {
/** /**
* 根据评论者获取数据 * 根据评论者获取数据
* *
* @param isComment true评论 false留言 * @param pagePath pagePath
* @param count 单页数据量 * @param count 单页数据量
* @param page 数据页 * @param page 数据页
* @return 分页数据 * @return 分页数据
*/ */
PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count); PageData<CommentModel> retrievePageByAuthor(String pagePath, int page, int count);
/**
* 根据文章获取数据
*
* @param articleID 文章id
* @param pid 父id
* @param count 单页数据量
* @param page 数据页
* @return 分页数据
*/
PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count);
/** /**
* 根据数据的type和pid获取数据 * 根据数据的type和pid获取数据
* *
* @param isComment true评论 false留言 * @param pagePath pagePath
* @param pid 父id * @param pid 父id
* @param count 单页数据量 * @param count 单页数据量
* @param page 数据页 * @param page 数据页
* @return 分页数据 * @return 分页数据
*/ */
PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count); PageData<CommentModel> retrievePageByPageAndPid(String pagePath, long pid, int page, int count);
/** /**
* 根据type获取数据 * 根据type获取数据
* *
* @param isComment true评论 false留言 * @param pagePath pagePath
* @param count 单页数据量 * @param count 单页数据量
* @param page 数据页 * @param page 数据页
* @return 分页数据 * @return 分页数据
*/ */
PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count); PageData<CommentModel> retrievePageByPage(String pagePath, int page, int count);
} }

View File

@@ -2,18 +2,22 @@ package cn.celess.blog.service.serviceimpl;
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.model.CommentModel; 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.entity.request.CommentReq;
import cn.celess.blog.exception.MyException; import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.ArticleMapper; import cn.celess.blog.mapper.ArticleMapper;
import cn.celess.blog.mapper.CommentMapper; import cn.celess.blog.mapper.CommentMapper;
import cn.celess.blog.mapper.UserMapper;
import cn.celess.blog.service.CommentService; import cn.celess.blog.service.CommentService;
import cn.celess.blog.service.UserService; import cn.celess.blog.service.UserService;
import cn.celess.blog.util.DateFormatUtil; import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.ModalTrans;
import cn.celess.blog.util.RedisUserUtil; import cn.celess.blog.util.RedisUserUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -31,7 +35,7 @@ public class CommentServiceImpl implements CommentService {
@Autowired @Autowired
CommentMapper commentMapper; CommentMapper commentMapper;
@Autowired @Autowired
UserService userService; UserMapper userMapper;
@Autowired @Autowired
ArticleMapper articleMapper; ArticleMapper articleMapper;
@Autowired @Autowired
@@ -41,52 +45,43 @@ public class CommentServiceImpl implements CommentService {
@Override @Override
public CommentModel create(CommentReq reqBody) { public CommentModel create(CommentReq reqBody) {
if (reqBody == null) { if (reqBody == null) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR); throw new MyException(ResponseEnum.PARAMETERS_ERROR);
} }
long authorID = redisUserUtil.get().getId(); User user = redisUserUtil.get();
Comment pComment = null; Comment pComment = null;
if (reqBody.getPid() != null && reqBody.getPid() != -1) { if (reqBody.getPid() != -1) {
pComment = commentMapper.findCommentById(reqBody.getPid()); pComment = commentMapper.findCommentById(reqBody.getPid());
} }
if (reqBody.getPid() == null) {
reqBody.setPid(-1L);
}
//不是一级评论 //不是一级评论
if (reqBody.getPid() != -1 && pComment == null) { if (reqBody.getPid() != -1 && pComment == null) {
//父评论不存在 //父评论不存在
throw new MyException(ResponseEnum.PARAMETERS_ERROR); throw new MyException(ResponseEnum.PARAMETERS_ERROR);
} }
Comment comment = new Comment(); Comment comment = new Comment();
comment.setAuthorID(authorID); comment.setFromUser(user);
comment.setType(reqBody.getComment()); User userTo = new User();
if (reqBody.getComment()) { userTo.setId(-1L);
//若为评论 if (reqBody.getToUserId() != -1) {
if (reqBody.getArticleID() <= 0) { userTo = userMapper.findById(reqBody.getToUserId());
throw new MyException(ResponseEnum.PARAMETERS_ERROR); comment.setToUser(userTo);
}
comment.setArticleID(reqBody.getArticleID());
} else {
comment.setArticleID(-1L);
} }
comment.setContent(reqBody.getContent()); comment.setToUser(userTo);
comment.setPid(reqBody.getPid()); userMapper.findById(reqBody.getToUserId());
comment.setDate(new Date()); BeanUtils.copyProperties(reqBody, comment);
comment.setResponseId("");
commentMapper.insert(comment); commentMapper.insert(comment);
if (reqBody.getPid() != -1) { return ModalTrans.comment(commentMapper.findCommentById(comment.getId()));
commentMapper.updateResponder(pComment.getResponseId() + comment.getId() + ",", reqBody.getPid());
}
return trans(comment);
} }
@Override @Override
public boolean delete(long id) { public boolean delete(long id) {
boolean b = commentMapper.existsById(id); Comment b = commentMapper.findCommentById(id);
if (!b) { if (b == null ) {
throw new MyException(ResponseEnum.COMMENT_NOT_EXIST); throw new MyException(ResponseEnum.COMMENT_NOT_EXIST);
} }
if(b.isDelete()){
throw new MyException(ResponseEnum.DATA_IS_DELETED);
}
commentMapper.delete(id); commentMapper.delete(id);
return true; return true;
} }
@@ -101,86 +96,54 @@ public class CommentServiceImpl implements CommentService {
commentMapper.updateContent(reqBody.getContent(), reqBody.getId()); commentMapper.updateContent(reqBody.getContent(), reqBody.getId());
comment.setContent(reqBody.getContent()); comment.setContent(reqBody.getContent());
} }
if (!comment.getResponseId().equals(reqBody.getResponseId())) { return ModalTrans.comment(comment);
commentMapper.updateResponder(reqBody.getResponseId(), reqBody.getId());
comment.setResponseId(reqBody.getResponseId());
}
return trans(comment);
} }
@Override @Override
public PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count) { public PageData<CommentModel> retrievePage(String pagePath, int page, int count) {
PageHelper.startPage(page, count); PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByType(isComment); List<Comment> list = commentMapper.findAllByPagePath(pagePath);
return pageTrans(commentList); return pageTrans(list);
} }
@Override @Override
public List<CommentModel> retrievePageByPid(long pid) { public List<CommentModel> retrievePageByPid(long pid) {
List<Comment> commentList = commentMapper.findAllByPId(pid); List<Comment> allByPagePath = commentMapper.findAllByPid(pid);
List<CommentModel> commentModels = new ArrayList<>();
allByPagePath.forEach(comment -> commentModels.add(ModalTrans.comment(comment)));
return commentModels;
}
@Override
public PageData<CommentModel> retrievePageByAuthor(String pagePath, int page, int count) {
User user = redisUserUtil.get();
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndFromUser(pagePath, user.getId());
return pageTrans(list);
}
@Override
public PageData<CommentModel> retrievePageByPageAndPid(String pagePath, long pid, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, pid);
return pageTrans(list);
}
@Override
public PageData<CommentModel> retrievePageByPage(String pagePath, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, -1);
return pageTrans(list);
}
private PageData<CommentModel> pageTrans(List<Comment> commentList) {
PageInfo<Comment> p = PageInfo.of(commentList);
List<CommentModel> modelList = new ArrayList<>(); List<CommentModel> modelList = new ArrayList<>();
commentList.forEach(m -> modelList.add(trans(m)));
return modelList;
}
@Override
public PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByArticleIDAndPId(articleID, pid);
return pageTrans(commentList);
}
@Override
public PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findCommentsByTypeAndPId(isComment, pid);
return pageTrans(commentList);
}
@Override
public PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByAuthorIDAndType(redisUserUtil.get().getId(), isComment);
return pageTrans(commentList);
}
@Override
public PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> 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<CommentModel> pageTrans(List<Comment> commentList) {
PageInfo p = new PageInfo(commentList);
List<CommentModel> modelList = new ArrayList<>();
commentList.forEach(l -> { commentList.forEach(l -> {
CommentModel model = trans(l); CommentModel model = ModalTrans.comment(l);
model.setRespComment(this.retrievePageByPid(model.getId())); model.setRespComment(this.retrievePageByPid(model.getId()));
modelList.add(model); modelList.add(model);
}); });
p.setList(modelList); return new PageData<CommentModel>(p, modelList);
return p;
} }
} }

View File

@@ -38,6 +38,9 @@ public class ModalTrans {
public static UserModel user(User user) { public static UserModel user(User user) {
if (user == null || user.getId() == -1) {
return null;
}
UserModel userModel = new UserModel(); UserModel userModel = new UserModel();
BeanUtils.copyProperties(user, userModel); BeanUtils.copyProperties(user, userModel);
userModel.setAvatarImgUrl(user.getAvatarImgUrl() == null || user.getAvatarImgUrl().length() == 0 ? userModel.setAvatarImgUrl(user.getAvatarImgUrl() == null || user.getAvatarImgUrl().length() == 0 ?
@@ -49,6 +52,9 @@ public class ModalTrans {
} }
public static CategoryModel category(Category category) { public static CategoryModel category(Category category) {
if (category == null) {
return null;
}
CategoryModel model = new CategoryModel(); CategoryModel model = new CategoryModel();
BeanUtils.copyProperties(category, model); BeanUtils.copyProperties(category, model);
return model; return model;
@@ -56,6 +62,9 @@ public class ModalTrans {
public static TagModel tag(Tag tag) { public static TagModel tag(Tag tag) {
if (tag == null) {
return null;
}
TagModel model = new TagModel(); TagModel model = new TagModel();
BeanUtils.copyProperties(tag, model); BeanUtils.copyProperties(tag, model);
return model; return model;
@@ -63,10 +72,25 @@ public class ModalTrans {
public static WebUpdateModel webUpdate(WebUpdate update) { public static WebUpdateModel webUpdate(WebUpdate update) {
if (update == null) {
return null;
}
WebUpdateModel model = new WebUpdateModel(); WebUpdateModel model = new WebUpdateModel();
model.setId(update.getId()); model.setId(update.getId());
model.setInfo(update.getUpdateInfo()); model.setInfo(update.getUpdateInfo());
model.setTime(DateFormatUtil.get(update.getUpdateTime())); model.setTime(DateFormatUtil.get(update.getUpdateTime()));
return model; 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;
}
} }

View File

@@ -68,7 +68,7 @@
<select id="findCommentById" resultMap="commentViewResultMap"> <select id="findCommentById" resultMap="commentViewResultMap">
select * select *
from commentView from commentView
where commentId = #{id} where commentId = #{sqid}
</select> </select>
<select id="findAllByFromUser" resultMap="commentViewResultMap"> <select id="findAllByFromUser" resultMap="commentViewResultMap">
@@ -89,6 +89,13 @@
where pagePath = #{pagePath} where pagePath = #{pagePath}
</select> </select>
<select id="findAllByPagePathAndFromUser" resultMap="commentViewResultMap">
select *
from commentView
where pagePath = #{pagePath}
and fromAuthorId = #{userId}
</select>
<select id="findAllByPagePathAndPid" resultMap="commentViewResultMap"> <select id="findAllByPagePathAndPid" resultMap="commentViewResultMap">
select * select *
from commentView from commentView
@@ -110,7 +117,8 @@
<select id="count" resultType="long"> <select id="count" resultType="long">
select count(*) select count(*)
from article where is_delete = false; from article
where is_delete = false;
</select> </select>
</mapper> </mapper>

View File

@@ -3,24 +3,18 @@ package cn.celess.blog.controller;
import cn.celess.blog.BaseTest; import cn.celess.blog.BaseTest;
import cn.celess.blog.entity.Article; import cn.celess.blog.entity.Article;
import cn.celess.blog.entity.Comment; 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.CommentModel;
import cn.celess.blog.entity.request.CommentReq; import cn.celess.blog.entity.request.CommentReq;
import cn.celess.blog.mapper.ArticleMapper; import cn.celess.blog.mapper.ArticleMapper;
import cn.celess.blog.mapper.CommentMapper; import cn.celess.blog.mapper.CommentMapper;
import cn.celess.blog.mapper.UserMapper; import cn.celess.blog.mapper.UserMapper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; 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.UUID;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static cn.celess.blog.enmu.ResponseEnum.*; import static cn.celess.blog.enmu.ResponseEnum.*;
@@ -30,19 +24,19 @@ public class CommentControllerTest extends BaseTest {
@Autowired @Autowired
ArticleMapper articleMapper; ArticleMapper articleMapper;
@Autowired @Autowired
UserMapper userMapper;
@Autowired
CommentMapper commentMapper; CommentMapper commentMapper;
@Test @Test
public void addOne() throws Exception { public void addOne() throws Exception {
Article article = articleMapper.getLastestArticle();
CommentReq commentReq = new CommentReq(); CommentReq commentReq = new CommentReq();
// 测试留言 commentReq.setPagePath("/article/" + article.getId());
commentReq.setArticleID(null);
commentReq.setComment(false);
commentReq.setContent(UUID.randomUUID().toString()); commentReq.setContent(UUID.randomUUID().toString());
commentReq.setPid(-1L); commentReq.setPid(-1L);
commentReq.setResponseId(null); commentReq.setToUserId(-1L);
String token = userLogin(); String token = userLogin();
CommentModel PC = null;
mockMvc.perform(post("/user/comment/create") mockMvc.perform(post("/user/comment/create")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.fromObject(commentReq).toString()) .content(JSONObject.fromObject(commentReq).toString())
@@ -52,22 +46,19 @@ public class CommentControllerTest extends BaseTest {
assertEquals(SUCCESS.getCode(), object.getInt(Code)); assertEquals(SUCCESS.getCode(), object.getInt(Code));
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
assertNotEquals(0, model.getId()); assertNotEquals(0, model.getId());
assertEquals(commentReq.getPid().longValue(), model.getPid()); assertEquals(commentReq.getPid(), model.getPid().longValue());
assertEquals(-1, model.getPid()); assertEquals(-1, model.getPid().longValue());
assertEquals(commentReq.getComment(), model.isComment());
assertEquals(commentReq.getContent(), model.getContent()); assertEquals(commentReq.getContent(), model.getContent());
assertNotNull(model.getDate()); assertNotNull(model.getDate());
assertNotNull(model.getAuthorName()); assertNotNull(model.getFromUser());
assertNotNull(model.getAuthorAvatarImgUrl()); assertNull(model.getToUser());
}); });
Article article = articleMapper.getLastestArticle();
// 测试评论 commentReq.setPagePath("/article/" + article.getId());
commentReq.setArticleID(article.getId());
commentReq.setComment(true);
commentReq.setContent(UUID.randomUUID().toString()); commentReq.setContent(UUID.randomUUID().toString());
commentReq.setPid(-1L); commentReq.setPid(-1L);
commentReq.setResponseId(null); commentReq.setToUserId(2);
mockMvc.perform(post("/user/comment/create") mockMvc.perform(post("/user/comment/create")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.fromObject(commentReq).toString()) .content(JSONObject.fromObject(commentReq).toString())
@@ -78,58 +69,50 @@ public class CommentControllerTest extends BaseTest {
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
// 响应数据的完整性 // 响应数据的完整性
assertNotEquals(0, model.getId()); assertNotEquals(0, model.getId());
assertEquals(commentReq.getPid().longValue(), model.getPid()); assertEquals(commentReq.getPid(), model.getPid().longValue());
assertEquals(-1, model.getPid()); assertEquals(-1, model.getPid().longValue());
assertEquals(commentReq.getComment(), model.isComment());
assertEquals(commentReq.getContent(), model.getContent()); assertEquals(commentReq.getContent(), model.getContent());
assertEquals(commentReq.getArticleID().longValue(), model.getArticleID()); assertEquals(commentReq.getPagePath(), "/article/" + article.getId());
assertNotNull(model.getDate()); assertNotNull(model.getDate());
assertNotNull(model.getAuthorName()); assertNotNull(model.getFromUser());
assertNotNull(model.getAuthorAvatarImgUrl()); assertNotNull(model.getToUser());
}); });
// 测试二级回复 // 测试二级回复
Comment lastestComment = commentMapper.getLastestComment(); Comment lastestComment = commentMapper.getLastestComment();
commentReq.setArticleID(lastestComment.getArticleID()); commentReq.setPagePath("/article/" + article.getId());
commentReq.setComment(lastestComment.getType());
commentReq.setContent(UUID.randomUUID().toString()); commentReq.setContent(UUID.randomUUID().toString());
commentReq.setPid(lastestComment.getId()); commentReq.setPid(lastestComment.getId());
commentReq.setResponseId(null);
mockMvc.perform(post("/user/comment/create") mockMvc.perform(post("/user/comment/create")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.fromObject(commentReq).toString()) .content(JSONObject.fromObject(commentReq).toString())
.header("Authorization", token) .header("Authorization", token)
).andDo(MockMvcResultHandlers.print()).andDo(result -> { ).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code)); assertEquals(SUCCESS.getCode(), object.getInt(Code));
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
// 重新获取父评论信息 // 重新获取父评论信息
Comment pCommon = commentMapper.findCommentById(lastestComment.getId()); Comment pCommon = commentMapper.findCommentById(lastestComment.getId());
assertEquals(pCommon.getId().longValue(), model.getPid()); assertEquals(pCommon.getId(), 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);
}); });
} }
@Test @Test
public void deleteTest() throws Exception { public void deleteTest() throws Exception {
// 准备数据 // 准备数据
Comment c = new Comment(); User from = userMapper.findByEmail("zh56462271@qq.com");
c.setArticleID(-1L); assertNotNull(from);
c.setType(true); User to = userMapper.findByEmail("a@celess.cn");
c.setAuthorID(2L); assertNotNull(to);
c.setDate(new Date());
c.setPid(-1L); Comment comment = new Comment();
commentMapper.insert(c); comment.setContent(randomStr(8));
Comment comment = commentMapper.getLastestComment(); comment.setFromUser(from);
comment.setToUser(to);
comment.setPagePath("/tags");
comment.setPid(-1L);
commentMapper.insert(comment);
comment = commentMapper.findCommentById(comment.getId());
// 接口测试 // 接口测试
long id = comment.getId(); long id = comment.getId();
assertNotEquals(0, id); 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 -> { mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); 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(); Comment comment = commentMapper.getLastestComment();
CommentReq commentReq = new CommentReq(); CommentReq commentReq = new CommentReq();
commentReq.setId(comment.getId()); commentReq.setId(comment.getId());
commentReq.setPid(comment.getPid());
commentReq.setContent(UUID.randomUUID().toString()); commentReq.setContent(UUID.randomUUID().toString());
commentReq.setArticleID(comment.getArticleID());
// 不合法数据 setResponseId // 不合法数据 setResponseId
commentReq.setResponseId("xxxx");
commentReq.setComment(comment.getType());
mockMvc.perform(put("/user/comment/update") mockMvc.perform(put("/user/comment/update")
.content(JSONObject.fromObject(commentReq).toString()) .content(JSONObject.fromObject(commentReq).toString())
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
@@ -165,161 +144,7 @@ public class CommentControllerTest extends BaseTest {
assertEquals(SUCCESS.getCode(), object.getInt(Code)); assertEquals(SUCCESS.getCode(), object.getInt(Code));
CommentModel c = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class); CommentModel c = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
assertEquals(commentReq.getContent(), c.getContent()); 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());
});
});
}
} }