3.3 保存视频信息 3.3.1 需求分析
用户进入课程计划页面,选择视频,将课程计划与视频信息保存在课程管理数据库中。 用户操作流程: 1、进入课程计划,点击”选择视频“,打开我的媒资查询页面 2、为课程计划选择对应的视频,选择“选择” 3、前端请求课程管理服务保存课程计划与视频信息。 3.3.2 数据模型 在课程管理数据库创建表 teachplan_media 存储课程计划与媒资关联信息,如下: 创建teachplanMedia 模型类:
[mw_shl_code=applescript,true]@Data
@ToString @Entity
@Table(name="teachplan_media")
@GenericGenerator(name = "jpa‐assigned", strategy = "assigned") public class TeachplanMedia implements Serializable { private static final long serialVersionUID = ‐916357110051689485L;
@Id
@GeneratedValue(generator = "jpa‐assigned")
@Column(name="teachplan_id")
private String teachplanId;
@Column(name="media_id")
private String mediaId;
@Column(name="media_fileoriginalname")
private String mediaFileOriginalName;
@Column(name="media_url")
private String mediaUrl;
@Column(name="courseid")
private String courseId; }
[/mw_shl_code]
3.3.3 API接口 此接口作为前端请求课程管理服务保存课程计划与视频信息的接口: 在课程管理服务增加接口:
[mw_shl_code=applescript,true]@ApiOperation("保存媒资信息")
public ResponseResult savemedia(TeachplanMedia teachplanMedia);[/mw_shl_code]
3.3.4 服务端开发 3.3.3.1 DAO
创建TeachplanMediaRepository用于对TeachplanMedia的操作。
[mw_shl_code=applescript,true]public interface TeachplanMediaRepository extends JpaRepository<TeachplanMedia, String> {
}
[/mw_shl_code]
3.3.3.2 Service
[mw_shl_code=applescript,true]//保存媒资信息 public ResponseResult savemedia(TeachplanMedia teachplanMedia) {
if(teachplanMedia == null){
ExceptionCast.cast(CommonCode.INVALIDPARAM);
}
//课程计划
String teachplanId = teachplanMedia.getTeachplanId();
//查询课程计划
Optional<Teachplan> optional = teachplanRepository.findById(teachplanId);
if(!optional.isPresent()){
ExceptionCast.cast(CourseCode.COURSE_MEDIA_TEACHPLAN_ISNULL);
}
Teachplan teachplan = optional.get();
//只允许为叶子结点课程计划选择视频
String grade = teachplan.getGrade();
if(StringUtils.isEmpty(grade) || !grade.equals("3")){
ExceptionCast.cast(CourseCode.COURSE_MEDIA_TEACHPLAN_GRADEERROR);
}
TeachplanMedia one = null;
Optional<TeachplanMedia> teachplanMediaOptional = teachplanMediaRepository.findById(teachplanId);
if(!teachplanMediaOptional.isPresent()){
one = new TeachplanMedia();
}else{
one = teachplanMediaOptional.get();
}
//保存媒资信息与课程计划信息
one.setTeachplanId(teachplanId);
one.setCourseId(teachplanMedia.getCourseId());
one.setMediaFileOriginalName(teachplanMedia.getMediaFileOriginalName());
one.setMediaId(teachplanMedia.getMediaId());
one.setMediaUrl(teachplanMedia.getMediaUrl());
teachplanMediaRepository.save(one);
return new ResponseResult(CommonCode.SUCCESS);
}[/mw_shl_code]
3.3.3.3 Controller
[mw_shl_code=applescript,true]@Override
@PostMapping("/savemedia") public ResponseResult savemedia(@RequestBody TeachplanMedia teachplanMedia) { return courseService.savemedia(teachplanMedia);
}[/mw_shl_code]