【探花交友】day04—圈子功能实现(三)

简介: 【探花交友】day04—圈子功能实现(三)

3.2、发布动态

后续的测试:使用106(13800138000)和1号用户(13500000000)

3.2.0、思路步骤

3.2.1、MovementsController

tanhua-server工程编写MovementsController,完成发布动态功能

1. @RestController
2. @RequestMapping("/movements")
3. public class MovementController {
4. 
5. @Autowired
6. private MovementService movementService;
7. 
8. @Autowired
9. private CommentsService commentsService;
10. 
11. /**
12.      * 发布动态
13.      */
14. @PostMapping
15. public ResponseEntity movements(Movement movement,
16.                                     MultipartFile imageContent[]) throws IOException {
17.         movementService.publishMovement(movement,imageContent);
18. return ResponseEntity.ok(null);
19.     }
20. }

3.2.2、编写service

tanhua-server工程编写MovementsService,完成发布动态功能

1. /**
2.  * 发布动态
3.  */
4. public void publishMovement(Movement movement, MultipartFile[] imageContent) throws IOException {
5. //1、判断发布动态的内容是否存在
6. if(StringUtils.isEmpty(movement.getTextContent())) {
7. throw  new BusinessException(ErrorResult.contentError());
8.     }
9. //2、获取当前登录的用户id
10. Long userId = UserHolder.getUserId();
11. //3、将文件内容上传到阿里云OSS,获取请求地址
12.     List<String> medias = new ArrayList<>();
13. for (MultipartFile multipartFile : imageContent) {
14. String upload = ossTemplate.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
15.         medias.add(upload);
16.     }
17. //4、将数据封装到Movement对象
18.     movement.setUserId(userId);
19.     movement.setMedias(medias);
20. //5、调用API完成发布动态
21.     movementApi.publish(movement);
22. }

3.2.3、API层

1. package com.tanhua.dubbo.api;
2. 
3. import com.tanhua.dubbo.utils.IdWorker;
4. import com.tanhua.dubbo.utils.TimeLineService;
5. import com.tanhua.model.mongo.Movement;
6. import com.tanhua.model.vo.PageResult;
7. import org.apache.dubbo.config.annotation.DubboService;
8. import org.springframework.beans.factory.annotation.Autowired;
9. import org.springframework.data.domain.Sort;
10. import org.springframework.data.mongodb.core.MongoTemplate;
11. import org.springframework.data.mongodb.core.query.Criteria;
12. import org.springframework.data.mongodb.core.query.Query;
13. 
14. import java.util.List;
15. 
16. @DubboService
17. public class MovementApiImpl implements MovementApi{
18. 
19. @Autowired
20. private MongoTemplate mongoTemplate;
21. 
22. @Autowired
23. private IdWorker idWorker;
24. 
25. @Autowired
26. private TimeLineService timeLineService;
27. 
28. //发布动态
29. public void publish(Movement movement) {
30. //1、保存动态详情
31. try {
32. //设置PID
33.             movement.setPid(idWorker.getNextId("movement"));
34. //设置时间
35.             movement.setCreated(System.currentTimeMillis());
36. //movement.setId(ObjectId.get());
37.             mongoTemplate.save(movement);
38. //            //2、查询当前用户的好友数据
39. //            Criteria criteria = Criteria.where("userId").is(movement.getUserId());
40. //            Query query = Query.query(criteria);
41. //            List<Friend> friends = mongoTemplate.find(query, Friend.class);
42. //            Thread.sleep(10000);
43. //            //3、循环好友数据,构建时间线数据存入数据库
44. //            for (Friend friend : friends) {
45. //                MovementTimeLine timeLine = new MovementTimeLine();
46. //                timeLine.setMovementId(movement.getId());
47. //                timeLine.setUserId(friend.getUserId());
48. //                timeLine.setFriendId(friend.getFriendId());
49. //                timeLine.setCreated(System.currentTimeMillis());
50. //                mongoTemplate.save(timeLine);
51. //            }
52.             timeLineService.saveTimeLine(movement.getUserId(),movement.getId());
53.         } catch (Exception e) {
54. //忽略事务处理
55.             e.printStackTrace();
56.         }
57.     }
58. }

