一、新增章节

web层

  1. @ApiOperation(value = "新增章节")
  2. @PostMapping
  3. public R save(
  4. @ApiParam(name = "chapterVo", value = "章节对象", required = true)
  5. @RequestBody Chapter chapter){
  6. chapterService.save(chapter);
  7. return R.ok();
  8. }

二、根据id查询

web层

  1. @ApiOperation(value = "根据ID查询章节")
  2. @GetMapping("{id}")
  3. public R getById(
  4. @ApiParam(name = "id", value = "章节ID", required = true)
  5. @PathVariable String id){
  6. Chapter chapter = chapterService.getById(id);
  7. return R.ok().data("item", chapter);
  8. }

三、更新

web层

  1. @ApiOperation(value = "根据ID修改章节")
  2. @PutMapping("{id}")
  3. public R updateById(
  4. @ApiParam(name = "id", value = "章节ID", required = true)
  5. @PathVariable String id,
  6. @ApiParam(name = "chapter", value = "章节对象", required = true)
  7. @RequestBody Chapter chapter){
  8. chapter.setId(id);
  9. chapterService.updateById(chapter);
  10. return R.ok();
  11. }

四、删除

1、web层

  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 = chapterService.removeChapterById(id);
  7. if(result){
  8. return R.ok();
  9. }else{
  10. return R.error().message("删除失败");
  11. }
  12. }

2、Service

ChapterService层:接口

  1. boolean removeChapterById(String id);

ChapterService层:实现

  1. @Override
  2. public boolean removeChapterById(String id) {
  3. //根据id查询是否存在视频,如果有则提示用户尚有子节点
  4. if(videoService.getCountByChapterId(id)){
  5. throw new GuliException(20001,"该分章节下存在视频课程,请先删除视频课程");
  6. }
  7. Integer result = baseMapper.deleteById(id);
  8. return null != result && result > 0;
  9. }

VideoService:接口

boolean getCountByChapterId(String chapterId);

VideoService:实现

@Override
public boolean getCountByChapterId(String chapterId) {
  QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
  queryWrapper.eq("chapter_id", chapterId);
  Integer count = baseMapper.selectCount(queryWrapper);
  return null != count && count > 0;

}

五、Swagger测试