一、定义Form表单对象
VideoInfoForm.java
package com.guli.edu.form;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author helen
* @since 2019/3/5
*/
@ApiModel(value = "课时基本信息", description = "编辑课时基本信息的表单对象")
@Data
public class VideoInfoForm {
@ApiModelProperty(value = "视频ID")
private String id;
@ApiModelProperty(value = "节点名称")
private String title;
@ApiModelProperty(value = "课程ID")
private String courseId;
@ApiModelProperty(value = "章节ID")
private String chapterId;
@ApiModelProperty(value = "视频资源")
private String videoSourceId;
@ApiModelProperty(value = "显示排序")
private Integer sort;
@ApiModelProperty(value = "是否可以试听:0默认 1免费")
private Boolean free;
}
二、课时保存
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;
}