一、定义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

  1. package com.guli.edu.controller.admin;
  2. @Api(description="课时管理")
  3. @CrossOrigin //跨域
  4. @RestController
  5. @RequestMapping("/admin/edu/video")
  6. public class VideoAdminController {
  7. @Autowired
  8. private VideoService videoService;
  9. @ApiOperation(value = "新增课时")
  10. @PostMapping("save-video-info")
  11. public R save(
  12. @ApiParam(name = "videoForm", value = "课时对象", required = true)
  13. @RequestBody VideoInfoForm videoInfoForm){
  14. videoService.saveVideoInfo(videoInfoForm);
  15. return R.ok();
  16. }
  17. }

2、业务层

VideoService.java

  1. void saveVideoInfo(VideoInfoForm videoInfoForm);

VideoServiceImpl.java

  1. @Override
  2. public void saveVideoInfo(VideoInfoForm videoInfoForm) {
  3. Video video = new Video();
  4. BeanUtils.copyProperties(videoInfoForm, video);
  5. boolean result = this.save(video);
  6. if(!result){
  7. throw new GuliException(20001, "课时信息保存失败");
  8. }
  9. }

三、课时的修改

1、web层接口的定义

VideoAdminController.java

  1. @ApiOperation(value = "根据ID查询课时")
  2. @GetMapping("video-info/{id}")
  3. public R getVideInfoById(
  4. @ApiParam(name = "id", value = "课时ID", required = true)
  5. @PathVariable String id){
  6. VideoInfoForm videoInfoForm = videoService.getVideoInfoFormById(id);
  7. return R.ok().data("item", videoInfoForm);
  8. }
  9. @ApiOperation(value = "更新课时")
  10. @PutMapping("update-video-info/{id}")
  11. public R updateCourseInfoById(
  12. @ApiParam(name = "VideoInfoForm", value = "课时基本信息", required = true)
  13. @RequestBody VideoInfoForm videoInfoForm,
  14. @ApiParam(name = "id", value = "课时ID", required = true)
  15. @PathVariable String id){
  16. videoService.updateVideoInfoById(videoInfoForm);
  17. return R.ok();
  18. }

2、业务层

VideoService.java

  1. VideoInfoForm getVideoInfoFormById(String id);
  2. void updateVideoInfoById(VideoInfoForm videoInfoForm);

VideoServiceImpl.java

  1. @Override
  2. public VideoInfoForm getVideoInfoFormById(String id) {
  3. //从video表中取数据
  4. Video video = this.getById(id);
  5. if(video == null){
  6. throw new GuliException(20001, "数据不存在");
  7. }
  8. //创建videoInfoForm对象
  9. VideoInfoForm videoInfoForm = new VideoInfoForm();
  10. BeanUtils.copyProperties(video, videoInfoForm);
  11. return videoInfoForm;
  12. }
  13. @Override
  14. public void updateVideoInfoById(VideoInfoForm videoInfoForm) {
  15. //保存课时基本信息
  16. Video video = new Video();
  17. BeanUtils.copyProperties(videoInfoForm, video);
  18. boolean result = this.updateById(video);
  19. if(!result){
  20. throw new GuliException(20001, "课时信息保存失败");
  21. }
  22. }

四、课时的删除

1、web层接口的定义

VideoAdminController.java

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

2、业务层

VideoService.java

  1. boolean removeVideoById(String id);

VideoServiceImpl.java

  1. @Override
  2. public boolean removeVideoById(String id) {
  3. //删除视频资源 TODO
  4. Integer result = baseMapper.deleteById(id);
  5. return null != result && result > 0;
  6. }