优化资源删除

This commit is contained in:
none 2023-03-07 11:52:19 +08:00
parent 4df9680361
commit 0118e0a164
5 changed files with 46 additions and 3 deletions

View File

@ -1,13 +1,18 @@
package xyz.playedu.api.controller.backend;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.config.MinioConfig;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.domain.ResourceCategory;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.request.backend.ResourceRequest;
import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.service.ResourceService;
@ -23,6 +28,7 @@ import java.util.*;
* @create 2023/2/23 10:50
*/
@RestController
@Slf4j
@RequestMapping("/backend/v1/resource")
public class ResourceController {
@ -35,6 +41,12 @@ public class ResourceController {
@Autowired
private ResourceCategoryService categoryService;
@Autowired
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
@GetMapping("/index")
public JsonResponse index(@RequestParam HashMap<String, Object> params) {
Integer page = MapUtils.getInteger(params, "page", 1);
@ -108,9 +120,20 @@ public class ResourceController {
}
@DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
resourceService.removeById(id);
return JsonResponse.success();
@Transactional
public JsonResponse destroy(@PathVariable(name = "id") Integer id) throws NotFoundException {
Resource resource = resourceService.findOrFail(id);
try {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucket()).object(resource.getPath()).build());
if (resource.getType().equals(BackendConstant.RESOURCE_TYPE_VIDEO)) {
resourceVideoService.removeByRid(resource.getId());
}
resourceService.removeById(resource.getId());
return JsonResponse.success();
} catch (Exception e) {
log.error(e.getMessage());
return JsonResponse.error("系统错误");
}
}
}

View File

@ -2,6 +2,7 @@ package xyz.playedu.api.service;
import xyz.playedu.api.domain.Resource;
import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
@ -16,4 +17,6 @@ public interface ResourceService extends IService<Resource> {
Resource create(Integer categoryId, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url);
Resource findOrFail(Integer id) throws NotFoundException;
}

View File

@ -11,4 +11,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface ResourceVideoService extends IService<ResourceVideo> {
void create(Integer resourceId, Integer duration);
void removeByRid(Integer resourceId);
}

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.mapper.ResourceMapper;
import org.springframework.stereotype.Service;
@ -83,6 +84,15 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
save(resource);
return resource;
}
@Override
public Resource findOrFail(Integer id) throws NotFoundException {
Resource resource = getById(id);
if (resource == null) {
throw new NotFoundException("资源不存在");
}
return resource;
}
}

View File

@ -24,6 +24,11 @@ public class ResourceVideoServiceImpl extends ServiceImpl<ResourceVideoMapper, R
video.setCreatedAt(new Date());
save(video);
}
@Override
public void removeByRid(Integer resourceId) {
remove(query().getWrapper().eq("rid", resourceId));
}
}