删除课程的同时删除所有小节视频
需求:删除课程的同时删除云端视频
一个课程有很多章节,一个章节有很多小节,每个小节都有视频,删除课程的时候,删除视频有多个,怎么实现删除课程的同时删除阿里云端所有小节。
1、在service模块添加pom依赖
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、在调用端的启动类添加注解
@EnableFeignClients
3、创建包和接口
创建client包
@FeignClient注解用于指定从哪个服务中调用功能 ,名称与被调用的服务名保持一致。
@DeleteMapping注解用于对被调用的微服务进行地址映射。
@PathVariable注解一定要指定参数名称,否则出错
@Component注解防止,在其他位置注入CodClient时idea报错
package com.nonglin.eduservice.client;
import com.nonglin.commonutils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @Description :
* @Author :lenovo
* @Date :2021/3/24 15:07
*/
@FeignClient("service-vod")
@Component
public interface VodClient {
//根据视频id删除阿里云视频
@DeleteMapping("/eduvod/video/removeAlyVideo/{id}")
public R removeAlyClient(@PathVariable("id") String id);
}
4、在service-vod中创建接口。用于删除多个视频
controller
//删除多个阿里云端视频
@DeleteMapping("delete-batch")
public R deleteBatch(@RequestParam("videoIdList") List videoIdList){
vodService.removeMoreAlyVideo(videoIdList);
return R.ok();
}
service接口
//删除多个阿里云端视频
void removeMoreAlyVideo(List videoIdList);
service实现类
//删除多个阿里云端视频
@Override
public void removeMoreAlyVideo(List<String> videoIdList) {
try {
//初始化对象
DefaultAcsClient client = InitVodClient.initVodClient(
ConstantVodUtils.ACCESS_KEY_ID,
ConstantVodUtils.ACCESS_KEY_SECRET);
//创建删除视频request对象
DeleteVideoRequest request = new DeleteVideoRequest();
//videoIdList值转换成1,2,3
String videoIds = StringUtils.join(videoIdList.toArray(), ",");
//向request设置视频id
request.setVideoIds(videoIds);
//调用初始化对象的方法实现删除
client.getAcsResponse(request);
}catch(Exception e){
e.printStackTrace();
throw new MyException(20001,"删除视频失败");
}
}
5、在servicr_edu中调用service_vod接口实现删除多个视频的功能
package com.nonglin.eduservice.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nonglin.eduservice.client.VodClient;
import com.nonglin.eduservice.entity.EduVideo;
import com.nonglin.eduservice.mapper.EduVideoMapper;
import com.nonglin.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 课程视频 服务实现类
* </p>
*
* @author Lenovo
* @since 2021-03-09
*/
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
@Autowired
private VodClient vodClient;
//1 根据课程id删除小节
// TODO 删除小节,删除对应视频文件
@Override
public void removeVideoByCourseId(String courseId) {
QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
wrapperVideo.eq("course_id",courseId);
wrapperVideo.select("video_source_id");
List<EduVideo> eduVideoList = baseMapper.selectList(wrapperVideo);
//List<EduVideo> 变成List<String>
List<String> videoIds = new ArrayList<>();
for (int i = 0; i < eduVideoList.size(); i++) {
EduVideo eduVideo = eduVideoList.get(i);
String videoSouceId = eduVideo.getVideoSourceId();
if(!StringUtils.isEmpty(videoSouceId)){
//放到videoIds集合里面
videoIds.add(videoSouceId);
}
}
if(videoIds.size()>0){
//根据多个视频id删除多个视频
vodClient.deleteBatch(videoIds);
}
QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
wrapper.eq("course_id",courseId);
baseMapper.delete(wrapper);
}
}