优化资源分类id必填

This commit is contained in:
none 2023-03-02 13:58:03 +08:00
parent 26a26c52c2
commit 0037b9e9ae
4 changed files with 36 additions and 33 deletions

View File

@ -57,29 +57,16 @@ public class ResourceController {
} }
@PostMapping("/create") @PostMapping("/create")
public JsonResponse store(@RequestBody @Validated ResourceRequest request) { public JsonResponse store(@RequestBody @Validated ResourceRequest req) {
if (categoryService.getById(request.getCategoryId()) == null) { if (categoryService.getById(req.getCategoryId()) == null) {
return JsonResponse.error("资源分类不存在"); return JsonResponse.error("资源分类不存在");
} }
if (!Arrays.asList(BackendConstant.RESOURCE_DISK_WHITELIST).contains(request.getDisk())) { if (!Arrays.asList(BackendConstant.RESOURCE_DISK_WHITELIST).contains(req.getDisk())) {
return JsonResponse.error("存储磁盘参数错误"); return JsonResponse.error("存储磁盘参数错误");
} }
Resource resource = new Resource(); Resource res = resourceService.create(req.getCategoryId(), req.getName(), req.getExtension(), req.getSize(), req.getDisk(), req.getFileId(), req.getPath(), req.getUrl());
return JsonResponse.data(res);
resource.setCategoryId(request.getCategoryId());
resource.setName(request.getName());
resource.setExtension(request.getExtension());
resource.setSize(request.getSize());
resource.setDisk(request.getDisk());
resource.setFileId(request.getFileId());
resource.setPath(request.getPath());
resource.setUrl(request.getUrl());
resource.setCreatedAt(new Date());
resourceService.save(resource);
return JsonResponse.success();
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -12,12 +12,12 @@ import org.springframework.web.multipart.MultipartFile;
import xyz.playedu.api.config.MinioConfig; import xyz.playedu.api.config.MinioConfig;
import xyz.playedu.api.constant.BackendConstant; import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource; import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.service.ResourceService; import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.types.JsonResponse; import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.util.HelperUtil; import xyz.playedu.api.util.HelperUtil;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
/** /**
@ -31,6 +31,9 @@ public class UploadController {
@Autowired @Autowired
private ResourceService resourceService; private ResourceService resourceService;
@Autowired
private ResourceCategoryService resourceCategoryService;
@Autowired @Autowired
private MinioConfig minioConfig; private MinioConfig minioConfig;
@ -39,7 +42,6 @@ public class UploadController {
@PostMapping("/image") @PostMapping("/image")
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) { public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
Integer categoryId = MapUtils.getInteger(params, "category_id", 0);
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) { if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
return JsonResponse.error("请上传文件"); return JsonResponse.error("请上传文件");
} }
@ -49,6 +51,11 @@ public class UploadController {
return JsonResponse.error("格式不支持"); return JsonResponse.error("格式不支持");
} }
Integer categoryId = MapUtils.getInteger(params, "category_id", 0);
if (resourceCategoryService.getById(categoryId) == null) {
return JsonResponse.error("分类不存在");
}
String filename = file.getOriginalFilename(); String filename = file.getOriginalFilename();
String ext = HelperUtil.fileExt(filename); String ext = HelperUtil.fileExt(filename);
if (!Arrays.asList(BackendConstant.UPLOAD_IMAGE_EXT_WL).contains(ext)) { if (!Arrays.asList(BackendConstant.UPLOAD_IMAGE_EXT_WL).contains(ext)) {
@ -71,19 +78,8 @@ public class UploadController {
String url = minioConfig.getDomain() + minioConfig.getBucket() + "/" + savePath; String url = minioConfig.getDomain() + minioConfig.getBucket() + "/" + savePath;
Resource resource = new Resource(); Resource res = resourceService.create(categoryId, oldFilename, ext, file.getSize(), "minio", "", savePath, url);
resource.setCategoryId(categoryId); return JsonResponse.data(res);
resource.setName(oldFilename);
resource.setExtension(ext);
resource.setSize(file.getSize());
resource.setDisk("minio");
resource.setFileId("");
resource.setPath(savePath);
resource.setUrl(url);
resource.setCreatedAt(new Date());
resourceService.save(resource);
return JsonResponse.data(resource);
} catch (Exception e) { } catch (Exception e) {
return JsonResponse.error("系统错误"); return JsonResponse.error("系统错误");
} }

View File

@ -14,4 +14,6 @@ public interface ResourceService extends IService<Resource> {
PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter); PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter);
Resource create(Integer categoryId, String filename, String ext, Long size, String disk, String fileId, String path, String url);
} }

View File

@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
import xyz.playedu.api.types.paginate.PaginationResult; import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter; import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
import java.util.Date;
/** /**
* @author tengteng * @author tengteng
* @description 针对表resources的数据库操作Service实现 * @description 针对表resources的数据库操作Service实现
@ -43,6 +45,22 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
return pageResult; return pageResult;
} }
@Override
public Resource create(Integer categoryId, String filename, String ext, Long size, String disk, String fileId, String path, String url) {
Resource resource = new Resource();
resource.setCategoryId(categoryId);
resource.setName(filename);
resource.setExtension(ext);
resource.setSize(size);
resource.setDisk(disk);
resource.setFileId(fileId);
resource.setPath(path);
resource.setUrl(url);
resource.setCreatedAt(new Date());
save(resource);
return resource;
}
} }