一、数据库设计
1、数据库
2、数据表
guli_edu.sql
二、创建课程评论接口
1、在service-edu模块,生成课程评论代码
2、在service-ucenter模块,创建接口
(1)实现用户id获取用户信息,返回用户信息对象
//根据token字符串获取用户信息
@PostMapping("getInfoUc/{id}")
public com.atguigu.commonutils.vo.UcenterMember getInfo(@PathVariable String id) {
//根据用户id获取用户信息
UcenterMember ucenterMember = memberService.getById(id);
com.atguigu.commonutils.vo.UcenterMember memeber = new com.atguigu.commonutils.vo.UcenterMember();
BeanUtils.copyProperties(ucenterMember,memeber);
return memeber;
}
3、创建课程评论controller
(1)在service-edu模块创建client,实现微服务调用
@Component
@FeignClient(name="service-ucenter",fallback = UcenterClientImpl.class)
public interface UcenterClient {
//根据用户id获取用户信息
@GetMapping("/ucenterservice/member/getUcenterPay/{memberId}")
public UcenterMemberPay getUcenterPay(@PathVariable("memberId") String memberId);
}
@Component
public class UcenterClientImpl implements UcenterClient {
@Override
public UcenterMemberPay getUcenterPay(String memberId) {
return null;
}
}
(2)创建评论列表和添加评论接口
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class CommentFrontController {
@Autowired
private CommentService commentService;
@Autowired
private UcenterClient ucenterClient;
//根据课程id查询评论列表
@ApiOperation(value = "评论分页列表")
@GetMapping("{page}/{limit}")
public R index(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit,
@ApiParam(name = "courseQuery", value = "查询对象", required = false)
String courseId) {
Page<Comment> pageParam = new Page<>(page, limit);
QueryWrapper<Comment> wrapper = new QueryWrapper<>();
wrapper.eq("course_id",courseId);
commentService.page(pageParam,wrapper);
List<Comment> commentList = pageParam.getRecords();
Map<String, Object> map = new HashMap<>();
map.put("items", commentList);
map.put("current", pageParam.getCurrent());
map.put("pages", pageParam.getPages());
map.put("size", pageParam.getSize());
map.put("total", pageParam.getTotal());
map.put("hasNext", pageParam.hasNext());
map.put("hasPrevious", pageParam.hasPrevious());
return R.ok().data(map);
}
@ApiOperation(value = "添加评论")
@PostMapping("auth/save")
public R save(@RequestBody Comment comment, HttpServletRequest request) {
String memberId = JwtUtils.getMemberIdByJwtToken(request);
if(StringUtils.isEmpty(memberId)) {
return R.error().code(28004).message("请登录");
}
comment.setMemberId(memberId);
UcenterMemberPay ucenterInfo = ucenterClient.getUcenterPay(memberId);
comment.setNickname(ucenterInfo.getNickname());
comment.setAvatar(ucenterInfo.getAvatar());
commentService.save(comment);
return R.ok();
}
}