目录
1:圈子
1.1:查询好友动态
1.1.1:接口分析
1.1.2:流程分析
1.1.2:代码实现
1.2:查询推荐动态
1.2.1:接口分析
1.2.2:流程分析
1.2.3:代码实现
1:圈子
1.1:查询好友动态
1.1.1:接口分析
API接口文档:http://192.168.136.160:3000/project/19/interface/api/148
1.1.2:流程分析
当你的一个好友发了一个动态,他可能不想让你不想看到,那你就没有看到他这条动态的权限,也就是说这条动态在数据库中的标注没有你这个好友的查看,这个功能的实现就是,根据当前用户id到数据库中的控制动态谁可以查看的表中(时间线表)去查找对应的好友想让你看到的数据。时间线表通过判断当前用户的id,查找出来对应的动态id数据。
1.1.2:代码实现
与前端交互的app-server模块
controller层实现
@GetMapping
public ResponseEntity friendmovements(PublicPageVo publicPageVo){
ResultList friendmovements = movementService.friendmovements(publicPageVo,UserHolder.getUserId());
return ResponseEntity.ok(friendmovements);
}
service层实现
public ResultList friendmovements(PublicPageVo publicPageVo, Long userId) {
ResultList friendmovements = movementApi.friendmovements(publicPageVo, userId);
List<Movement> items = friendmovements.getItems();
List<MovementVo> movements = new ArrayList<>();
for (Movement movement : items) {
UserInfo userInfo = userInfoApi.findUser(movement.getUserId());
extracted(userId, movements, movement, userInfo);
}
friendmovements.setItems(movements);
return friendmovements;
}
统一封装接口的模块
ResultList friendmovements(PublicPageVo publicPageVo,Long Userid);
public UserInfo findUser(Long id);
提供者模块(提供相关接口的实现)
mongo提供者:
@Override
public ResultList friendmovements(PublicPageVo publicPageVo, Long userId) {
//创建封装类
ResultList movementVoResultList = new ResultList<>();
//通过userId
Query query = new Query();
query.addCriteria(Criteria.where("friendId").is(userId));
long count = mongoTemplate.count(query,MovementTimeline.class);
query.skip((publicPageVo.getPage()-1L)*publicPageVo.getPagesize()).limit(publicPageVo.getPagesize().intValue());
List<MovementTimeline> movementTimelineList = mongoTemplate.find(query, MovementTimeline.class);
System.out.println(movementTimelineList);
//处理数据,获得动态id
List<ObjectId> movementId = CollUtil.getFieldValues(movementTimelineList, "movementId", ObjectId.class);
//查询对应的动态
Query movementQuery = new Query(Criteria.where("id").in(movementId)).with(Sort.by(Sort.Order.desc("created")));
List<Movement> movements = mongoTemplate.find(movementQuery, Movement.class);
//设置列表
movementVoResultList.setItems(movements);
//设置当前页码
movementVoResultList.setPage(publicPageVo.getPage().intValue());
//设置总记录数
movementVoResultList.setCounts((int) count);
//设置页大小
movementVoResultList.setPagesize(publicPageVo.getPagesize().intValue());
//设置总页数
movementVoResultList.setPages((int) (count%publicPageVo.getPagesize() == 0 ? count/ publicPageVo.getPagesize():(count/ publicPageVo.getPagesize())+1));
return movementVoResultList;
}
db提供者服务:
@Override
public UserInfo findUser(Long id) {
UserInfo userInfo = this.userInfoMapper.selectById(id);
return userInfo;
}
1.2:查询推荐动态
1.2.1:接口分析
接口文档:http://192.168.136.160:3000/project/19/interface/api/145
1.2.2:流程分析
推荐用户的值在redis缓存中,将其取出查找数据,封装数据。
1.2.3:代码实现
与前端交互的app-server模块
controller层实现
/**
* 推荐动态
* @return
*/
@GetMapping("/recommend")
public ResponseEntity recommendmovements(PublicPageVo publicPageVo){
ResultList recommendmovements = movementService.recommendmovements(publicPageVo, UserHolder.getUserId());
return ResponseEntity.ok(recommendmovements);
}
service层实现
public ResultList recommendmovements(PublicPageVo publicPageVo, Long userId) {
//处理redis中数据
String key = "MOVEMENTS_RECOMMEND_"+userId;
//找到推荐评论的推荐pid
String pids = stringRedisTemplate.opsForValue().get(key);
//切割处理数据
String[] pidList = pids.split(",");
//查找对应的评论和userid
ResultList recommendmovements = movementApi.recommendmovements(pidList,publicPageVo);
List<Movement> items = recommendmovements.getItems();
//传入用户APi
Map<Long, UserInfo> userInfoMap = userInfoApi.findUserByids(items.stream().map(movement -> movement.getUserId()).collect(Collectors.toList()));
List<MovementVo> movementVoList = new ArrayList<>();
//处理返回数据
for (Movement movement : items) {
UserInfo userInfo = userInfoMap.get(movement.getUserId());
extracted(userId, movementVoList, movement, userInfo);
}
recommendmovements.setItems(movementVoList);
return recommendmovements;
}
统一封装接口的模块
ResultList recommendmovements(String[] pidList, PublicPageVo publicPageVo);
Map<Long,UserInfo> findUserByids(List<Long> collect);
db提供者模块(提供相关接口的实现)
@Override
public Map<Long, UserInfo> findUserByids(List<Long> collect) {
Map<Long,UserInfo> userInfoMap = new HashMap<>();
//传入的是用户id
for (Long userid : collect) {
UserInfo userInfo = userInfoMapper.selectById(userid);
if (userInfo!=null){
userInfoMap.put(userInfo.getId(),userInfo);
}
}
return userInfoMap;
}
mongo提供者模块(提供相关接口的实现)
@Override
public ResultList recommendmovements(String[] pidList, PublicPageVo publicPageVo) {
ResultList<Movement> objectResultList = new ResultList<>();
//拼凑条件
Query query = new Query();
Integer [] newpidList = new Integer[pidList.length];
for (int i = 0; i < pidList.length; i++) {
newpidList[i] = Integer.valueOf(pidList[i]);
}
query.addCriteria(Criteria.where("pid").in(newpidList)).limit(publicPageVo.getPagesize().intValue()).skip((publicPageVo.getPage()-1)* publicPageVo.getPagesize());
List<Movement> movements = mongoTemplate.find(query, Movement.class);
System.out.println(movements);
long count = mongoTemplate.count(query, Movement.class);
objectResultList.setItems(movements);
objectResultList.setPagesize(publicPageVo.getPagesize().intValue());
objectResultList.setPage(publicPageVo.getPage().intValue());
objectResultList.setPages((int) (count%publicPageVo.getPagesize() == 0 ? count/ publicPageVo.getPagesize():(count/ publicPageVo.getPagesize())+1));
//返回处理的数据
return objectResultList;
}