栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022年最新《谷粒学院开发教程》:5 - 章节管理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022年最新《谷粒学院开发教程》:5 - 章节管理

  • 更好的阅读体验
  • 资料地址
  • 1 - 构建工程篇
  • 2 - 前后端交互篇
  • 3 - 文件上传
  • 4 - 课程管理

目录
  • 一、课程章节增删改查
    • 1.1、后端接口
    • 1.2、前端实现
    • 1.3、添加章节
    • 1.4、修改章节
    • 1.5、删除章节
  • 二、课程章节小节功能
    • 2.1、后端接口
    • 2.2、前端实现
    • 2.3、添加小节
    • 2.4、删除小节
    • 2.5、修改小节
  • 三、课程发布信息预览
    • 3.1、后端接口
    • 3.3、前端实现
  • 四、课程发布
    • 4.1、后端接口
    • 4.2、前端页面
  • 五、课程列表
    • 5.1、后端接口
    • 5.2、前端实现
  • 六、课程删除
    • 6.1、后端接口
    • 6.2、前端实现
  • 七、编辑基本信息及大纲


一、课程章节增删改查 1.1、后端接口
@Api(tags = "章节模块")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    EduCourseService eduCourseService;

    // 添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    // 根据章节id查询
    @GetMapping("getChapter/{chapterId}")
    public R getChapter(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }

    // 修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    // 删除章节 若存在小节则不可删除
    @DeleteMapping("deleteById/{chapterId}")
    public R deleteById(@PathVariable String chapterId) {
        boolean flag = eduChapterService.deleteChapter(chapterId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}
@Override
public boolean deleteChapter(String chapterId) {
    QueryWrapper wrapper = new QueryWrapper<>();
    wrapper.eq("chapter_id", chapterId);
    int count = eduVideoService.count(wrapper);
    
    if (count > 0) {
        throw new LaptoyException(20001, "还有小节数据,不能删除");
    } else {
        return this.removeById(chapterId);
    }
}

1.2、前端实现

1、页面代码


data() {
  return {
    dialogChapterFormVisible: false,
    chapter: {
      title: "",
      sort: 0
    },
  };
},

稍微优化了页面


2、创建api - chapter.js

//添加章节
addChapter(chapter) {
    return request({
        url: `/eduservice/chapter/addChapter`,
        method: `post`,
        data: chapter
    })
},
//根据id查询章节
updateChapterById(chapterId) {
    return request({
        url: `/eduservice/chapter/getChapter/${chapterId}`,
        method: `get`,
    })
},
//修改章节
updateChapter(chapter) {
    return request({
        url: `/eduservice/chapter/updateChapter`,
        method: `post`,
        data: chapter
    })
},
//删除章节
deleteById(chapterId) {
    return request({
        url: `/eduservice/chapter/deleteById/${chapterId}`,
        method: `delete`,
    })
}

1.3、添加章节
saveChapter() {
  //设置课程id到chapter对象中,不指定课程id无法正常保存,因为数据库字段为非空
  this.chapter.courseId = this.courseId
  chapter.addChapter(this.chapter).then((resp) => {
    this.dialogChapterFormVisible = false;
    this.$message({
      message: "添加章节成功",
      type: "success",
    });
    this.chapter = {}
    this.getChapterVideoByCourseId()
  });
},
saveOrUpdate() {
  this.saveChapter()
},

1.4、修改章节

1、点击修改章节按钮根据章节id回显数据


2、修改方法

// 修改章节弹窗回显数据
openEditChapter(id) {
  this.dialogChapterFormVisible = true;
  chapter.updateChapterById(id).then((resp) => {
    this.chapter = resp.data.data
  })
},
// 修改章节
updateChapter(id) {
  //设置课程id到chapter对象中,不指定课程id无法正常保存,因为数据库字段为非空
  this.chapter.courseId = this.courseId
  chapter.updateChapter(this.chapter).then((resp) => {
    this.dialogChapterFormVisible = false;
    this.$message({
      message: "修改章节成功",
      type: "success",
    });
    this.getChapterVideoByCourseId();
  });
},

1.5、删除章节

// 删除章节
removeById(chapterId) {
  this.$confirm("此操作将永久删除章节信息, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    chapter.deleteById(chapterId).then((resp) => {
      this.$message({
        type: "success",
        message: "删除成功!",
      });
      this.getChapterVideoByCourseId();
    });
  });
},

