From 21b8c1d4b6800e1032de23e2c025b639c8e16d7c Mon Sep 17 00:00:00 2001 From: lan-yonghui Date: Tue, 19 Dec 2023 14:02:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E7=AE=A1=E7=90=86=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=E6=81=A2=E5=A4=8D=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E6=8E=A5=E5=8F=A3=E6=A8=A1=E5=9D=97=E5=B7=B2?= =?UTF-8?q?=E5=88=A0=E9=99=A4=EF=BC=8C=E9=87=8D=E7=BD=AE=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=20ID=20=E4=B8=BA=E6=9C=AA=E8=A7=84=E5=88=92?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definition/ApiDefinitionService.java | 33 ++++++++++++++++--- .../ApiDefinitionControllerTests.java | 9 ++--- .../resources/dml/init_api_definition.sql | 5 +-- 3 files changed, 37 insertions(+), 10 deletions(-) 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 086dbbcbb9..00c77e05ba 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 @@ -458,7 +458,7 @@ public class ApiDefinitionService { ApiDefinitionExample example = new ApiDefinitionExample(); example.createCriteria() .andPathEqualTo(apiDefinition.getPath()).andMethodEqualTo(apiDefinition.getMethod()) - .andProtocolEqualTo(apiDefinition.getProtocol()); + .andProtocolEqualTo(apiDefinition.getProtocol()).andVersionIdEqualTo(apiDefinition.getVersionId()); if (CollectionUtils.isNotEmpty(apiDefinitionMapper.selectByExample(example))) { throw new MSException(ApiResultCode.API_DEFINITION_EXIST); } @@ -469,7 +469,7 @@ public class ApiDefinitionService { ApiDefinitionExample example = new ApiDefinitionExample(); example.createCriteria() .andIdNotEqualTo(apiDefinition.getId()).andProtocolEqualTo(apiDefinition.getProtocol()) - .andPathEqualTo(apiDefinition.getPath()).andMethodEqualTo(apiDefinition.getMethod()); + .andPathEqualTo(apiDefinition.getPath()).andMethodEqualTo(apiDefinition.getMethod()).andVersionIdEqualTo(apiDefinition.getVersionId()); if (apiDefinitionMapper.countByExample(example) > 0) { throw new MSException(ApiResultCode.API_DEFINITION_EXIST); } @@ -660,12 +660,13 @@ public class ApiDefinitionService { private void doRestore(List apiIds, String userId, String projectId, boolean isBatch) { if (CollectionUtils.isNotEmpty(apiIds)) { - // 记录恢复数据之前的原数据,单条通过注解记录 + // 记录恢复数据之前的原数据日志,单条通过注解记录日志 if(isBatch){ apiDefinitionLogService.batchRestoreLog(apiIds, userId, projectId); } extApiDefinitionMapper.batchRestoreById(apiIds, userId, projectId); + List updateApiIds = new ArrayList<>(); apiIds.forEach(id -> { // 恢复数据恢复最新标识 ApiDefinition apiDefinition = checkApiDefinition(id); @@ -675,12 +676,36 @@ public class ApiDefinitionService { if (CollectionUtils.isNotEmpty(apiDefinitionVersions) && apiDefinitionVersions.size() > 1) { handleMultipleVersions(apiDefinition); } + + // 判断接口的模块 ID 是否存在,不存在修改模块 ID 为未规划模块 ID + if (!ModuleConstants.DEFAULT_NODE_ID.equals(apiDefinition.getModuleId()) && + moduleNeedsUpdate(apiDefinition.getModuleId())) { + updateApiIds.add(apiDefinition.getId()); + } + }); + // 模块已删除,修改为未规划模块 ID + handleModule(updateApiIds); // 恢复接口关联数据 recoverApiRelatedData(apiIds, userId, projectId); } } + private boolean moduleNeedsUpdate(String moduleId) { + ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(moduleId); + return apiDefinitionModule == null || StringUtils.isBlank(apiDefinitionModule.getName()); + } + + private void handleModule(List updateApiIds) { + if(!updateApiIds.isEmpty()){ + ApiDefinition updateApiDefinition = new ApiDefinition(); + updateApiDefinition.setModuleId(ModuleConstants.DEFAULT_NODE_ID); + ApiDefinitionExample apiDefinitionExample = new ApiDefinitionExample(); + apiDefinitionExample.createCriteria().andIdIn(updateApiIds); + apiDefinitionMapper.updateByExampleSelective(updateApiDefinition, apiDefinitionExample); + } + } + private void handleMultipleVersions(ApiDefinition apiDefinition) { String defaultVersion = extBaseProjectVersionMapper.getDefaultVersion(apiDefinition.getProjectId()); // 清除所有最新标识 @@ -816,7 +841,7 @@ public class ApiDefinitionService { apiDefinitionDocDTO.setDocTitle(Translator.get(ALL_API)); } else { ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(first.getModuleId()); - if (StringUtils.isNotBlank(apiDefinitionModule.getName())) { + if (apiDefinitionModule != null && StringUtils.isNotBlank(apiDefinitionModule.getName())) { apiDefinitionDocDTO.setDocTitle(apiDefinitionModule.getName()); } else { throw new MSException(API_DEFINITION_MODULE_NOT_EXIST); 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 9dedfb56b8..526c5af3ed 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 @@ -1,16 +1,15 @@ package io.metersphere.api.controller; +import io.metersphere.api.constants.ApiDefinitionDocType; import io.metersphere.api.constants.ApiDefinitionStatus; import io.metersphere.api.controller.result.ApiResultCode; import io.metersphere.api.domain.*; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.http.MsHTTPElement; -import io.metersphere.api.constants.ApiDefinitionDocType; import io.metersphere.api.mapper.*; import io.metersphere.api.model.CheckLogModel; import io.metersphere.api.service.ApiFileResourceService; import io.metersphere.api.utils.ApiDataUtils; -import io.metersphere.sdk.exception.MSException; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.project.dto.filemanagement.FileInfo; import io.metersphere.project.dto.filemanagement.request.FileUploadRequest; @@ -19,12 +18,13 @@ import io.metersphere.project.service.FileAssociationService; import io.metersphere.project.service.FileMetadataService; import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.file.FileCenter; +import io.metersphere.sdk.file.FileRequest; import io.metersphere.sdk.util.*; import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.dto.sdk.BaseCondition; -import io.metersphere.sdk.file.FileCenter; -import io.metersphere.sdk.file.FileRequest; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.utils.Pager; import jakarta.annotation.Resource; @@ -365,6 +365,7 @@ public class ApiDefinitionControllerTests extends BaseTest { // @@校验权限 request.setProjectId(DEFAULT_PROJECT_ID); request.setName("permission-st-6"); + request.setModuleId("module-st-6"); requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_UPDATE, UPDATE, request); } diff --git a/backend/services/api-test/src/test/resources/dml/init_api_definition.sql b/backend/services/api-test/src/test/resources/dml/init_api_definition.sql index 7de4e0ff86..2ec14ac4d4 100644 --- a/backend/services/api-test/src/test/resources/dml/init_api_definition.sql +++ b/backend/services/api-test/src/test/resources/dml/init_api_definition.sql @@ -2,7 +2,7 @@ DELETE FROM `api_definition` WHERE `id` in ('1001','1002','1003','1004','1005','1006'); INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1001', 'test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test', 'HTTP', 'POST', '/api/admin/1', 'Prepare', 1001, '[\"test3\",\"te\"]', 1, '100001100001', 'root', b'1', '100570499574136985', '1001', NULL, 1699500298164, 'admin', 1699500298162, 'admin', NULL, NULL, b'0'); -INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1002', 'test-2', 'HTTP', 'GET', '/api/admin/2', 'Underway', 1002, null, 1, '100001100001', '10001', b'1', '1005704995741369851', '1002', NULL, 1699500298165, 'admin', 1699500298163, 'admin', NULL, NULL, b'0'); +INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1002', 'test-2', 'HTTP', 'GET', '/api/admin/2', 'Underway', 1002, null, 1, '100001100001', '1001001', b'1', '1005704995741369851', '1002', NULL, 1699500298165, 'admin', 1699500298163, 'admin', NULL, NULL, b'0'); INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1003', 'test-3', 'HTTP', 'POST', '/api/admin/3', 'Completed', 1003, '[\"test3\",\"te\"]', 1, '100001100001', '10001', b'1', '100570499574136985', '1002', NULL, 1699500298166, 'admin', 1699500298164, 'admin', NULL, NULL, b'0'); INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1004', 'test-4', 'HTTP', 'GET', '/api/admin/4', 'Prepare', 1004, '[\"test4\",\"te\"]', 1, '100001100001', '10001', b'1', '100570499574136985', '1004', NULL, 1699500298167, 'admin', 1699500298165, 'admin', NULL, NULL, b'0'); INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1005', 'test-65', 'HTTP', 'POST', '/api/admin/5', 'Underway', 1005, '[\"test5\",\"te\"]', 1, '100001100001', '10001', b'0', '100570499574136985', '1004', NULL, 1699500298168, 'admin', 1699500298166, 'admin', NULL, NULL, b'0'); @@ -32,8 +32,9 @@ INSERT INTO `api_report` (`id`, `name`, `resource_id`, `create_time`, `update_ti INSERT INTO `api_report` (`id`, `name`, `resource_id`, `create_time`, `update_time`, `create_user`, `update_user`, `deleted`, `status`, `start_time`, `end_time`, `run_mode`, `pool_id`, `trigger_mode`, `version_id`, `project_id`, `integrated_report_id`, `integrated`) VALUES ('10007', '报告004', 'resource_id_10001', 1680624405386, 1680624405386, 'admin', 'admin', b'0', 'SUCCESS', 1680624405386, 1680624405386, 'API', 'pol_id_100001', 'hand', NULL, '100001100001', 'NONE', b'0'); INSERT INTO `api_report` (`id`, `name`, `resource_id`, `create_time`, `update_time`, `create_user`, `update_user`, `deleted`, `status`, `start_time`, `end_time`, `run_mode`, `pool_id`, `trigger_mode`, `version_id`, `project_id`, `integrated_report_id`, `integrated`) VALUES ('10008', '报告005', 'resource_id_10001', 1680624405386, 1680624405386, 'admin', 'admin', b'0', 'ERROR', 1680624405386, 1680624405386, 'API', 'pol_id_100001', 'hand', NULL, '100001100001', 'NONE', b'0'); -DELETE FROM `api_definition_module` WHERE `id` = '10001'; +DELETE FROM `api_definition_module` WHERE `id` in ('10001', 'case-moduleId'); INSERT INTO `api_definition_module` (`id`, `name`, `protocol`, `parent_id`, `project_id`, `pos`, `create_time`, `update_time`, `update_user`, `create_user`) VALUES ('10001', 'module1', 'HTTP', 'NONE', '100001100001', 10, 0, 0, 'admin', 'admin'); +INSERT INTO `api_definition_module` (`id`, `name`, `protocol`, `parent_id`, `project_id`, `pos`, `create_time`, `update_time`, `update_user`, `create_user`) VALUES ('case-moduleId', 'case-moduleId', 'HTTP', 'NONE', '100001100001', 10, 0, 0, 'admin', 'admin'); DELETE FROM `api_definition_blob` WHERE `id` in ('1001','1002','1003','1004','1005','1006'); INSERT INTO `api_definition_blob` (`id`, `request`, `response`) VALUES ('1001', 0x504B03041400080808004A547857000000000000000000000000030000007A6970CD92B14EC3301040FFE5E6A8296B36842A180055281B62709D4B6D61FBDCF3991055FD779C840C949185C5A77BB6CFEF6C9F21921B3D7134563F2B8FD0C0537A68DBFDCEA1C720504158F01C2AC0A00EAEE4BD72092BD0C6BA8E314013B27315647665AD11894D5D0FC3B0114CB2D1E4CBD6A8C494C97A4225F52886BA02EE776D490FD48D6B1583AA434ED0BC9EE11D0B9EC70A3E94CB93CB127FC9749834DB28968ACF9CC1E5AD029ECEFB7BA9A90156BE1D23AE9E8CA76C194B13C2B9ECF2363C62384E6D2E0BBCFAFC093068EAD66326B953461EFFAD1D8941BEA3D0DB233467D014026A69AD47CAE54E6FB6DBED7CBF9142C22BAC91C5F6562BC15B6755794D5073ACA027E76878C1AEE86949ABA0CA4257F03253B32A2C9A9171CFA431259A3EC937A424D7F4F205504B070852639A522B010000E0020000504B010214001400080808004A54785752639A522B010000E00200000300000000000000000000000000000000007A6970504B05060000000001000100310000005C0100000000, 0x504B03041400080808004A547857000000000000000000000000030000007A6970A58F414BC4301085FFCB9C03B69EA4B74559141465295E9622B3CD743798263199C82EA5FFDD49EB82772F19DECB9BEF25FB098C86A656C09740D0C063DBBE81028763513B4AC1BB44B55827424D3141B39FE0932E725B4E05DF6873C9AE5301393C583106B48914684A7D34818D77122A0AE64EC1C16B414CCB6CD7EAEDEBEEE5E361D36E0432443FBE17E03FFB98124B2660C4F1B786E95CAC485FD94492CF73CCB2371AF74CEEC82768EE44E1F9AAEA4A41EF1D93E33F849B60D1B8A5BEF79AAE94C1587A12A6CBD6CEDDAC2031724EF74B046EAB0ACA0B07CC96B7168FEBDADCFD00504B0708FF595995E400000087010000504B010214001400080808004A547857FF595995E4000000870100000300000000000000000000000000000000007A6970504B0506000000000100010031000000150100000000);