图片.png

图片.png

一、课程列表后端实现

1、定义搜索对象

CourseQuery

  1. package com.guli.edu.query;
  2. @ApiModel(value = "Course查询对象", description = "课程查询对象封装")
  3. @Data
  4. public class CourseQuery implements Serializable {
  5. private static final long serialVersionUID = 1L;
  6. @ApiModelProperty(value = "课程名称")
  7. private String title;
  8. @ApiModelProperty(value = "讲师id")
  9. private String teacherId;
  10. @ApiModelProperty(value = "一级类别id")
  11. private String subjectParentId;
  12. @ApiModelProperty(value = "二级类别id")
  13. private String subjectId;
  14. }

2、定义service方法

接口

  1. void pageQuery(Page<Course> pageParam, CourseQuery courseQuery);

实现

  1. @Override
  2. public void pageQuery(Page<Course> pageParam, CourseQuery courseQuery) {
  3. QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.orderByDesc("gmt_create");
  5. if (courseQuery == null){
  6. baseMapper.selectPage(pageParam, queryWrapper);
  7. return;
  8. }
  9. String title = courseQuery.getTitle();
  10. String teacherId = courseQuery.getTeacherId();
  11. String subjectParentId = courseQuery.getSubjectParentId();
  12. String subjectId = courseQuery.getSubjectId();
  13. if (!StringUtils.isEmpty(title)) {
  14. queryWrapper.like("title", title);
  15. }
  16. if (!StringUtils.isEmpty(teacherId) ) {
  17. queryWrapper.eq("teacher_id", teacherId);
  18. }
  19. if (!StringUtils.isEmpty(subjectParentId)) {
  20. queryWrapper.ge("subject_parent_id", subjectParentId);
  21. }
  22. if (!StringUtils.isEmpty(subjectId)) {
  23. queryWrapper.ge("subject_id", subjectId);
  24. }
  25. baseMapper.selectPage(pageParam, queryWrapper);
  26. }

3、定义web层方法

  1. @ApiOperation(value = "分页课程列表")
  2. @GetMapping("{page}/{limit}")
  3. public R pageQuery(
  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. CourseQuery courseQuery){
  10. Page<Course> pageParam = new Page<>(page, limit);
  11. courseService.pageQuery(pageParam, courseQuery);
  12. List<Course> records = pageParam.getRecords();
  13. long total = pageParam.getTotal();
  14. return R.ok().data("total", total).data("rows", records);
  15. }

二、课程列表前端实现

1、定义api

course.js

  1. getPageList(page, limit, searchObj) {
  2. return request({
  3. url: `${api_name}/${page}/${limit}`,
  4. method: 'get',
  5. params: searchObj
  6. })
  7. },

三、课程删除后端实现

图片.png

1、web层

定义删除api方法:CourseAdminController.java

  1. @ApiOperation(value = "根据ID删除课程")
  2. @DeleteMapping("{id}")
  3. public R removeById(
  4. @ApiParam(name = "id", value = "课程ID", required = true)
  5. @PathVariable String id){
  6. boolean result = courseService.removeCourseById(id);
  7. if(result){
  8. return R.ok();
  9. }else{
  10. return R.error().message("删除失败");
  11. }
  12. }

2、service层

如果用户确定删除,则首先删除video记录,然后删除chapter记录,最后删除Course记录

2.1、在VideoService中定义根据courseId删除video业务方法

接口

  1. boolean removeByCourseId(String courseId);

实现

  1. @Override
  2. public boolean removeByCourseId(String courseId) {
  3. QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.eq("course_id", courseId);
  5. Integer count = baseMapper.delete(queryWrapper);
  6. return null != count && count > 0;
  7. }

2.2、在ChapterService中定义根据courseId删除chapter业务方法

接口

  1. boolean removeByCourseId(String courseId);

实现

  1. @Override
  2. public boolean removeByCourseId(String courseId) {
  3. QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
  4. queryWrapper.eq("course_id", courseId);
  5. Integer count = baseMapper.delete(queryWrapper);
  6. return null != count && count > 0;
  7. }

2.3、删除当前course记录

接口:CourseService.java

  1. boolean removeCourseById(String id);

实现:CourseServiceImpl.java

  1. @Override
  2. public boolean removeCourseById(String id) {
  3. //根据id删除所有视频
  4. videoService.removeByCourseId(id);
  5. //根据id删除所有章节
  6. chapterService.removeByCourseId(id);
  7. Integer result = baseMapper.deleteById(id);
  8. return null != result && result > 0;
  9. }

二、课程删除前端实现

1、定义api

course.js中添加删除方法

  1. removeById(id) {
  2. return request({
  3. url: `${api_name}/${id}`,
  4. method: 'delete'
  5. })
  6. }