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

@@ -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));
}
}

View File

@@ -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;

View File

@@ -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<CommentModel> respComment;
private int status;
}

View File

@@ -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;
}

View File

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

View File

@@ -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<CommentModel> retrievePage(Boolean isComment, int page, int count);
PageData<CommentModel> retrievePage(String pagePath, int page, int count);
/**
* 通过pid获取数据
*
* @param pid 父id
* @param pid 父id
* @return 分页数据
*/
List<CommentModel> 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<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获取数据
*
* @param isComment true评论 false留言
* @param pid 父id
* @param count 单页数据量
* @param page 数据页
* @param pagePath pagePath
* @param pid 父id
* @param count 单页数据量
* @param page 数据页
* @return 分页数据
*/
PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count);
PageData<CommentModel> 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<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.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<CommentModel> retrievePage(Boolean isComment, int page, int count) {
public PageData<CommentModel> retrievePage(String pagePath, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByType(isComment);
return pageTrans(commentList);
List<Comment> list = commentMapper.findAllByPagePath(pagePath);
return pageTrans(list);
}
@Override
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<>();
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 -> {
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<CommentModel>(p, modelList);
}
}

View File

@@ -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;
}
}

View File

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