二、课程章节小节功能 2.1、后端接口
@Api(tags = "小节模块")
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin //解决跨域问题
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    // TODO 后面这个方法需要完善,删除小节的时候,同时也要把视频删除
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id) {
        eduVideoService.removeById(id);
        return R.ok();
    }

    //修改小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    //根据小节id查询
    @GetMapping("/getVideoById/{videoId}")
    public R getVideoById(@PathVariable String videoId) {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return R.ok().data("data", eduVideo);
    }
}

2.2、前端实现

1、页面



  
  
    添加小节
  




  
    
      
    
    
      
    
    
      
        免费
        默认
      
    
    
      
    
  
  

data() {
  return {
    saveVideoBtnDisabled: false,
    dialogVideoFormVisible: false,
    video: {
      sort: 0,
      title: "",
      free: ""
    }
  };
},

2、API - srcapiteachervideo.js

import request from '@/utils/request' //引入已经封装好的axios 和 拦截器

export default {
    //添加小节
    addVideo(video) {
        return request({
            url: `/eduservice/video/addVideo`,
            method: `post`,
            data: video
        })
    },
    //根据id查询小节
    getVideoById(videoId) {
        return request({
            url: `/eduservice/video/getVideoById/${videoId}`,
            method: `get`,
        })
    },
    //修改小节
    updateVideo(video) {
        return request({
            url: `/eduservice/video/updateVideo`,
            method: `post`,
            data: video
        })
    },
    //删除小节
    deleteById(videoId) {
        return request({
            url: `/eduservice/video/deleteVideo/${videoId}`,
            method: `delete`,
        })
    },
}

2.3、添加小节
openSaveVideoForm(id) {
  this.dialogVideoFormVisible = true
  this.video.courseId = this.courseId
  this.video.chapterId = id
},
saveOrUpdateVideo(){
  if (this.video.id) {
    //修改小节
  } else {
    //新增小节
    this.saveVideo();
  }
},
// 添加小节
saveVideo() {
  video.addVideo(this.video).then((resp) => {
    this.dialogVideoFormVisible = false;
    this.$message({
      message: "添加小节成功",
      type: "success",
    });
    this.video = {}
    this.getChapterVideoByCourseId();
  });
},

2.4、删除小节


  
  
  
  
    
{{ video.title }} 编辑 删除
// 删除小节
removeVideo(id) {
  this.$confirm("此操作将永久删除小节信息, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    video.deleteById(id).then((resp) => {
      this.$message({
        type: "success",
        message: "删除成功!",
      });
      this.getChapterVideoByCourseId();
    });
  });
},

2.5、修改小节


  

  
  
    
{{ video.title }} 编辑 删除
// 展开编辑表单
openEditVideoForm(id) {
  this.dialogVideoFormVisible = true;
  video.getVideoById(id).then((resp) => {
    this.video = resp.data.data;
  });
},

// 修改小节
updateVideo() {
  video.updateVideo(this.video).then((resp) => {
    this.dialogVideoFormVisible = false;
    this.$message({
      message: "修改小节成功",
      type: "success",
    });
    this.getChapterVideoByCourseId();
  });
},

// 添加或修改小节
saveOrUpdateVideo() {
  if (this.video.id) {
    //修改小节
    this.updateVideo();
  } else {
    //新增小节
    this.saveVideo();
  }
},

三、课程发布信息预览 3.1、后端接口

1、创建 VO

@Data
public class CoursePublishVo implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;//课程id

    private String title; //课程名称

    private String cover; //封面

    private Integer lessonNum;//课时数

    private String subjectLevelOne;//一级分类

    private String subjectLevelTwo;//二级分类

    private String teacherName;//讲师名称

    private String price;//价格 ,只用于显示

}

2、控制层

// 根据课程id查询课程确认信息
@GetMapping("/getPublishCourseInfo/{id}")
public R getPublishCourseInfo(@PathVariable String id) {
    CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
    return R.ok().data("data", publishCourseInfo);
}

3、业务层

@Override
public CoursePublishVo getPublishCourseInfo(String id) {
    return eduCourseMapper.getPublishCourseInfo(id);
}

4、数据层

CoursePublishVo getPublishCourseInfo(String id);