1.2 陌生人问题
对数据库表进行操作:如果存在数据,更新数据库。如果不存在数据,保存数据库表数据
1.2.1 接口文档
1.2.2 代码实现
SettingsController
1. /** 2. * 设置陌生人问题 3. */ 4. @PostMapping("/questions") 5. public ResponseEntity questions(@RequestBody Map map) { 6. //获取参数 7. String content = (String) map.get("content"); 8. settingsService.saveQuestion(content); 9. return ResponseEntity.ok(null); 10. }
SettingsService
1. //设置陌生人问题 2. public void saveQuestion(String content) { 3. //1、获取当前用户id 4. Long userId = UserHolder.getUserId(); 5. //2、调用api查询当前用户的陌生人问题 6. Question question = questionApi.findByUserId(userId); 7. //3、判断问题是否存在 8. if(question == null) { 9. //3.1 如果不存在,保存 10. question = new Question(); 11. question.setUserId(userId); 12. question.setTxt(content); 13. questionApi.save(question); 14. }else { 15. //3.2 如果存在,更新 16. question.setTxt(content); 17. questionApi.update(question); 18. } 19. }
QuestionApi
tanhua-dubbo
工程中的QuestionApi
和QuestionApiImpl
中添加保存和更新方法
1. @Override 2. public void save(Question question) { 3. questionMapper.insert(question); 4. } 5. 6. @Override 7. public void update(Question question) { 8. questionMapper.updateById(question); 9. }
1.3 通知设置
1.3.1 接口文档
通知管理:对通知进行保存或者更新的操作
http://192.168.136.160:3000/project/19/interface/api/280
1.3.2 代码实现
SettingsController
1. /** 2. * 通知设置 3. */ 4. @PostMapping("/notifications/setting") 5. public ResponseEntity notifications(@RequestBody Map map) { 6. //获取参数 7. settingsService.saveSettings(map); 8. return ResponseEntity.ok(null); 9. }
SettingsService
1. //通知设置 2. public void saveSettings(Map map) { 3. boolean likeNotification = (Boolean) map.get("likeNotification"); 4. boolean pinglunNotification = (Boolean) map.get("pinglunNotification"); 5. boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification"); 6. //1、获取当前用户id 7. Long userId = UserHolder.getUserId(); 8. //2、根据用户id,查询用户的通知设置 9. Settings settings = settingsApi.findByUserId(userId); 10. //3、判断 11. if(settings == null) { 12. //保存 13. settings = new Settings(); 14. settings.setUserId(userId); 15. settings.setPinglunNotification(pinglunNotification); 16. settings.setLikeNotification(likeNotification); 17. settings.setGonggaoNotification(gonggaoNotification); 18. settingsApi.save(settings); 19. }else { 20. settings.setPinglunNotification(pinglunNotification); 21. settings.setLikeNotification(likeNotification); 22. settings.setGonggaoNotification(gonggaoNotification); 23. settingsApi.update(settings); 24. } 25. }
SettingsApi
tanhua-dubbo
工程中的SettingsApi
和SettingsApiImpl
中添加保存和更新方法
1. @Override 2. public void save(Settings settings) { 3. settingsMapper.insert(settings); 4. } 5. 6. @Override 7. public void update(Settings settings) { 8. settingsMapper.updateById(settings); 9. }
1.4 黑名单管理
1.3.1 接口文档
- 查询黑名单列表
- 移除黑名单
1.3.2 分页查询
vo对象
tanhua-domain
工程的配置分页vo对象
1. package com.tanhua.domain.vo; 2. 3. import lombok.AllArgsConstructor; 4. import lombok.Data; 5. import lombok.NoArgsConstructor; 6. 7. import java.io.Serializable; 8. import java.util.Collections; 9. import java.util.List; 10. 11. @Data 12. @AllArgsConstructor 13. @NoArgsConstructor 14. public class PageResult implements Serializable { 15. 16. private Integer counts = 0;//总记录数 17. private Integer pagesize;//页大小 18. private Integer pages = 0;//总页数 19. private Integer page;//当前页码 20. private List<?> items = Collections.emptyList(); //列表 21. 22. public PageResult(Integer page,Integer pagesize, 23. int counts,List list) { 24. this.page = page; 25. this.pagesize = pagesize; 26. this.items = list; 27. this.counts = counts; 28. this.pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize + 1; 29. } 30. 31. }
SettingsController
1. /** 2. * 分页查询黑名单列表 3. */ 4. @GetMapping("/blacklist") 5. public ResponseEntity blacklist( 6. @RequestParam(defaultValue = "1") int page, 7. @RequestParam(defaultValue = "10") int size) { 8. //1、调用service查询 9. PageResult pr = settingsService.blacklist(page,size); 10. //2、构造返回 11. return ResponseEntity.ok(pr); 12. } 13. 14. /** 15. * 取消黑名单 16. */ 17. @DeleteMapping("/blacklist/{uid}") 18. public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) { 19. settingsService.deleteBlackList(blackUserId); 20. return ResponseEntity.ok(null); 21. }
SettingService
1. //分页查询黑名单列表 2. public PageResult blacklist(int page, int size) { 3. //1、获取当前用户的id 4. Long userId = UserHolder.getUserId(); 5. //2、调用API查询用户的黑名单分页列表 Ipage对象 6. IPage<UserInfo> iPage = blackListApi.findByUserId(userId,page,size); 7. //3、对象转化,将查询的Ipage对象的内容封装到PageResult中 8. PageResult pr = new PageResult(page,size,iPage.getTotal(),iPage.getRecords()); 9. //4、返回 10. return pr; 11. } 12. 13. //取消黑名单 14. public void deleteBlackList(Long blackUserId) { 15. //1、获取当前用户id 16. Long userId = UserHolder.getUserId(); 17. //2、调用api删除 18. blackListApi.delete(userId,blackUserId); 19. }
BlackListApi
1. @Override 2. public IPage<UserInfo> findByUserId(Long userId, int page, int size) { 3. //1、构建分页参数对象Page 4. Page pages = new Page(page,size); 5. //2、调用方法分页(自定义编写 分页参数Page,sql条件参数) 6. return userInfoMapper.findBlackList(pages,userId); 7. } 8. 9. @Override 10. public void delete(Long userId, Long blackUserId) { 11. QueryWrapper<BlackList> qw = new QueryWrapper<>(); 12. qw.eq("user_id",userId); 13. qw.eq("black_user_id",blackUserId); 14. blackListMapper.delete(qw); 15. }
UserInfoMapper
1. public interface UserInfoMapper extends BaseMapper<UserInfo> { 2. 3. @Select("select * from tb_user_info where id in (\n" + 4. " SELECT black_user_id FROM tb_black_list where user_id=#{userId}\n" + 5. ")") 6. IPage<UserInfo> findBlackList(@Param("pages") Page pages, @Param("userId") Long userId); 7. }
MybatisPlusConfig
tanhua-dubbo-db
引导类开启mybatis-plus分页插件支持
1. public interface UserInfoMapper extends BaseMapper<UserInfo> { 2. 3. @Select("select * from tb_user_info where id in (\n" + 4. " SELECT black_user_id FROM tb_black_list where user_id=#{userId}\n" + 5. ")") 6. IPage<UserInfo> findBlackList(@Param("pages") Page pages, @Param("userId") Long userId); 7. }
使用mybatis-plus的分页:
- 创建分页对象:Page,指定当前页和每页查询条数
- 基础查询:mapper.selectPage(page,查询条件)
- 自定义查询:Ipage 方法名称(Page对象,xxx查询条件)