一、数据库设计

1、数据库

edu_comment

2、数据表

  1. guli_edu.sql

二、创建课程评论接口

1、在service-edu模块,生成课程评论代码

(1)使用mp代码生成器生成

2、在service-ucenter模块,创建接口

(1)实现用户id获取用户信息,返回用户信息对象

  1. //根据token字符串获取用户信息
  2. @PostMapping("getInfoUc/{id}")
  3. public com.atguigu.commonutils.vo.UcenterMember getInfo(@PathVariable String id) {
  4. //根据用户id获取用户信息
  5. UcenterMember ucenterMember = memberService.getById(id);
  6. com.atguigu.commonutils.vo.UcenterMember memeber = new com.atguigu.commonutils.vo.UcenterMember();
  7. BeanUtils.copyProperties(ucenterMember,memeber);
  8. return memeber;
  9. }

3、创建课程评论controller

(1)在service-edu模块创建client,实现微服务调用

  1. @Component
  2. @FeignClient(name="service-ucenter",fallback = UcenterClientImpl.class)
  3. public interface UcenterClient {
  4. //根据用户id获取用户信息
  5. @GetMapping("/ucenterservice/member/getUcenterPay/{memberId}")
  6. public UcenterMemberPay getUcenterPay(@PathVariable("memberId") String memberId);
  7. }
  8. @Component
  9. public class UcenterClientImpl implements UcenterClient {
  10. @Override
  11. public UcenterMemberPay getUcenterPay(String memberId) {
  12. return null;
  13. }
  14. }

(2)创建评论列表和添加评论接口

  1. @RestController
  2. @RequestMapping("/eduservice/comment")
  3. @CrossOrigin
  4. public class CommentFrontController {
  5. @Autowired
  6. private CommentService commentService;
  7. @Autowired
  8. private UcenterClient ucenterClient;
  9. //根据课程id查询评论列表
  10. @ApiOperation(value = "评论分页列表")
  11. @GetMapping("{page}/{limit}")
  12. public R index(
  13. @ApiParam(name = "page", value = "当前页码", required = true)
  14. @PathVariable Long page,
  15. @ApiParam(name = "limit", value = "每页记录数", required = true)
  16. @PathVariable Long limit,
  17. @ApiParam(name = "courseQuery", value = "查询对象", required = false)
  18. String courseId) {
  19. Page<Comment> pageParam = new Page<>(page, limit);
  20. QueryWrapper<Comment> wrapper = new QueryWrapper<>();
  21. wrapper.eq("course_id",courseId);
  22. commentService.page(pageParam,wrapper);
  23. List<Comment> commentList = pageParam.getRecords();
  24. Map<String, Object> map = new HashMap<>();
  25. map.put("items", commentList);
  26. map.put("current", pageParam.getCurrent());
  27. map.put("pages", pageParam.getPages());
  28. map.put("size", pageParam.getSize());
  29. map.put("total", pageParam.getTotal());
  30. map.put("hasNext", pageParam.hasNext());
  31. map.put("hasPrevious", pageParam.hasPrevious());
  32. return R.ok().data(map);
  33. }
  34. @ApiOperation(value = "添加评论")
  35. @PostMapping("auth/save")
  36. public R save(@RequestBody Comment comment, HttpServletRequest request) {
  37. String memberId = JwtUtils.getMemberIdByJwtToken(request);
  38. if(StringUtils.isEmpty(memberId)) {
  39. return R.error().code(28004).message("请登录");
  40. }
  41. comment.setMemberId(memberId);
  42. UcenterMemberPay ucenterInfo = ucenterClient.getUcenterPay(memberId);
  43. comment.setNickname(ucenterInfo.getNickname());
  44. comment.setAvatar(ucenterInfo.getAvatar());
  45. commentService.save(comment);
  46. return R.ok();
  47. }
  48. }