04 课程列表功能.png

一、课程列表后端接口

1、课程列表

(1)课程列表vo类

  1. @ApiModel(value = "课程查询对象", description = "课程查询对象封装")
  2. @Data
  3. public class CourseQueryVo implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. @ApiModelProperty(value = "课程名称")
  6. private String title;
  7. @ApiModelProperty(value = "讲师id")
  8. private String teacherId;
  9. @ApiModelProperty(value = "一级类别id")
  10. private String subjectParentId;
  11. @ApiModelProperty(value = "二级类别id")
  12. private String subjectId;
  13. @ApiModelProperty(value = "销量排序")
  14. private String buyCountSort;
  15. @ApiModelProperty(value = "最新时间排序")
  16. private String gmtCreateSort;
  17. @ApiModelProperty(value = "价格排序")
  18. private String priceSort;
  19. }

(2)课程列表controller

  1. @ApiOperation(value = "分页课程列表")
  2. @PostMapping(value = "{page}/{limit}")
  3. public R pageList(
  4. @ApiParam(name = "page", value = "当前页码", required = true)
  5. @PathVariable Long page,
  6. @ApiParam(name = "limit", value = "每页记录数", required = true)
  7. @PathVariable Long limit,
  8. @ApiParam(name = "courseQuery", value = "查询对象", required = false)
  9. @RequestBody(required = false) CourseQueryVo courseQuery){
  10. Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
  11. Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
  12. return R.ok().data(map);
  13. }

(3)课程列表service

  1. @Override
  2. public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
  3. QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
  4. if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
  5. queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
  6. }
  7. if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
  8. queryWrapper.eq("subject_id", courseQuery.getSubjectId());
  9. }
  10. if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
  11. queryWrapper.orderByDesc("buy_count");
  12. }
  13. if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
  14. queryWrapper.orderByDesc("gmt_create");
  15. }
  16. if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
  17. queryWrapper.orderByDesc("price");
  18. }
  19. baseMapper.selectPage(pageParam, queryWrapper);
  20. List<EduCourse> records = pageParam.getRecords();
  21. long current = pageParam.getCurrent();
  22. long pages = pageParam.getPages();
  23. long size = pageParam.getSize();
  24. long total = pageParam.getTotal();
  25. boolean hasNext = pageParam.hasNext();
  26. boolean hasPrevious = pageParam.hasPrevious();
  27. Map<String, Object> map = new HashMap<String, Object>();
  28. map.put("items", records);
  29. map.put("current", current);
  30. map.put("pages", pages);
  31. map.put("size", size);
  32. map.put("total", total);
  33. map.put("hasNext", hasNext);
  34. map.put("hasPrevious", hasPrevious);
  35. return map;
  36. }

二、课程详情页

image.png

1、vo对象的定义

在项目中很多时候需要把model转换成dto用于网站信息的展示,按前端的需要传递对象的数据,保证model对外是隐私的,例如密码之类的属性能很好地避免暴露在外,同时也会减小数据传输的体积。
CourseWebVo.java

  1. package com.guli.edu.vo;
  2. @ApiModel(value="课程信息", description="网站课程详情页需要的相关字段")
  3. @Data
  4. public class CourseWebVo implements Serializable {
  5. private static final long serialVersionUID = 1L;
  6. private String id;
  7. @ApiModelProperty(value = "课程标题")
  8. private String title;
  9. @ApiModelProperty(value = "课程销售价格,设置为0则可免费观看")
  10. private BigDecimal price;
  11. @ApiModelProperty(value = "总课时")
  12. private Integer lessonNum;
  13. @ApiModelProperty(value = "课程封面图片路径")
  14. private String cover;
  15. @ApiModelProperty(value = "销售数量")
  16. private Long buyCount;
  17. @ApiModelProperty(value = "浏览数量")
  18. private Long viewCount;
  19. @ApiModelProperty(value = "课程简介")
  20. private String description;
  21. @ApiModelProperty(value = "讲师ID")
  22. private String teacherId;
  23. @ApiModelProperty(value = "讲师姓名")
  24. private String teacherName;
  25. @ApiModelProperty(value = "讲师资历,一句话说明讲师")
  26. private String intro;
  27. @ApiModelProperty(value = "讲师头像")
  28. private String avatar;
  29. @ApiModelProperty(value = "课程类别ID")
  30. private String subjectLevelOneId;
  31. @ApiModelProperty(value = "类别名称")
  32. private String subjectLevelOne;
  33. @ApiModelProperty(value = "课程类别ID")
  34. private String subjectLevelTwoId;
  35. @ApiModelProperty(value = "类别名称")
  36. private String subjectLevelTwo;
  37. }

2、课程和讲师信息的获取

1)Mapper中关联查询课程和讲师信息

CourseMapper.java

  1. CourseWebVo selectInfoWebById(String courseId);

CourseMapper.xml

  1. <select id="selectInfoWebById" resultType="com.guli.edu.vo.CourseWebVo">
  2. SELECT
  3. c.id,
  4. c.title,
  5. c.cover,
  6. CONVERT(c.price, DECIMAL(8,2)) AS price,
  7. c.lesson_num AS lessonNum,
  8. c.cover,
  9. c.buy_count AS buyCount,
  10. c.view_count AS viewCount,
  11. cd.description,
  12. t.id AS teacherId,
  13. t.name AS teacherName,
  14. t.intro,
  15. t.avatar,
  16. s1.id AS subjectLevelOneId,
  17. s1.title AS subjectLevelOne,
  18. s2.id AS subjectLevelTwoId,
  19. s2.title AS subjectLevelTwo
  20. FROM
  21. edu_course c
  22. LEFT JOIN edu_course_description cd ON c.id = cd.id
  23. LEFT JOIN edu_teacher t ON c.teacher_id = t.id
  24. LEFT JOIN edu_subject s1 ON c.subject_parent_id = s1.id
  25. LEFT JOIN edu_subject s2 ON c.subject_id = s2.id
  26. WHERE
  27. c.id = #{id}
  28. </select>

2)业务层获取数据并更新浏览量

CourseService
接口

  1. /**
  2. * 获取课程信息
  3. * @param id
  4. * @return
  5. */
  6. CourseWebVo selectInfoWebById(String id);
  7. /**
  8. * 更新课程浏览数
  9. * @param id
  10. */
  11. void updatePageViewCount(String id);

实现

  1. @Override
  2. public CourseWebVo selectInfoWebById(String id) {
  3. this.updatePageViewCount(id);
  4. return baseMapper.selectInfoWebById(id);
  5. }
  6. @Override
  7. public void updatePageViewCount(String id) {
  8. Course course = baseMapper.selectById(id);
  9. course.setViewCount(course.getViewCount() + 1);
  10. baseMapper.updateById(course);
  11. }

3)接口层

CourseController

  1. @Autowired
  2. private ChapterService chapterService;
  3. @ApiOperation(value = "根据ID查询课程")
  4. @GetMapping(value = "{courseId}")
  5. public R getById(
  6. @ApiParam(name = "courseId", value = "课程ID", required = true)
  7. @PathVariable String courseId){
  8. //查询课程信息和讲师信息
  9. CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
  10. //查询当前课程的章节信息
  11. List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
  12. return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList);
  13. }