课程说明
- 点赞消息列表
- 喜欢消息列表
- 评论消息列表
- 公告列表
- 个人主页
- 聊一下功能
- 谁看过我的功能
1、消息点赞、喜欢、评论列表
在消息模块中的点赞、喜欢、评论列表,是别人对自己发布的内容的操作,其实现基本一致,所以在一起实现。
效果:
点赞列表接口地址:https://mock-java.itheima.net/project/35/interface/api/779
评论列表接口地址:https://mock-java.itheima.net/project/35/interface/api/785
喜欢列表接口地址:https://mock-java.itheima.net/project/35/interface/api/791
1.1、dubbo服务
1.1.1、定义接口
//com.tanhua.dubbo.server.api.QuanZiApi
/**
* 查询对我的点赞消息列表
*
* @return
*/
PageInfo<Comment> queryLikeCommentListByUser(Long userId, Integer page, Integer pageSize);
/**
* 查询对我的喜欢消息列表
*
* @return
*/
PageInfo<Comment> queryLoveCommentListByUser(Long userId, Integer page, Integer pageSize);
/**
* 查询对我的评论消息列表
*
* @return
*/
PageInfo<Comment> queryCommentListByUser(Long userId, Integer page, Integer pageSize);
1.1.2、编写实现
//com.tanhua.dubbo.server.api.QuanziApiImpl
@Override
public PageInfo<Comment> queryLikeCommentListByUser(Long userId, Integer page, Integer pageSize) {
return this.queryCommentListByUser(userId, CommentType.LIKE, page, pageSize);
}
@Override
public PageInfo<Comment> queryLoveCommentListByUser(Long userId, Integer page, Integer pageSize) {
return this.queryCommentListByUser(userId, CommentType.LOVE, page, pageSize);
}
@Override
public PageInfo<Comment> queryCommentListByUser(Long userId, Integer page, Integer pageSize) {
return this.queryCommentListByUser(userId, CommentType.COMMENT, page, pageSize);
}
private PageInfo<Comment> queryCommentListByUser(Long userId, CommentType commentType, Integer page, Integer pageSize) {
PageRequest pageRequest = PageRequest.of(page - 1, pageSize,
Sort.by(Sort.Order.desc("created")));
Query query = new Query(Criteria
.where("publishUserId").is(userId)
.and("commentType").is(commentType.getType())).with(pageRequest);
List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);
PageInfo<Comment> pageInfo = new PageInfo<>();
pageInfo.setPageNum(page);
pageInfo.setPageSize(pageSize);
pageInfo.setRecords(commentList);
return pageInfo;
}
1.2、APP接口服务
1.2.1、MessageCommentVo
根据接口定义vo对象。
package com.tanhua.server.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MessageCommentVo {
private String id;
private String avatar;
private String nickname;
private String createDate; //格式:2019-09-08 10:07
}
1.2.3、IMController
//com.tanhua.server.controller.IMController
/**
* 查询消息点赞列表
*
* @param page
* @param pageSize
* @return
*/
@GetMapping("likes")
public ResponseEntity<PageResult> queryLikeCommentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
try {
PageResult pageResult = this.imService.queryLikeCommentList(page, pageSize);
return ResponseEntity.ok(pageResult);
} catch (Exception e) {
log.error("查询点赞列表失败~ ", e);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
/**
* 查询消息评论列表
*
* @param page
* @param pageSize
* @return
*/
@GetMapping("comments")
public ResponseEntity<PageResult> queryUserCommentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
try {
PageResult pageResult = this.imService.queryUserCommentList(page, pageSize);
return ResponseEntity.ok(pageResult);
} catch (Exception e) {
log.error("查询评论列表失败~ ", e);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
/**
* 查询消息喜欢列表
*
* @param page
* @param pageSize
* @return
*/
@GetMapping("loves")
public ResponseEntity<PageResult> queryLoveCommentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
try {
PageResult pageResult = this.imService.queryLoveCommentList(page, pageSize);
return ResponseEntity.ok(pageResult);
} catch (Exception e) {
log.error("查询喜欢列表失败~ ", e);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
1.2.4、IMService
//com.tanhua.server.service.IMService
public PageResult queryLikeCommentList(Integer page, Integer pageSize) {
User user = UserThreadLocal.get();
PageInfo<Comment> pageInfo = this.quanZiApi.queryLikeCommentListByUser(user.getId(), page, pageSize);
return this.fillUserCommentList(pageInfo);
}
public PageResult queryLoveCommentList(Integer page, Integer pageSize) {
User user = UserThreadLocal.get();
PageInfo<Comment> pageInfo = this.quanZiApi.queryLoveCommentListByUser(user.getId(), page, pageSize);
return this.fillUserCommentList(pageInfo);
}
public PageResult queryUserCommentList(Integer page, Integer pageSize) {
User user = UserThreadLocal.get();
PageInfo<Comment> pageInfo = this.quanZiApi.queryCommentListByUser(user.getId(), page, pageSize);
return this.fillUserCommentList(pageInfo);
}
private PageResult fillUserCommentList(PageInfo<Comment> pageInfo){
PageResult pageResult = new PageResult();
pageResult.setPage(pageInfo.getPageNum());
pageResult.setPagesize(pageInfo.getPageSize());
List<Comment> records = pageInfo.getRecords();
if(CollUtil.isEmpty(records)){
//没有查询到数据
return pageResult;
}
List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIdList);
List<MessageCommentVo> messageCommentVoList = new ArrayList<>();
for (Comment comment : records) {
for (UserInfo userInfo : userInfoList) {
if(ObjectUtil.equals(comment.getUserId(), userInfo.getUserId())){
MessageCommentVo messageCommentVo = new MessageCommentVo();
messageCommentVo.setId(comment.getId().toHexString());
messageCommentVo.setAvatar(userInfo.getLogo());
messageCommentVo.setNickname(userInfo.getNickName());
messageCommentVo.setCreateDate(DateUtil.format(new Date(comment.getCreated()), "yyyy-MM-dd HH:mm"));
messageCommentVoList.add(messageCommentVo);
break;
}
}
}
pageResult.setItems(messageCommentVoList);
return pageResult;
}
1.3、测试
2、公告列表
公告是后台系统对所有用户发布的公告消息。
效果:
接口地址:https://mock-java.itheima.net/project/35/interface/api/797
2.1、表结构
CREATE TABLE `tb_announcement` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL COMMENT '标题