refactor(项目设置): 优化查询是否开启项目模板接口

This commit is contained in:
AgAngle 2023-10-27 11:22:15 +08:00 committed by 刘瑞斌
parent c45bca97fc
commit ae5cdab4b8
6 changed files with 83 additions and 23 deletions

View File

@ -22,6 +22,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author : jianxing * @author : jianxing
@ -82,10 +83,17 @@ public class ProjectTemplateController {
} }
@GetMapping("/set-default/{projectId}/{id}") @GetMapping("/set-default/{projectId}/{id}")
@Operation(summary = "设置模板模板") @Operation(summary = "设置默认模板")
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_UPDATE) @RequiresPermissions(PermissionConstants.PROJECT_TEMPLATE_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.setDefaultTemplateLog(#id)", msClass = ProjectTemplateLogService.class) @Log(type = OperationLogType.UPDATE, expression = "#msClass.setDefaultTemplateLog(#id)", msClass = ProjectTemplateLogService.class)
public void setDefaultTemplate(@PathVariable String projectId, @PathVariable String id) { public void setDefaultTemplate(@PathVariable String projectId, @PathVariable String id) {
projectTemplateservice.setDefaultTemplate(projectId, id); projectTemplateservice.setDefaultTemplate(projectId, id);
} }
@GetMapping("/enable/config/{projectId}")
@Operation(summary = "是否启用组织模版")
@RequiresPermissions(PermissionConstants.PROJECT_TEMPLATE_READ)
public Map<String, Boolean> getProjectTemplateEnableConfig(@PathVariable String projectId) {
return projectTemplateservice.getProjectTemplateEnableConfig(projectId);
}
} }

View File

@ -29,10 +29,7 @@ import org.pf4j.PluginWrapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Comparator; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -369,4 +366,19 @@ public class ProjectTemplateService extends BaseTemplateService {
throw new MSException(PROJECT_TEMPLATE_PERMISSION); throw new MSException(PROJECT_TEMPLATE_PERMISSION);
} }
} }
/**
* 一个接口返回各个模板是否启用项目模板
* @param projectId
* @return
*/
public Map<String, Boolean> getProjectTemplateEnableConfig(String projectId) {
ProjectService.checkResourceExist(projectId);
ProjectDTO project = projectService.getProjectById(projectId);
HashMap<String, Boolean> templateEnableConfig = new HashMap<>();
Arrays.stream(TemplateScene.values())
.forEach(scene ->
templateEnableConfig.put(scene.name(), !isOrganizationTemplateEnable(project.getOrganizationId(), scene.name())));
return templateEnableConfig;
}
} }

View File

@ -56,6 +56,7 @@ public class ProjectTemplateControllerTests extends BaseTest {
private static final String BASE_PATH = "/project/template/"; private static final String BASE_PATH = "/project/template/";
private static final String LIST = "list/{0}/{1}"; private static final String LIST = "list/{0}/{1}";
private static final String SET_DEFAULT = "set-default/{0}/{1}"; private static final String SET_DEFAULT = "set-default/{0}/{1}";
protected static final String PROJECT_TEMPLATE_ENABLE_CONFIG = "enable/config/{0}";
@Resource @Resource
private TemplateMapper templateMapper; private TemplateMapper templateMapper;
@ -382,7 +383,7 @@ public class ProjectTemplateControllerTests extends BaseTest {
// @@校验日志 // @@校验日志
checkLog(addTemplate.getId(), OperationLogType.UPDATE); 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 @Test
@ -418,6 +419,27 @@ public class ProjectTemplateControllerTests extends BaseTest {
requestGetPermissionTest(PermissionConstants.PROJECT_TEMPLATE_DELETE, DEFAULT_DELETE, addTemplate.getId()); 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) { private void assertSetDefaultTemplate(Template template) {
String defaultTemplateId = projectTemplateService.getDefaultTemplateId(template.getScopeId(), template.getScene()); String defaultTemplateId = projectTemplateService.getDefaultTemplateId(template.getScopeId(), template.getScene());
Assertions.assertEquals(defaultTemplateId, template.getId()); Assertions.assertEquals(defaultTemplateId, template.getId());

View File

@ -21,6 +21,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author : jianxing * @author : jianxing
@ -88,10 +89,10 @@ public class OrganizationTemplateController {
organizationTemplateService.disableOrganizationTemplate(organizationId, scene); organizationTemplateService.disableOrganizationTemplate(organizationId, scene);
} }
@GetMapping("/is-enable/{organizationId}/{scene}") @GetMapping("/enable/config/{organizationId}")
@Operation(summary = "是否启用组织模版") @Operation(summary = "是否启用组织模版")
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_READ) @RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_READ)
public boolean isOrganizationTemplateEnable(@PathVariable String organizationId, @PathVariable String scene) { public Map<String, Boolean> getOrganizationTemplateEnableConfig(@PathVariable String organizationId) {
return organizationTemplateService.isOrganizationTemplateEnable(organizationId, scene); return organizationTemplateService.getOrganizationTemplateEnableConfig(organizationId);
} }
} }

View File

