修改部分api以及响应数据结构

This commit is contained in:
小海
2020-04-06 15:26:41 +08:00
parent ec0b17151c
commit 1cd6e0d7c9
5 changed files with 36 additions and 48 deletions

View File

@@ -58,15 +58,11 @@ public class CommentController {
* 通过pid获取数据 * 通过pid获取数据
* *
* @param pid * @param pid
* @param count
* @param page
* @return * @return
*/ */
@GetMapping("/comment/pid/{pid}") @GetMapping("/comment/pid/{pid}")
public Response retrievePage(@PathVariable("pid") long pid, public Response retrievePage(@PathVariable("pid") long pid) {
@RequestParam(value = "count", required = false, defaultValue = "10") int count, return ResponseUtil.success(commentService.retrievePageByPid(pid));
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
return ResponseUtil.success(commentService.retrievePageByPid(pid, page, count));
} }
/** /**

View File

@@ -3,7 +3,7 @@ package cn.celess.blog.entity.model;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.Date; import java.util.List;
/** /**
* @author : xiaohai * @author : xiaohai
@@ -53,5 +53,7 @@ public class CommentModel {
*/ */
private long pid = -1; private long pid = -1;
private List<CommentModel> respComment;
} }

View File

@@ -5,6 +5,8 @@ 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;
import java.util.List;
/** /**
* @author : xiaohai * @author : xiaohai
* @date : 2019/03/29 16:58 * @date : 2019/03/29 16:58
@@ -49,11 +51,9 @@ public interface CommentService {
* 通过pid获取数据 * 通过pid获取数据
* *
* @param pid 父id * @param pid 父id
* @param count 单页数据量
* @param page 数据页
* @return 分页数据 * @return 分页数据
*/ */
PageInfo<CommentModel> retrievePageByPid(long pid, int page, int count); List<CommentModel> retrievePageByPid(long pid);
/** /**

View File

@@ -11,6 +11,7 @@ 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.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.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -112,45 +113,36 @@ public class CommentServiceImpl implements CommentService {
public PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count) { public PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count) {
PageHelper.startPage(page, count); PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByType(isComment); List<Comment> commentList = commentMapper.findAllByType(isComment);
PageInfo pageInfo = new PageInfo(commentList); return pageTrans(commentList);
pageInfo.setList(list2List(commentList));
return pageInfo;
} }
@Override @Override
public PageInfo<CommentModel> retrievePageByPid(long pid, int page, int count) { public List<CommentModel> retrievePageByPid(long pid) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByPId(pid); List<Comment> commentList = commentMapper.findAllByPId(pid);
PageInfo pageInfo = new PageInfo(commentList); List<CommentModel> modelList = new ArrayList<>();
pageInfo.setList(list2List(commentList)); commentList.forEach(m -> modelList.add(trans(m)));
return pageInfo; return modelList;
} }
@Override @Override
public PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count) { public PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count) {
PageHelper.startPage(page, count); PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByArticleIDAndPId(articleID, pid); List<Comment> commentList = commentMapper.findAllByArticleIDAndPId(articleID, pid);
PageInfo pageInfo = new PageInfo(commentList); return pageTrans(commentList);
pageInfo.setList(list2List(commentList));
return pageInfo;
} }
@Override @Override
public PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count) { public PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count) {
PageHelper.startPage(page, count); PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findCommentsByTypeAndPId(isComment, pid); List<Comment> commentList = commentMapper.findCommentsByTypeAndPId(isComment, pid);
PageInfo pageInfo = new PageInfo(commentList); return pageTrans(commentList);
pageInfo.setList(list2List(commentList));
return pageInfo;
} }
@Override @Override
public PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count) { public PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count) {
PageHelper.startPage(page, count); PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByAuthorIDAndType(redisUserUtil.get().getId(), isComment); List<Comment> commentList = commentMapper.findAllByAuthorIDAndType(redisUserUtil.get().getId(), isComment);
PageInfo pageInfo = new PageInfo(commentList); return pageTrans(commentList);
pageInfo.setList(list2List(commentList));
return pageInfo;
} }
@@ -158,17 +150,7 @@ public class CommentServiceImpl implements CommentService {
public PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count) { public PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count) {
PageHelper.startPage(page, count); PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByType(isComment); List<Comment> commentList = commentMapper.findAllByType(isComment);
PageInfo pageInfo = new PageInfo(commentList); return pageTrans(commentList);
pageInfo.setList(list2List(commentList));
return pageInfo;
}
private List<CommentModel> list2List(List<Comment> commentList) {
List<CommentModel> content = new ArrayList<>();
for (Comment c : commentList) {
content.add(trans(c));
}
return content;
} }
private CommentModel trans(Comment comment) { private CommentModel trans(Comment comment) {
@@ -189,4 +171,16 @@ public class CommentServiceImpl implements CommentService {
return commentModel; 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);
model.setRespComment(this.retrievePageByPid(model.getId()));
modelList.add(model);
});
p.setList(modelList);
return p;
}
} }

View File

@@ -9,6 +9,7 @@ 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 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;
@@ -198,22 +199,17 @@ public class CommentControllerTest extends BaseTest {
@Test @Test
public void retrievePage() throws Exception { public void retrievePage() throws Exception {
long pid = 17; long pid = -1;
mockMvc.perform(get("/comment/pid/" + pid + "?articleId=3&page=1&count=10")).andDo(result -> { mockMvc.perform(get("/comment/pid/" + pid)).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));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class); JSONArray jsonArray = object.getJSONArray(Result);
assertNotEquals(0, pageInfo.getStartRow()); assertNotEquals(0, jsonArray.size());
assertNotEquals(0, pageInfo.getEndRow()); jsonArray.forEach(o -> {
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class); CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertEquals(3, model.getArticleID());
assertNotNull(model.getDate()); assertNotNull(model.getDate());
assertNotNull(model.getAuthorName()); assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl()); assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getArticleTitle());
assertNotNull(model.getContent()); assertNotNull(model.getContent());
assertNotNull(model.getResponseId()); assertNotNull(model.getResponseId());
}); });