3.2.4、异步处理工具类

1. @Component
2. public class TimeLineService {
3. 
4. @Autowired
5. private MongoTemplate mongoTemplate;
6. 
7. @Async
8. public void saveTimeLine(Long userId, ObjectId movementId) {
9. //2、查询当前用户的好友数据
10. Criteria criteria = Criteria.where("userId").is(userId);
11. Query query = Query.query(criteria);
12.         List<Friend> friends = mongoTemplate.find(query, Friend.class);
13. try {
14.             Thread.sleep(10000);
15.         } catch (InterruptedException e) {
16.             e.printStackTrace();
17.         }
18. //3、循环好友数据,构建时间线数据存入数据库
19. for (Friend friend : friends) {
20. MovementTimeLine timeLine = new MovementTimeLine();
21.             timeLine.setMovementId(movementId);
22.             timeLine.setUserId(friend.getUserId());
23.             timeLine.setFriendId(friend.getFriendId());
24.             timeLine.setCreated(System.currentTimeMillis());
25.             mongoTemplate.save(timeLine);
26.         }
27.     }
28. }

3.2.5、整合测试

1. @RunWith(SpringRunner.class)
2. @SpringBootTest(classes = AppServerApplication.class)
3. public class MovementApiTest {
4. 
5. @DubboReference
6. private MovementApi movementApi;
7. 
8. @Test
9. public void testPublish() {
10. Movement movement = new Movement();
11.         movement.setUserId(106l);
12.         movement.setTextContent("你的酒窝没有酒,我却醉的像条狗");
13.         List<String> list = new ArrayList<>();
14.         list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_1.png");
15.         list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_2.png");
16.         movement.setMedias(list);
17.         movement.setLatitude("40.066355");
18.         movement.setLongitude("116.350426");
19.         movement.setLocationName("中国北京市昌平区建材城西路16号");
20.         movementApi.publish(movement);
21.     }
22. }

3.3、查询个人动态

查询好友动态其实就是查询自己的时间线表,好友在发动态时已经将动态信息写入到了自己的时间线表中。

3.3.0、思路分析

3.3.1、vo对象

1. package com.tanhua.model.vo;
2. 
3. import com.tanhua.model.domain.UserInfo;
4. import com.tanhua.model.mongo.Movement;
5. import lombok.AllArgsConstructor;
6. import lombok.Data;
7. import lombok.NoArgsConstructor;
8. import org.springframework.beans.BeanUtils;
9. import org.springframework.util.StringUtils;
10. 
11. import java.io.Serializable;
12. import java.text.SimpleDateFormat;
13. import java.util.Date;
14. 
15. @Data
16. @NoArgsConstructor
17. @AllArgsConstructor
18. public class MovementsVo  implements Serializable {
19. 
20. private String id; //动态id
21. 
22. private Long userId; //用户id
23. private String avatar; //头像
24. private String nickname; //昵称
25. private String gender; //性别 man woman
26. private Integer age; //年龄
27. private String[] tags; //标签
28. 
29. 
30. private String textContent; //文字动态
31. private String[] imageContent; //图片动态
32. private String distance; //距离
33. private String createDate; //发布时间 如: 10分钟前
34. private Integer likeCount; //点赞数
35. private Integer commentCount; //评论数
36. private Integer loveCount; //喜欢数
37. 
38. 
39. private Integer hasLiked; //是否点赞(1是,0否)
40. private Integer hasLoved; //是否喜欢(1是,0否)
41. 
42. 
43. public static MovementsVo init(UserInfo userInfo, Movement item) {
44. MovementsVo vo = new MovementsVo();
45. //设置动态数据
46.         BeanUtils.copyProperties(item, vo);
47.         vo.setId(item.getId().toHexString());
48. //设置用户数据
49.         BeanUtils.copyProperties(userInfo, vo);
50. if(!StringUtils.isEmpty(userInfo.getTags())) {
51.             vo.setTags(userInfo.getTags().split(","));
52.         }
53. //图片列表
54.         vo.setImageContent(item.getMedias().toArray(new String[]{}));
55. //距离
56.         vo.setDistance("500米");
57. Date date = new Date(item.getCreated());
58.         vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date));
59. //设置是否点赞(后续处理)
60.         vo.setHasLoved(0);
61.         vo.setHasLiked(0);
62. return vo;
63.     }
64. }

