发表于: 2019-11-19 23:18:42
1 1190
今天完成的事情:
一个棘手问题,返回留言列表,且判断当前用户给哪条留言点过赞。
先把代码写下,还没测试。明天 测试下。。
目前是返回两个列表,一个是留言列表,一个是留言与点赞关联列表。只要某个留言被当前用户点过赞,就会返回一个列表,里面是留言id与之对应的点赞id。
通过学习对象查留言列表,再根据留言id,type,userId查点赞,
/**
* 学习对象内的留言列表查看
*/
@GetMapping("/a/u/list/comment")
public Map<String, Object> commentListSelect(HttpServletRequest request, Long id,
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size) {
HashMap<String, Object> result = new HashMap<>(16);
long userId = JWTUtil.getUserIdFromToken(request);
log.info("用户{}获取第{}页共{}行学习对象{}的评论列表", userId, page, size, id);
try {
Comment comment = new Comment();
comment.setStudyObjectId(id);
PageHelper.startPage(page, size, "c.create_at desc");
/*1.查询留言列表*/
List<Comment> commentList = commentServer.selectSelective(comment);
PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
/*获取总数据量*/
Long total = pageInfo.getTotal();
/*留言列表与当前用户对应的点赞id*/
Map<String, Object> commentLikeMap = new HashMap(16);
commentLikeMap.put("commentList", commentList);
if (null != commentList.get(0)) {
/*创建关联当前用户留言id与点赞id的集合*/
List userCommentLike= new ArrayList();
/*当前用户是否给此留言列表中的留言点赞集合*/
for (Comment comment1 : commentList) {
/*2.查询点赞表,判断当前用户是否给此留言点赞*/
StudyLike studyLike = new StudyLike();
studyLike.setType(20);
studyLike.setBizId(comment1.getId());
studyLike.setCreateBy(userId);
List<StudyLike> userLikeList = studyLikeServer.selectSelective(studyLike);
if (!userLikeList.isEmpty()) {
/*获取到当前用户给当前留言的点赞id*/
long studyLikeId = userLikeList.get(0).getId();
Map<String, Object> map = new HashMap(16);
map.put("commentId", comment1.getId());
map.put("studyLikeId", studyLikeId);
userCommentLike.add(map);
}
}
commentLikeMap.put("userLike", userCommentLike);
}
result.put("code", SUCCESS.getCode());
result.put("msg", SUCCESS.getMsg());
result.put("total", total);
result.put("data", commentLikeMap);
return result;
} catch (Exception e) {
e.printStackTrace();
result.put("code", REQUEST_FAILED.getCode());
result.put("msg", REQUEST_FAILED.getMsg());
return result;
}
}
明天计划的事情:
把这个接口测试下。呼,还是画图好理解点
遇到的问题:
收获:
如果一个接口只需要接收前端传的id,那可以把id放在url上。不管什么请求。
评论