一、课程列表后端实现
1、定义搜索对象
CourseQuery
package com.guli.edu.query;
@ApiModel(value = "Course查询对象", description = "课程查询对象封装")
@Data
public class CourseQuery 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;
}
2、定义service方法
接口
void pageQuery(Page<Course> pageParam, CourseQuery courseQuery);
实现
@Override
public void pageQuery(Page<Course> pageParam, CourseQuery courseQuery) {
QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("gmt_create");
if (courseQuery == null){
baseMapper.selectPage(pageParam, queryWrapper);
return;
}
String title = courseQuery.getTitle();
String teacherId = courseQuery.getTeacherId();
String subjectParentId = courseQuery.getSubjectParentId();
String subjectId = courseQuery.getSubjectId();
if (!StringUtils.isEmpty(title)) {
queryWrapper.like("title", title);
}
if (!StringUtils.isEmpty(teacherId) ) {
queryWrapper.eq("teacher_id", teacherId);
}
if (!StringUtils.isEmpty(subjectParentId)) {
queryWrapper.ge("subject_parent_id", subjectParentId);
}
if (!StringUtils.isEmpty(subjectId)) {
queryWrapper.ge("subject_id", subjectId);
}
baseMapper.selectPage(pageParam, queryWrapper);
}
3、定义web层方法
@ApiOperation(value = "分页课程列表")
@GetMapping("{page}/{limit}")
public R pageQuery(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit,
@ApiParam(name = "courseQuery", value = "查询对象", required = false)
CourseQuery courseQuery){
Page<Course> pageParam = new Page<>(page, limit);
courseService.pageQuery(pageParam, courseQuery);
List<Course> records = pageParam.getRecords();
long total = pageParam.getTotal();
return R.ok().data("total", total).data("rows", records);
}
二、课程列表前端实现
1、定义api
course.js
getPageList(page, limit, searchObj) {
return request({
url: `${api_name}/${page}/${limit}`,
method: 'get',
params: searchObj
})
},
三、课程删除后端实现
1、web层
定义删除api方法:CourseAdminController.java
@ApiOperation(value = "根据ID删除课程")
@DeleteMapping("{id}")
public R removeById(
@ApiParam(name = "id", value = "课程ID", required = true)
@PathVariable String id){
boolean result = courseService.removeCourseById(id);
if(result){
return R.ok();
}else{
return R.error().message("删除失败");
}
}
2、service层
如果用户确定删除,则首先删除video记录,然后删除chapter记录,最后删除Course记录
2.1、在VideoService中定义根据courseId删除video业务方法
接口
boolean removeByCourseId(String courseId);
实现
@Override
public boolean removeByCourseId(String courseId) {
QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("course_id", courseId);
Integer count = baseMapper.delete(queryWrapper);
return null != count && count > 0;
}
2.2、在ChapterService中定义根据courseId删除chapter业务方法
接口
boolean removeByCourseId(String courseId);
实现
@Override
public boolean removeByCourseId(String courseId) {
QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("course_id", courseId);
Integer count = baseMapper.delete(queryWrapper);
return null != count && count > 0;
}
2.3、删除当前course记录
接口:CourseService.java
boolean removeCourseById(String id);
实现:CourseServiceImpl.java
@Override
public boolean removeCourseById(String id) {
//根据id删除所有视频
videoService.removeByCourseId(id);
//根据id删除所有章节
chapterService.removeByCourseId(id);
Integer result = baseMapper.deleteById(id);
return null != result && result > 0;
}
二、课程删除前端实现
1、定义api
course.js中添加删除方法
removeById(id) {
return request({
url: `${api_name}/${id}`,
method: 'delete'
})
}