3.3.2、controller

修改MovementsController完成查询好友动态功能

1. /**
2.  * 查询我的动态
3.  */
4. @GetMapping("/all")
5. public ResponseEntity findByUserId(Long userId,
6. @RequestParam(defaultValue = "1") Integer page,
7. @RequestParam(defaultValue = "10") Integer pagesize) {
8. PageResult pr = movementService.findByUserId(userId,page,pagesize);
9. return ResponseEntity.ok(pr);
10. }

3.3.3、service

修改MovementsService完成查询好友动态功能

1. //查询个人动态
2. public PageResult findByUserId(Long userId, Integer page, Integer pagesize) {
3. //1、根据用户id,调用API查询个人动态内容(PageResult  -- Movement)
4. PageResult pr = movementApi.findByUserId(userId,page,pagesize);
5. //2、获取PageResult中的item列表对象
6.     List<Movement> items = (List<Movement>) pr.getItems();
7. //3、非空判断
8. if(items == null) {
9. return pr;
10.     }
11. //4、循环数据列表
12. UserInfo userInfo = userInfoApi.findById(userId);
13.     List<MovementsVo> vos = new ArrayList<>();
14. for (Movement item : items) {
15. //5、一个Movement构建一个Vo对象
16. MovementsVo vo = MovementsVo.init(userInfo, item);
17.         vos.add(vo);
18.     }
19. //6、构建返回值
20.     pr.setItems(vos);
21. return pr;
22. }

3.3.4、API层

修改PublishApiPublishApiImpl完成查询好友动态功能

1. @Override
2. public PageResult findByUserId(Long userId, Integer page, Integer pagesize) {
3. Criteria criteria = Criteria.where("userId").is(userId);
4. Query query = Query.query(criteria).skip((page -1 ) * pagesize).limit(pagesize)
5.             .with(Sort.by(Sort.Order.desc("created")));
6.     List<Movement> movements = mongoTemplate.find(query, Movement.class);
7. return new PageResult(page,pagesize,0l,movements);
8. }

3.3.5、测试

相关文章
|
7月前
|
存储 NoSQL API
day04—圈子功能实现
day04—圈子功能实现
50 1
|
存储 NoSQL API
【探花交友】day04—圈子功能实现(二)
【探花交友】day04—圈子功能实现(二)
143 0
|
存储 SQL 缓存
【探花交友】day04—圈子功能实现(一)
【探花交友】day04—圈子功能实现
125 0
|
存储 NoSQL 搜索推荐
【探花交友】day05—圈子互动(上)
【探花交友】day05—圈子互动
122 0
|
NoSQL API Redis
【探花交友】day05—圈子互动(下)
【探花交友】day05—圈子互动(下)
141 0
|
Dubbo NoSQL 应用服务中间件
【探花交友】day06—即时通信(四)
【探花交友】day06—即时通信(四)
185 0
|
JSON 测试技术 API
【探花交友】day06—即时通信(二)
【探花交友】day06—即时通信(二)
121 0
|
NoSQL 程序员 API
【探花交友】day06—即时通信(三)
【探花交友】day06—即时通信(三)
124 0
|
消息中间件 NoSQL 前端开发
【探花交友】day06—即时通信(一)
【探花交友】day06—即时通信
183 0
|
Dubbo NoSQL 测试技术
【探花交友】day07—搜附近(一)
【探花交友】day07—搜附近
130 0