From b0974477c2f6fbb3ea14f21343157fac7348cc6b Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Thu, 17 Oct 2024 17:40:00 +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=E5=88=86=E4=BA=AB=E9=87=8D=E5=90=8D=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/i18n/api.properties | 2 +- .../main/resources/i18n/api_en_US.properties | 3 ++- .../main/resources/i18n/api_zh_CN.properties | 3 ++- .../main/resources/i18n/api_zh_TW.properties | 3 ++- .../definition/ApiDocShareService.java | 20 +++++++++++++++++++ .../ApiDocShareControllerTests.java | 9 +++++++-- 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/backend/framework/sdk/src/main/resources/i18n/api.properties b/backend/framework/sdk/src/main/resources/i18n/api.properties index dc8bbe8d55..98e7ceb131 100644 --- a/backend/framework/sdk/src/main/resources/i18n/api.properties +++ b/backend/framework/sdk/src/main/resources/i18n/api.properties @@ -466,4 +466,4 @@ api_doc_share.create_user.not_blank=创建人不能为空 api_doc_share.create_user.length_range=创建人长度必须在{min}和{max}之间 api_doc_share.project_id.not_blank=项目ID不能为空 api_doc_share.project_id.length_range=项目ID长度必须在{min}和{max}之间 - +api_doc_share.name_duplicate=分享名称重复 diff --git a/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties index e3bb4370c5..4787e68549 100644 --- a/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties @@ -476,4 +476,5 @@ api_doc_share.api_range.length_range=apiRange length must be between {min} and { api_doc_share.create_user.not_blank=createUser cannot be empty api_doc_share.create_user.length_range=createUser length must be between {min} and {max} api_doc_share.project_id.not_blank=projectId cannot be empty -api_doc_share.project_id.length_range=projectId length must be between {min} and {max} \ No newline at end of file +api_doc_share.project_id.length_range=projectId length must be between {min} and {max} +api_doc_share.name_duplicate=Share name duplicate \ No newline at end of file diff --git a/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties index 41a2b8f945..149442e4a1 100644 --- a/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties @@ -444,4 +444,5 @@ api_doc_share.api_range.length_range=接口范围长度必须在{min}和{max}之 api_doc_share.create_user.not_blank=创建人不能为空 api_doc_share.create_user.length_range=创建人长度必须在{min}和{max}之间 api_doc_share.project_id.not_blank=项目ID不能为空 -api_doc_share.project_id.length_range=项目ID长度必须在{min}和{max}之间 \ No newline at end of file +api_doc_share.project_id.length_range=项目ID长度必须在{min}和{max}之间 +api_doc_share.name_duplicate=分享名称重复 \ No newline at end of file diff --git a/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties index 06eed891fb..29d17994e0 100644 --- a/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties @@ -444,4 +444,5 @@ api_doc_share.api_range.length_range=接口范围長度必須在{min}和{max}之 api_doc_share.create_user.not_blank=创建人不能為空 api_doc_share.create_user.length_range=创建人長度必須在{min}和{max}之间 api_doc_share.project_id.not_blank=项目ID不能為空 -api_doc_share.project_id.length_range=项目ID長度必須在{min}和{max}之间 \ No newline at end of file +api_doc_share.project_id.length_range=项目ID長度必須在{min}和{max}之间 +api_doc_share.name_duplicate=分享名稱重複 \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDocShareService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDocShareService.java index 2f873939dd..f921d3422a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDocShareService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDocShareService.java @@ -1,6 +1,7 @@ package io.metersphere.api.service.definition; import io.metersphere.api.domain.ApiDocShare; +import io.metersphere.api.domain.ApiDocShareExample; import io.metersphere.api.dto.definition.ApiDocShareDTO; import io.metersphere.api.dto.definition.ApiDocShareDetail; import io.metersphere.api.dto.definition.request.*; @@ -63,6 +64,7 @@ public class ApiDocShareService { * @return 分享 */ public ApiDocShare create(ApiDocShareEditRequest request, String currentUser) { + checkDuplicateName(request); ApiDocShare docShare = new ApiDocShare(); BeanUtils.copyBean(docShare, request); docShare.setId(IDGenerator.nextStr()); @@ -79,6 +81,7 @@ public class ApiDocShareService { */ public ApiDocShare update(ApiDocShareEditRequest request) { checkExit(request.getId()); + checkDuplicateName(request); ApiDocShare docShare = new ApiDocShare(); BeanUtils.copyBean(docShare, request); apiDocShareMapper.updateByPrimaryKeySelective(docShare); @@ -295,6 +298,23 @@ public class ApiDocShareService { return docShare; } + /** + * 检查分享名称是否重名 + * @param request 请求参数 + */ + private void checkDuplicateName(ApiDocShareEditRequest request) { + ApiDocShareExample example = new ApiDocShareExample(); + ApiDocShareExample.Criteria criteria = example.createCriteria(); + criteria.andNameEqualTo(request.getName()); + criteria.andProjectIdEqualTo(request.getProjectId()); + if (StringUtils.isNotBlank(request.getId())) { + criteria.andIdNotEqualTo(request.getId()); + } + if (apiDocShareMapper.countByExample(example) > 0) { + throw new MSException(Translator.get("api_doc_share.name_duplicate")); + } + } + /** * 组合左侧模块树的查询条件 * @param name 条件字段名 diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDocShareControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDocShareControllerTests.java index 04e9081001..3f7b5af565 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDocShareControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDocShareControllerTests.java @@ -92,17 +92,19 @@ public class ApiDocShareControllerTests extends BaseTest { @Test public void page() throws Exception { ApiDocShareEditRequest request = new ApiDocShareEditRequest(); - request.setName("share-1"); + request.setName("share-2"); request.setProjectId(DEFAULT_PROJECT_ID); request.setApiRange("ALL"); request.setIsPrivate(false); request.setAllowExport(false); - this.requestPostWithOk(ADD, request); + this.requestPost(ADD, request); + request.setName("share-3"); request.setInvalidTime(1); request.setInvalidUnit("HOUR"); request.setApiRange("MODULE"); request.setRangeMatchVal("module-1"); this.requestPostWithOk(ADD, request); + request.setName("share-4"); request.setApiRange("PATH"); request.setRangeMatchSymbol("EQUALS"); request.setRangeMatchVal("path-1"); @@ -115,6 +117,7 @@ public class ApiDocShareControllerTests extends BaseTest { moduleRequest.setProjectId(DEFAULT_PROJECT_ID); moduleRequest.setProtocols(List.of("HTTP", "SPX", "Redis", "MongoDB")); this.requestPostWithOk(MODULE_COUNT, moduleRequest); + request.setName("share-5"); request.setRangeMatchSymbol("CONTAINS"); MvcResult mvcResult1 = this.requestPostWithOk(ADD, request).andReturn(); String returnData1 = mvcResult1.getResponse().getContentAsString(StandardCharsets.UTF_8); @@ -122,6 +125,7 @@ public class ApiDocShareControllerTests extends BaseTest { ApiDocShare docShare1 = JSON.parseObject(JSON.toJSONString(resultHolder1.getData()), ApiDocShare.class); moduleRequest.setShareId(docShare1.getId()); this.requestPostWithOk(MODULE_COUNT, moduleRequest); + request.setName("share-6"); request.setApiRange("TAG"); request.setRangeMatchVal("tag-1,tag-2"); MvcResult mvcResult2 = this.requestPostWithOk(ADD, request).andReturn(); @@ -130,6 +134,7 @@ public class ApiDocShareControllerTests extends BaseTest { ApiDocShare docShare2 = JSON.parseObject(JSON.toJSONString(resultHolder2.getData()), ApiDocShare.class); moduleRequest.setShareId(docShare2.getId()); this.requestPostWithOk(MODULE_COUNT, moduleRequest); + this.requestPost(ADD, request).andExpect(status().is5xxServerError()); ApiDocSharePageRequest pageRequest = new ApiDocSharePageRequest(); pageRequest.setProjectId(DEFAULT_PROJECT_ID); pageRequest.setCurrent(1);