Skip to main content

一、新增章节

web层

@ApiOperation(value = "新增章节")
@PostMapping
public R save(
@ApiParam(name = "chapterVo", value = "章节对象", required = true)
@RequestBody Chapter chapter){

chapterService.save(chapter);
return R.ok();
}

二、根据id查询

web层

@ApiOperation(value = "根据ID查询章节")
@GetMapping("{id}")
public R getById(
@ApiParam(name = "id", value = "章节ID", required = true)
@PathVariable String id){

Chapter chapter = chapterService.getById(id);
return R.ok().data("item", chapter);
}

三、更新

web层

@ApiOperation(value = "根据ID修改章节")
@PutMapping("{id}")
public R updateById(
@ApiParam(name = "id", value = "章节ID", required = true)
@PathVariable String id,

@ApiParam(name = "chapter", value = "章节对象", required = true)
@RequestBody Chapter chapter){

chapter.setId(id);
chapterService.updateById(chapter);
return R.ok();
}

四、删除

1、web层

@ApiOperation(value = "根据ID删除章节")
@DeleteMapping("{id}")
public R removeById(
@ApiParam(name = "id", value = "章节ID", required = true)
@PathVariable String id){

boolean result = chapterService.removeChapterById(id);
if(result){
return R.ok();
}else{
return R.error().message("删除失败");
}
}

2、Service

ChapterService层:接口

boolean removeChapterById(String id);

ChapterService层:实现

@Override
public boolean removeChapterById(String id) {

//根据id查询是否存在视频,如果有则提示用户尚有子节点
if(videoService.getCountByChapterId(id)){
throw new GuliException(20001,"该分章节下存在视频课程,请先删除视频课程");
}

Integer result = baseMapper.deleteById(id);
return null != result && result > 0;
}

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测试