目录
1:根据id查询单条动态
1.1:接口分析
API 接口文档: http://192.168.136.160:3000/project/19/interface/api/151
注意: 单条动态 和 查询评论列表 ,同时完成,app端才可以显示内容
1.2:流程分析
根据id查询对应动态详情信息,接收到前端传来的id信息,到数据库中查询指定的数据,将数据处理封装返回前端,这个功能需要和该动态的所有评论一起实现才能在探花app上显示页面。
1.3:代码实现
接收请求的app服务端
app-server服务:controller层
@GetMapping("/{id}")
public ResponseEntity findmovement(@PathVariable("id") ObjectId id){
MovementVo findmovement = movementService.findmovement(id);
System.out.println(findmovement);
return ResponseEntity.ok(findmovement);
}
app-server服务:service层
public MovementVo findmovement(ObjectId id) {
//处理评论
Movement movement = movementApi.findmovement(id);
UserInfo userInfo = userInfoApi.findUser(movement.getUserId());
MovementVo movementVo = new MovementVo(movement.getId().toString(),
userInfo.getId(),userInfo.getAvatar(),userInfo.getNickname(),userInfo.getGender(),Integer.valueOf(userInfo.getAge()),
userInfo.getTags().split(","),movement.getTextContent(),movement.getMedias(),"500米",movement.getCreated().toString()
,movement.getLikeCount(),movement.getCommentCount(),movement.getLoveCount(),0,0);
return movementVo;
}
接口模块:专门封装接口的模块
Movement findmovement(ObjectId id);
public UserInfo findUser(Long id);
db提供者服务:mysql
@Override
public UserInfo findUser(Long id) {
UserInfo userInfo = this.userInfoMapper.selectById(id);
return userInfo;
}
mongo提供者服务:mongo
@Override
public Movement findmovement(ObjectId id) {
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
Movement one = mongoTemplate.findOne(query, Movement.class);
return one;
}
2:发布评论
2.1:接口分析
接口: http://192.168.136.160:3000/project/19/interface/api/154
2.2:流程分析
接收到前端传入的发布动态参数,将图片信息上传到阿里云上面,将数据处理保存到数据库中。
2.3:代码实现
接收请求的app服务端
app-server服务:controller层
@PostMapping
public ResponseEntity fabudongtai(@RequestParam("imageContent") List<MultipartFile> imageContent,PostUpdateVo postUpdateVo){
movementService.fabudongtai(imageContent,postUpdateVo,UserHolder.getUserId(),UserHolder.getMobile());
return ResponseEntity.ok(null);
}
app-server服务:service层
public void fabudongtai(List<MultipartFile> imageContent, PostUpdateVo postUpdateVo, Long userId, String mobile) {
for (MultipartFile multipartFile : imageContent) {
String upload = ossTemplate.upload(multipartFile);
System.out.println(upload);
postUpdateVo.getImages().add(upload);
}
movementApi.fabudongtai(postUpdateVo,userId,mobile);
}
接口模块:专门封装接口的模块
void fabudongtai(PostUpdateVo postUpdateVo, Long userId, String mobile);
mongo提供者服务:mongo
@Override
public void fabudongtai(PostUpdateVo postUpdateVo, Long userId, String mobile) {
//保存动态到mongo动态表中
Movement movement = new Movement();
Long movement1 = idWorker.getNextId("movement");
movement.setPid(movement1);
movement.setUserId(userId);
movement.setTextContent(postUpdateVo.getTextContent());
movement.setMedias(postUpdateVo.getImages().toArray(new String[0]));
movement.setState(0);
movement.setLongitude(Double.valueOf(postUpdateVo.getLongitude()));
movement.setLatitude(Double.valueOf(postUpdateVo.getLatitude()));
movement.setLocationName(postUpdateVo.getLocation());
movement.setCreated(new Date().getTime());
//保存动态表
Movement save = mongoTemplate.save(movement);
//查询好友列表
//构建条件
Query query = new Query();
Criteria criteria = Criteria.where("userId").is(userId);
query.addCriteria(criteria);
List<Friend> friends = mongoTemplate.find(query, Friend.class);
//保存时间线数据
for (Friend friend : friends) {
MovementTimeline movementTimeline = new MovementTimeline(save.getCreated(),friend.getFriendId(),save.getId(),save.getUserId());
mongoTemplate.save(movementTimeline);
}
//处理结束
}
3:查询评论列表
3.1:接口分析
接口地址: http://192.168.136.160:3000/project/19/interface/api/178
3.2:流程分析
通过动态id,到评论表中查找对应的所有评论
3.3:代码实现
接收请求的app服务端
app-server服务:controller层
@GetMapping
public ResponseEntity findComments(@RequestParam("movementId") ObjectId movementId, PublicPageVo publicPageVo){
System.out.println(movementId);
ResultList comments = commentService.findComments(movementId, publicPageVo);
return ResponseEntity.ok(comments);
}
app-server服务:service层
public ResultList findComments(ObjectId movementId, PublicPageVo publicPageVo) {
ResultList comments = commentApi.findComments(movementId, publicPageVo);
List<Comment> items = comments.getItems();
//传入用户APi
Map<Long, UserInfo> userInfoMap = userInfoApi.findUserByids(items.stream().map(comment -> comment.getUserId()).collect(Collectors.toList()));
List<CommentVo> commentVoList = new ArrayList<>();
//处理返回数据
for (Comment comment : items) {
UserInfo userInfo = userInfoMap.get(comment.getUserId());
CommentVo commentVo = new CommentVo(comment.getId().toString(),userInfo.getAvatar(),userInfo.getNickname(),comment.getContent(),
comment.getCreated().toString(), (int) comment.getLikeCount(),0);
commentVoList.add(commentVo);
}
comments.setItems(commentVoList);
return comments;
}
接口模块:专门封装接口的模块
Map<Long,UserInfo> findUserByids(List<Long> collect);
ResultList findComments(ObjectId movementId, PublicPageVo publicPageVo);
db提供者服务:mysql
@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提供者服务:mongo
@Override
public ResultList findComments(ObjectId movementId, PublicPageVo publicPageVo) {
ResultList<Comment> objectResultList = new ResultList<>();
//拼凑条件
Query query = new Query();
//添加条件
query.addCriteria(Criteria.where("publishId").is(movementId));
long count = mongoTemplate.count(query, Comment.class);
query.limit(publicPageVo.getPagesize().intValue()).skip((publicPageVo.getPage()-1)*publicPageVo.getPagesize()).with(Sort.by(Sort.Order.desc("created")));
List<Comment> comments = mongoTemplate.find(query, Comment.class);
objectResultList.setItems(comments);
objectResultList.setCounts(Integer.valueOf(count+""));
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;
}