@ -1,5 +1,6 @@
package io.metersphere.system.service; package io.metersphere.system.service;
import io.metersphere.sdk.constants.TemplateScene;
import io.metersphere.sdk.constants.TemplateScopeType; import io.metersphere.sdk.constants.TemplateScopeType;
import io.metersphere.system.dto.sdk.TemplateDTO; import io.metersphere.system.dto.sdk.TemplateDTO;
import io.metersphere.system.dto.sdk.request.TemplateCustomFieldRequest; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import static io.metersphere.system.controller.result.SystemResultCode.ORGANIZATION_TEMPLATE_PERMISSION; import static io.metersphere.system.controller.result.SystemResultCode.ORGANIZATION_TEMPLATE_PERMISSION;
@ -175,4 +179,18 @@ public class OrganizationTemplateService extends BaseTemplateService {
baseOrganizationParameterService.add(organizationParameter); baseOrganizationParameterService.add(organizationParameter);
} }
} }
/**
* 一个接口返回各个模板是否启用组织模板
* @param organizationId
* @return
*/
public Map<String, Boolean> getOrganizationTemplateEnableConfig(String organizationId) {
OrganizationService.checkResourceExist(organizationId);
HashMap<String, Boolean> templateEnableConfig = new HashMap<>();
Arrays.stream(TemplateScene.values())
.forEach(scene ->
templateEnableConfig.put(scene.name(), isOrganizationTemplateEnable(organizationId, scene.name())));
return templateEnableConfig;
}
} }

View File

@ -45,7 +45,7 @@ public class OrganizationTemplateControllerTests extends BaseTest {
private static final String BASE_PATH = "/organization/template/"; private static final String BASE_PATH = "/organization/template/";
private static final String LIST = "list/{0}/{1}"; private static final String LIST = "list/{0}/{1}";
private static final String DISABLE_ORG_TEMPLATE = "disable/{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 @Resource
private TemplateMapper templateMapper; private TemplateMapper templateMapper;
@ -159,12 +159,6 @@ public class OrganizationTemplateControllerTests extends BaseTest {
}); });
} }
private List<Template> getTemplateByScopeId(String scopeId) {
TemplateExample example = new TemplateExample();
example.createCriteria().andScopeIdEqualTo(scopeId);
return templateMapper.selectByExample(example);
}
private void changeOrgTemplateEnable(boolean enable) { private void changeOrgTemplateEnable(boolean enable) {
if (enable) { if (enable) {
organizationParameterMapper.deleteByPrimaryKey(DEFAULT_ORGANIZATION_ID, OrganizationParameterConstants.ORGANIZATION_FUNCTIONAL_TEMPLATE_ENABLE_KEY); organizationParameterMapper.deleteByPrimaryKey(DEFAULT_ORGANIZATION_ID, OrganizationParameterConstants.ORGANIZATION_FUNCTIONAL_TEMPLATE_ENABLE_KEY);
@ -386,17 +380,22 @@ public class OrganizationTemplateControllerTests extends BaseTest {
@Test @Test
@Order(8) @Order(8)
public void isOrganizationTemplateEnable() throws Exception { public void getOrganizationTemplateEnableConfig() throws Exception {
// @@请求成功 // @@请求成功
MvcResult mvcResult = this.requestGetWithOkAndReturn(IS_ORGANIZATION_TEMPLATE_ENABLE, DEFAULT_ORGANIZATION_ID, TemplateScene.FUNCTIONAL.name()); MvcResult mvcResult = this.requestGetWithOkAndReturn(ORGANIZATION_TEMPLATE_ENABLE_CONFIG, DEFAULT_ORGANIZATION_ID);
Assertions.assertTrue(getResultData(mvcResult, Boolean.class)); Map resultData = getResultData(mvcResult, Map.class);
Assertions.assertEquals(resultData.size(), TemplateScene.values().length);
Assertions.assertTrue((Boolean) resultData.get(TemplateScene.FUNCTIONAL.name()));
changeOrgTemplateEnable(false); changeOrgTemplateEnable(false);
mvcResult = this.requestGetWithOkAndReturn(IS_ORGANIZATION_TEMPLATE_ENABLE, DEFAULT_ORGANIZATION_ID, TemplateScene.FUNCTIONAL.name()); mvcResult = this.requestGetWithOkAndReturn(ORGANIZATION_TEMPLATE_ENABLE_CONFIG, DEFAULT_ORGANIZATION_ID);
Assertions.assertFalse(getResultData(mvcResult, Boolean.class)); Assertions.assertFalse((Boolean) getResultData(mvcResult, Map.class).get(TemplateScene.FUNCTIONAL.name()));
changeOrgTemplateEnable(true); changeOrgTemplateEnable(true);
// @@校验 NOT_FOUND 异常
assertErrorCode(this.requestGet(ORGANIZATION_TEMPLATE_ENABLE_CONFIG,"1111"), NOT_FOUND);
// @@校验权限 // @@校验权限
requestGetPermissionTest(PermissionConstants.ORGANIZATION_TEMPLATE_READ, IS_ORGANIZATION_TEMPLATE_ENABLE, DEFAULT_ORGANIZATION_ID, TemplateScene.FUNCTIONAL.name()); requestGetPermissionTest(PermissionConstants.ORGANIZATION_TEMPLATE_READ, ORGANIZATION_TEMPLATE_ENABLE_CONFIG, DEFAULT_ORGANIZATION_ID);
} }
/** /**