一、课程列表后端接口
1、课程列表
(1)课程列表vo类
@ApiModel(value = "课程查询对象", description = "课程查询对象封装")
@Data
public class CourseQueryVo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "课程名称")
private String title;
@ApiModelProperty(value = "讲师id")
private String teacherId;
@ApiModelProperty(value = "一级类别id")
private String subjectParentId;
@ApiModelProperty(value = "二级类别id")
private String subjectId;
@ApiModelProperty(value = "销量排序")
private String buyCountSort;
@ApiModelProperty(value = "最新时间排序")
private String gmtCreateSort;
@ApiModelProperty(value = "价格排序")
private String priceSort;
}
(2)课程列表controller
@ApiOperation(value = "分页课程列表")
@PostMapping(value = "{page}/{limit}")
public R pageList(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit,
@ApiParam(name = "courseQuery", value = "查询对象", required = false)
@RequestBody(required = false) CourseQueryVo courseQuery){
Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
return R.ok().data(map);
}
(3)课程列表service
@Override
public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
}
if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
queryWrapper.eq("subject_id", courseQuery.getSubjectId());
}
if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
queryWrapper.orderByDesc("buy_count");
}
if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
queryWrapper.orderByDesc("gmt_create");
}
if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
queryWrapper.orderByDesc("price");
}
baseMapper.selectPage(pageParam, queryWrapper);
List<EduCourse> records = pageParam.getRecords();
long current = pageParam.getCurrent();
long pages = pageParam.getPages();
long size = pageParam.getSize();
long total = pageParam.getTotal();
boolean hasNext = pageParam.hasNext();
boolean hasPrevious = pageParam.hasPrevious();
Map<String, Object> map = new HashMap<String, Object>();
map.put("items", records);
map.put("current", current);
map.put("pages", pages);
map.put("size", size);
map.put("total", total);
map.put("hasNext", hasNext);
map.put("hasPrevious", hasPrevious);
return map;
}
二、课程详情页
1、vo对象的定义
在项目中很多时候需要把model转换成dto用于网站信息的展示,按前端的需要传递对象的数据,保证model对外是隐私的,例如密码之类的属性能很好地避免暴露在外,同时也会减小数据传输的体积。
CourseWebVo.java
package com.guli.edu.vo;
@ApiModel(value="课程信息", description="网站课程详情页需要的相关字段")
@Data
public class CourseWebVo implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
@ApiModelProperty(value = "课程标题")
private String title;
@ApiModelProperty(value = "课程销售价格,设置为0则可免费观看")
private BigDecimal price;
@ApiModelProperty(value = "总课时")
private Integer lessonNum;
@ApiModelProperty(value = "课程封面图片路径")
private String cover;
@ApiModelProperty(value = "销售数量")
private Long buyCount;
@ApiModelProperty(value = "浏览数量")
private Long viewCount;
@ApiModelProperty(value = "课程简介")
private String description;
@ApiModelProperty(value = "讲师ID")
private String teacherId;
@ApiModelProperty(value = "讲师姓名")
private String teacherName;
@ApiModelProperty(value = "讲师资历,一句话说明讲师")
private String intro;
@ApiModelProperty(value = "讲师头像")
private String avatar;
@ApiModelProperty(value = "课程类别ID")
private String subjectLevelOneId;
@ApiModelProperty(value = "类别名称")
private String subjectLevelOne;
@ApiModelProperty(value = "课程类别ID")
private String subjectLevelTwoId;
@ApiModelProperty(value = "类别名称")
private String subjectLevelTwo;
}
2、课程和讲师信息的获取
1)Mapper中关联查询课程和讲师信息
CourseMapper.java
CourseWebVo selectInfoWebById(String courseId);
CourseMapper.xml
<select id="selectInfoWebById" resultType="com.guli.edu.vo.CourseWebVo">
SELECT
c.id,
c.title,
c.cover,
CONVERT(c.price, DECIMAL(8,2)) AS price,
c.lesson_num AS lessonNum,
c.cover,
c.buy_count AS buyCount,
c.view_count AS viewCount,
cd.description,
t.id AS teacherId,
t.name AS teacherName,
t.intro,
t.avatar,
s1.id AS subjectLevelOneId,
s1.title AS subjectLevelOne,
s2.id AS subjectLevelTwoId,
s2.title AS subjectLevelTwo
FROM
edu_course c
LEFT JOIN edu_course_description cd ON c.id = cd.id
LEFT JOIN edu_teacher t ON c.teacher_id = t.id
LEFT JOIN edu_subject s1 ON c.subject_parent_id = s1.id
LEFT JOIN edu_subject s2 ON c.subject_id = s2.id
WHERE
c.id = #{id}
</select>
2)业务层获取数据并更新浏览量
CourseService
接口
/**
* 获取课程信息
* @param id
* @return
*/
CourseWebVo selectInfoWebById(String id);
/**
* 更新课程浏览数
* @param id
*/
void updatePageViewCount(String id);
实现
@Override
public CourseWebVo selectInfoWebById(String id) {
this.updatePageViewCount(id);
return baseMapper.selectInfoWebById(id);
}
@Override
public void updatePageViewCount(String id) {
Course course = baseMapper.selectById(id);
course.setViewCount(course.getViewCount() + 1);
baseMapper.updateById(course);
}
3)接口层
CourseController
@Autowired
private ChapterService chapterService;
@ApiOperation(value = "根据ID查询课程")
@GetMapping(value = "{courseId}")
public R getById(
@ApiParam(name = "courseId", value = "课程ID", required = true)
@PathVariable String courseId){
//查询课程信息和讲师信息
CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
//查询当前课程的章节信息
List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList);
}