From b7d117ba790e25c9a6b01eb1415a8bf9e4ab1ff2 Mon Sep 17 00:00:00 2001 From: lan-yonghui Date: Fri, 1 Dec 2023 11:33:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=AE=A1=E7=90=86=E6=A8=A1=E5=9D=97=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=9A=E4=B9=89=E6=97=A5=E5=BF=97=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=20Blob=20=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiFileResourceService.java | 6 --- .../definition/ApiDefinitionLogService.java | 24 ++++----- .../definition/ApiDefinitionService.java | 51 ++++++++++++------- .../ApiDefinitionControllerTests.java | 7 ++- 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java index a6d476280a..4cd55ae681 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java @@ -223,12 +223,6 @@ public class ApiFileResourceService { return apiFileResourceMapper.selectByExample(example); } - public List getFileIdsByResourceId(String resourceId) { - return getByResourceId(resourceId).stream() - .map(ApiFileResource::getFileId) - .toList(); - } - /** * 上传临时文件 * system/temp/{fileId}/{fileName} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java index b48e1d813a..ed64e6f6e5 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java @@ -63,8 +63,8 @@ public class ApiDefinitionLogService { * @return */ public LogDTO updateLog(ApiDefinitionUpdateRequest request) { - ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); - if(apiDefinition != null){ + ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId()); + if(apiDefinition.getId() != null){ LogDTO dto = new LogDTO( request.getProjectId(), null, @@ -89,8 +89,8 @@ public class ApiDefinitionLogService { * @return */ public LogDTO delLog(ApiDefinitionDeleteRequest request) { - ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); - if(apiDefinition != null){ + ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId()); + if(apiDefinition.getId() != null){ LogDTO dto = new LogDTO( request.getProjectId(), null, @@ -173,8 +173,8 @@ public class ApiDefinitionLogService { } public LogDTO copyLog(ApiDefinitionCopyRequest request) { - ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); - if(apiDefinition != null){ + ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId()); + if(apiDefinition.getId() != null){ LogDTO dto = new LogDTO( apiDefinition.getProjectId(), null, @@ -219,8 +219,8 @@ public class ApiDefinitionLogService { } public LogDTO followLog(String id) { - ApiDefinition apiDefinition = apiDefinitionService.get(id, SessionUtils.getUserId()); - if(apiDefinition != null){ + ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(id, SessionUtils.getUserId()); + if(apiDefinition.getId() != null){ Project project = projectMapper.selectByPrimaryKey(apiDefinition.getProjectId()); LogDTO dto = new LogDTO( apiDefinition.getProjectId(), @@ -246,8 +246,8 @@ public class ApiDefinitionLogService { * @return */ public LogDTO restoreLog(ApiDefinitionDeleteRequest request) { - ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); - if(apiDefinition != null){ + ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId()); + if(apiDefinition.getId() != null){ LogDTO dto = new LogDTO( request.getProjectId(), null, @@ -304,8 +304,8 @@ public class ApiDefinitionLogService { * 删除回收站接口定义接口日志 */ public LogDTO trashDelLog(ApiDefinitionDeleteRequest request) { - ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); - if(apiDefinition != null){ + ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId()); + if(apiDefinition.getId() != null){ LogDTO dto = new LogDTO( request.getProjectId(), null, diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index 966cb8217c..53315cdc7a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -16,7 +16,10 @@ import io.metersphere.sdk.constants.ApplicationNumScope; import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.util.*; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.FileAssociationSourceUtil; +import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.SubListUtils; import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.service.UserLoginService; @@ -114,24 +117,9 @@ public class ApiDefinitionService { } public ApiDefinitionDTO get(String id, String userId){ - ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO(); // 1. 避免重复查询数据库,将查询结果传递给get方法 ApiDefinition apiDefinition = checkApiDefinition(id); - // 2. 使用Optional避免空指针异常 - Optional apiDefinitionBlobOptional = Optional.ofNullable(apiDefinitionBlobMapper.selectByPrimaryKey(id)); - apiDefinitionBlobOptional.ifPresent(blob -> { - apiDefinitionDTO.setRequest(ApiDataUtils.parseObject(new String(blob.getRequest()), AbstractMsTestElement.class)); - // blob.getResponse() 为 null 时不进行转换 - if (blob.getResponse() != null) { - apiDefinitionDTO.setResponse(ApiDataUtils.parseArray(new String(blob.getResponse()), HttpResponse.class)); - } - }); - // 3. 使用Stream简化集合操作 - ApiDefinitionFollowerExample example = new ApiDefinitionFollowerExample(); - example.createCriteria().andApiDefinitionIdEqualTo(id).andUserIdEqualTo(userId); - apiDefinitionDTO.setFollow(apiDefinitionFollowerMapper.countByExample(example) > 0); - BeanUtils.copyBean(apiDefinitionDTO, apiDefinition); - return apiDefinitionDTO; + return getApiDefinitionInfo(id, userId, apiDefinition); } public ApiDefinition create(ApiDefinitionAddRequest request, String userId) { @@ -652,4 +640,33 @@ public class ApiDefinitionService { public String uploadTempFile(MultipartFile file) { return apiFileResourceService.uploadTempFile(file); } + + public ApiDefinitionDTO getInfo(String id, String userId){ + ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO(); + ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(id); + if(apiDefinition != null){ + apiDefinitionDTO = getApiDefinitionInfo(id, userId, apiDefinition); + } + return apiDefinitionDTO; + } + + public ApiDefinitionDTO getApiDefinitionInfo(String id, String userId, ApiDefinition apiDefinition) { + ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO(); + // 2. 使用Optional避免空指针异常 + Optional apiDefinitionBlobOptional = Optional.ofNullable(apiDefinitionBlobMapper.selectByPrimaryKey(id)); + apiDefinitionBlobOptional.ifPresent(blob -> { + apiDefinitionDTO.setRequest(ApiDataUtils.parseObject(new String(blob.getRequest()), AbstractMsTestElement.class)); + // blob.getResponse() 为 null 时不进行转换 + if (blob.getResponse() != null) { + apiDefinitionDTO.setResponse(ApiDataUtils.parseArray(new String(blob.getResponse()), HttpResponse.class)); + } + }); + // 3. 使用Stream简化集合操作 + ApiDefinitionFollowerExample example = new ApiDefinitionFollowerExample(); + example.createCriteria().andApiDefinitionIdEqualTo(id).andUserIdEqualTo(userId); + apiDefinitionDTO.setFollow(apiDefinitionFollowerMapper.countByExample(example) > 0); + BeanUtils.copyBean(apiDefinitionDTO, apiDefinition); + return apiDefinitionDTO; + } + } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index 3d2d0af736..3cb05d9b28 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -23,7 +23,6 @@ import io.metersphere.system.dto.sdk.BaseCondition; import io.metersphere.system.file.FileCenter; import io.metersphere.system.file.FileRequest; import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.utils.Pager; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; @@ -35,10 +34,7 @@ import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MvcResult; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import java.io.File; import java.nio.charset.StandardCharsets; import java.util.*; @@ -647,6 +643,9 @@ public class ApiDefinitionControllerTests extends BaseTest { }); } checkLog(apiDefinitionDeleteRequest.getId(), OperationLogType.DELETE); + apiDefinitionDeleteRequest.setId("121"); + apiDefinitionDeleteRequest.setDeleteAll(false); + assertErrorCode(this.requestPost(DELETE, apiDefinitionDeleteRequest), ApiResultCode.API_DEFINITION_NOT_EXIST); // @@校验权限 requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, DELETE, apiDefinitionDeleteRequest); }