一、定义Form表单对象

VideoInfoForm.java

  1. package com.guli.edu.form;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import lombok.Data;
  5. /**
  6. * @author helen
  7. * @since 2019/3/5
  8. */
  9. @ApiModel(value = "课时基本信息", description = "编辑课时基本信息的表单对象")
  10. @Data
  11. public class VideoInfoForm {
  12. @ApiModelProperty(value = "视频ID")
  13. private String id;
  14. @ApiModelProperty(value = "节点名称")
  15. private String title;
  16. @ApiModelProperty(value = "课程ID")
  17. private String courseId;
  18. @ApiModelProperty(value = "章节ID")
  19. private String chapterId;
  20. @ApiModelProperty(value = "视频资源")
  21. private String videoSourceId;
  22. @ApiModelProperty(value = "显示排序")
  23. private Integer sort;
  24. @ApiModelProperty(value = "是否可以试听:0默认 1免费")
  25. private Boolean free;
  26. }

二、课时保存

1、web层接口的定义

VideoAdminController.java

package com.guli.edu.controller.admin;

@Api(description="课时管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/edu/video")
public class VideoAdminController {


  @Autowired
  private VideoService videoService;


  @ApiOperation(value = "新增课时")
  @PostMapping("save-video-info")
  public R save(
          @ApiParam(name = "videoForm", value = "课时对象", required = true)
          @RequestBody VideoInfoForm videoInfoForm){


      videoService.saveVideoInfo(videoInfoForm);
      return R.ok();
  }

}

2、业务层

VideoService.java

void saveVideoInfo(VideoInfoForm videoInfoForm);

VideoServiceImpl.java

@Override
public void saveVideoInfo(VideoInfoForm videoInfoForm) {


  Video video = new Video();
  BeanUtils.copyProperties(videoInfoForm, video);
  boolean result = this.save(video);


  if(!result){
      throw new GuliException(20001, "课时信息保存失败");
  }

}

三、课时的修改

1、web层接口的定义

VideoAdminController.java

@ApiOperation(value = "根据ID查询课时")
@GetMapping("video-info/{id}")
public R getVideInfoById(
   @ApiParam(name = "id", value = "课时ID", required = true)
   @PathVariable String id){


   VideoInfoForm videoInfoForm = videoService.getVideoInfoFormById(id);
   return R.ok().data("item", videoInfoForm);

}


@ApiOperation(value = "更新课时")
@PutMapping("update-video-info/{id}")
public R updateCourseInfoById(
  @ApiParam(name = "VideoInfoForm", value = "课时基本信息", required = true)
  @RequestBody VideoInfoForm videoInfoForm,


  @ApiParam(name = "id", value = "课时ID", required = true)
  @PathVariable String id){


  videoService.updateVideoInfoById(videoInfoForm);
  return R.ok();

}

2、业务层

VideoService.java

VideoInfoForm getVideoInfoFormById(String id);

void updateVideoInfoById(VideoInfoForm videoInfoForm);

VideoServiceImpl.java

@Override
public VideoInfoForm getVideoInfoFormById(String id) {
  //从video表中取数据
  Video video = this.getById(id);
  if(video == null){
      throw new GuliException(20001, "数据不存在");
  }


  //创建videoInfoForm对象
  VideoInfoForm videoInfoForm = new VideoInfoForm();
  BeanUtils.copyProperties(video, videoInfoForm);


  return videoInfoForm;

}


@Override

public void updateVideoInfoById(VideoInfoForm videoInfoForm) {
  //保存课时基本信息
  Video video = new Video();
  BeanUtils.copyProperties(videoInfoForm, video);
  boolean result = this.updateById(video);
  if(!result){
      throw new GuliException(20001, "课时信息保存失败");
  }

}

四、课时的删除

1、web层接口的定义

VideoAdminController.java

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


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

}

2、业务层

VideoService.java

boolean removeVideoById(String id);

VideoServiceImpl.java








@Override
public boolean removeVideoById(String id) {


   //删除视频资源 TODO


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

}