From ae5cdab4b870c994b1f325d6409eecf3727b600f Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Fri, 27 Oct 2023 11:22:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=BC=80=E5=90=AF=E9=A1=B9=E7=9B=AE=E6=A8=A1=E6=9D=BF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProjectTemplateController.java | 12 +++++++-- .../service/ProjectTemplateService.java | 20 ++++++++++++--- .../ProjectTemplateControllerTests.java | 24 +++++++++++++++++- .../OrganizationTemplateController.java | 7 +++--- .../service/OrganizationTemplateService.java | 18 +++++++++++++ .../OrganizationTemplateControllerTests.java | 25 +++++++++---------- 6 files changed, 83 insertions(+), 23 deletions(-) diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTemplateController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTemplateController.java index bd9f579538..5dfb88019c 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTemplateController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTemplateController.java @@ -22,6 +22,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; /** * @author : jianxing @@ -82,10 +83,17 @@ public class ProjectTemplateController { } @GetMapping("/set-default/{projectId}/{id}") - @Operation(summary = "设置模板模板") - @RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_UPDATE) + @Operation(summary = "设置默认模板") + @RequiresPermissions(PermissionConstants.PROJECT_TEMPLATE_UPDATE) @Log(type = OperationLogType.UPDATE, expression = "#msClass.setDefaultTemplateLog(#id)", msClass = ProjectTemplateLogService.class) public void setDefaultTemplate(@PathVariable String projectId, @PathVariable String id) { projectTemplateservice.setDefaultTemplate(projectId, id); } + + @GetMapping("/enable/config/{projectId}") + @Operation(summary = "是否启用组织模版") + @RequiresPermissions(PermissionConstants.PROJECT_TEMPLATE_READ) + public Map getProjectTemplateEnableConfig(@PathVariable String projectId) { + return projectTemplateservice.getProjectTemplateEnableConfig(projectId); + } } \ No newline at end of file diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectTemplateService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectTemplateService.java index 0ee6da5186..ced2b3407d 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectTemplateService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectTemplateService.java @@ -29,10 +29,7 @@ import org.pf4j.PluginWrapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Comparator; -import java.util.List; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -369,4 +366,19 @@ public class ProjectTemplateService extends BaseTemplateService { throw new MSException(PROJECT_TEMPLATE_PERMISSION); } } + + /** + * 一个接口返回各个模板是否启用项目模板 + * @param projectId + * @return + */ + public Map getProjectTemplateEnableConfig(String projectId) { + ProjectService.checkResourceExist(projectId); + ProjectDTO project = projectService.getProjectById(projectId); + HashMap templateEnableConfig = new HashMap<>(); + Arrays.stream(TemplateScene.values()) + .forEach(scene -> + templateEnableConfig.put(scene.name(), !isOrganizationTemplateEnable(project.getOrganizationId(), scene.name()))); + return templateEnableConfig; + } } \ No newline at end of file diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTemplateControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTemplateControllerTests.java index c041a3b5a8..92bb1fe817 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTemplateControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTemplateControllerTests.java @@ -56,6 +56,7 @@ public class ProjectTemplateControllerTests extends BaseTest { private static final String BASE_PATH = "/project/template/"; private static final String LIST = "list/{0}/{1}"; private static final String SET_DEFAULT = "set-default/{0}/{1}"; + protected static final String PROJECT_TEMPLATE_ENABLE_CONFIG = "enable/config/{0}"; @Resource private TemplateMapper templateMapper; @@ -382,7 +383,7 @@ public class ProjectTemplateControllerTests extends BaseTest { // @@校验日志 checkLog(addTemplate.getId(), OperationLogType.UPDATE); // @@校验权限 - requestGetPermissionTest(PermissionConstants.ORGANIZATION_TEMPLATE_UPDATE, SET_DEFAULT, DEFAULT_PROJECT_ID, addTemplate.getId()); + requestGetPermissionTest(PermissionConstants.PROJECT_TEMPLATE_UPDATE, SET_DEFAULT, DEFAULT_PROJECT_ID, addTemplate.getId()); } @Test @@ -418,6 +419,27 @@ public class ProjectTemplateControllerTests extends BaseTest { requestGetPermissionTest(PermissionConstants.PROJECT_TEMPLATE_DELETE, DEFAULT_DELETE, addTemplate.getId()); } + @Test + @Order(7) + public void getProjectTemplateEnableConfig() throws Exception { + changeOrgTemplateEnable(true); + // @@请求成功 + MvcResult mvcResult = this.requestGetWithOkAndReturn(PROJECT_TEMPLATE_ENABLE_CONFIG, DEFAULT_PROJECT_ID); + Map resultData = getResultData(mvcResult, Map.class); + Assertions.assertEquals(resultData.size(), TemplateScene.values().length); + Assertions.assertFalse((Boolean) resultData.get(TemplateScene.FUNCTIONAL.name())); + changeOrgTemplateEnable(false); + mvcResult = this.requestGetWithOkAndReturn(PROJECT_TEMPLATE_ENABLE_CONFIG, DEFAULT_PROJECT_ID); + Assertions.assertTrue((Boolean) getResultData(mvcResult, Map.class).get(TemplateScene.FUNCTIONAL.name())); + changeOrgTemplateEnable(true); + + // @@校验 NOT_FOUND 异常 + assertErrorCode(this.requestGet(PROJECT_TEMPLATE_ENABLE_CONFIG,"1111"), NOT_FOUND); + + // @@校验权限 + requestGetPermissionTest(PermissionConstants.PROJECT_TEMPLATE_READ, PROJECT_TEMPLATE_ENABLE_CONFIG, DEFAULT_PROJECT_ID); + } + private void assertSetDefaultTemplate(Template template) { String defaultTemplateId = projectTemplateService.getDefaultTemplateId(template.getScopeId(), template.getScene()); Assertions.assertEquals(defaultTemplateId, template.getId()); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTemplateController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTemplateController.java index a136b4cdb5..1aea8b91b8 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTemplateController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTemplateController.java @@ -21,6 +21,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; /** * @author : jianxing @@ -88,10 +89,10 @@ public class OrganizationTemplateController { organizationTemplateService.disableOrganizationTemplate(organizationId, scene); } - @GetMapping("/is-enable/{organizationId}/{scene}") + @GetMapping("/enable/config/{organizationId}") @Operation(summary = "是否启用组织模版") @RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_READ) - public boolean isOrganizationTemplateEnable(@PathVariable String organizationId, @PathVariable String scene) { - return organizationTemplateService.isOrganizationTemplateEnable(organizationId, scene); + public Map getOrganizationTemplateEnableConfig(@PathVariable String organizationId) { + return organizationTemplateService.getOrganizationTemplateEnableConfig(organizationId); } } \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationTemplateService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationTemplateService.java index f21983c1b6..176c43a289 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationTemplateService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationTemplateService.java @@ -1,5 +1,6 @@ package io.metersphere.system.service; +import io.metersphere.sdk.constants.TemplateScene; import io.metersphere.sdk.constants.TemplateScopeType; import io.metersphere.system.dto.sdk.TemplateDTO; import io.metersphere.system.dto.sdk.request.TemplateCustomFieldRequest; @@ -17,7 +18,10 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static io.metersphere.system.controller.result.SystemResultCode.ORGANIZATION_TEMPLATE_PERMISSION; @@ -175,4 +179,18 @@ public class OrganizationTemplateService extends BaseTemplateService { baseOrganizationParameterService.add(organizationParameter); } } + + /** + * 一个接口返回各个模板是否启用组织模板 + * @param organizationId + * @return + */ + public Map getOrganizationTemplateEnableConfig(String organizationId) { + OrganizationService.checkResourceExist(organizationId); + HashMap templateEnableConfig = new HashMap<>(); + Arrays.stream(TemplateScene.values()) + .forEach(scene -> + templateEnableConfig.put(scene.name(), isOrganizationTemplateEnable(organizationId, scene.name()))); + return templateEnableConfig; + } } \ No newline at end of file diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationTemplateControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationTemplateControllerTests.java index aa0a93408e..e238fce96d 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationTemplateControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationTemplateControllerTests.java @@ -45,7 +45,7 @@ public class OrganizationTemplateControllerTests extends BaseTest { private static final String BASE_PATH = "/organization/template/"; private static final String LIST = "list/{0}/{1}"; private static final String DISABLE_ORG_TEMPLATE = "disable/{0}/{1}"; - protected static final String IS_ORGANIZATION_TEMPLATE_ENABLE = "is-enable/{0}/{1}"; + protected static final String ORGANIZATION_TEMPLATE_ENABLE_CONFIG = "enable/config/{0}"; @Resource private TemplateMapper templateMapper; @@ -159,12 +159,6 @@ public class OrganizationTemplateControllerTests extends BaseTest { }); } - private List