refactor(项目管理): 优化获取环境列表接口

This commit is contained in:
wxg0103 2024-02-27 20:31:41 +08:00 committed by Craftsman
parent ef622cfc57
commit ec3ef79081
10 changed files with 123 additions and 14 deletions

View File

@ -130,4 +130,10 @@ public class EnvironmentController {
environmentService.editPos(request);
}
@GetMapping("/get-options/{projectId}")
@Operation(summary = "项目管理-环境-环境目录-列表")
@RequiresPermissions(PermissionConstants.PROJECT_ENVIRONMENT_READ)
public List<EnvironmentOptionsDTO> list(@PathVariable String projectId) {
return environmentService.listOption(projectId);
}
}

View File

@ -2,7 +2,7 @@ package io.metersphere.project.controller;
import io.metersphere.project.dto.environment.EnvironmentFilterRequest;
import io.metersphere.project.dto.environment.EnvironmentGroupInfo;
import io.metersphere.project.dto.environment.EnvironmentGroupDTO;
import io.metersphere.project.dto.environment.EnvironmentGroupRequest;
import io.metersphere.project.service.EnvironmentGroupLogService;
import io.metersphere.project.service.EnvironmentGroupService;
@ -68,7 +68,7 @@ public class EnvironmentGroupController {
@Operation(summary = "项目管理-环境组-详情")
@RequiresPermissions(PermissionConstants.PROJECT_ENVIRONMENT_READ)
@CheckOwner(resourceId = "#id", resourceType = "environment_group")
public List<EnvironmentGroupInfo> get(@PathVariable String id) {
public EnvironmentGroupDTO get(@PathVariable String id) {
return environmentGroupService.get(id);
}

View File

@ -0,0 +1,40 @@
package io.metersphere.project.dto.environment;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class EnvironmentGroupDTO implements Serializable {
private String id;
@Schema(description = "环境组名")
private String name;
@Schema(description = "所属项目id")
private String projectId;
@Schema(description = "环境组描述")
private String description;
@Schema(description = "创建人")
private String createUser;
@Schema(description = "修改人")
private String updateUser;
@Schema(description = "创建时间")
private Long createTime;
@Schema(description = "更新时间")
private Long updateTime;
@Schema(description = "自定义排序")
private Long pos;
@Schema(description = "环境组详情")
private List<EnvironmentGroupInfo> environmentGroupInfo;
}

View File

@ -12,8 +12,6 @@ public class EnvironmentGroupProjectDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "环境组id")
private String environmentGroupId;
@Schema(description = "环境id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank
private String environmentId;

View File

@ -0,0 +1,29 @@
package io.metersphere.project.dto.environment;
import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.sdk.domain.Environment;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
@Data
public class EnvironmentOptionsDTO extends Environment implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "ID")
private String id;
@Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String projectId;
@Schema(description = "环境名称")
private String name;
@Schema(description = "域名")
private List<HttpConfig> domain;
@Schema(description = "是否是mock环境")
private Boolean mock;
@Schema(description = "描述")
private String description;
}

View File

@ -37,6 +37,8 @@ public class HttpConfig implements Serializable {
private HttpConfigModuleMatchRule moduleMatchRule = new HttpConfigModuleMatchRule();
@Schema(description = "请求头")
private List<@Valid KeyValueEnableParam> headers = new ArrayList<>(0);
@Schema(description = "描述")
private String description;
public boolean isModuleMatchRule() {

View File

@ -13,12 +13,12 @@ import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.mapper.EnvironmentGroupMapper;
import io.metersphere.sdk.mapper.EnvironmentGroupRelationMapper;
import io.metersphere.sdk.mapper.EnvironmentMapper;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.PosRequest;
import io.metersphere.system.mapper.OrganizationMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.utils.ServiceUtils;
@ -29,7 +29,6 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -57,15 +56,13 @@ public class EnvironmentGroupService {
@Resource
private ExtProjectMapper extProjectMapper;
@Resource
private OrganizationMapper organizationMapper;
@Resource
private EnvironmentService environmentService;
public static final Long ORDER_STEP = 5000L;
public EnvironmentGroup add(EnvironmentGroupRequest request, String userId) {
EnvironmentGroup environmentGroup = new EnvironmentGroup();
BeanUtils.copyProperties(request, environmentGroup);
BeanUtils.copyBean(environmentGroup, request);
environmentGroup.setId(IDGenerator.nextStr());
this.checkEnvironmentGroup(environmentGroup);
@ -173,8 +170,9 @@ public class EnvironmentGroupService {
return extEnvironmentMapper.groupList(request.getKeyword(), request.getProjectId());
}
public List<EnvironmentGroupInfo> get(String groupId) {
checkEnvironmentGroup(groupId);
public EnvironmentGroupDTO get(String groupId) {
EnvironmentGroupDTO environmentGroupDTO = new EnvironmentGroupDTO();
EnvironmentGroup environmentGroup = checkEnvironmentGroup(groupId);
EnvironmentGroupRelationExample example = new EnvironmentGroupRelationExample();
example.createCriteria().andEnvironmentGroupIdEqualTo(groupId);
List<EnvironmentGroupRelation> relations = environmentGroupRelationMapper.selectByExample(example);
@ -201,7 +199,9 @@ public class EnvironmentGroupService {
}
result.add(dto);
});
return result;
BeanUtils.copyBean(environmentGroupDTO, environmentGroup);
environmentGroupDTO.setEnvironmentGroupInfo(result);
return environmentGroupDTO;
}
public List<OptionDTO> getProject(String userId, String organizationId) {

View File

@ -479,4 +479,29 @@ public class EnvironmentService {
example.createCriteria().andIdIn(envIds);
return environmentBlobMapper.selectByExampleWithBLOBs(example);
}
public List<EnvironmentOptionsDTO> listOption(String projectId) {
List<EnvironmentOptionsDTO> environmentOptions = new ArrayList<>();
EnvironmentExample environmentExample = new EnvironmentExample();
environmentExample.createCriteria().andProjectIdEqualTo(projectId);
List<Environment> environments = environmentMapper.selectByExample(environmentExample);
List<String> ids = environments.stream().map(Environment::getId).toList();
EnvironmentBlobExample environmentBlobExample = new EnvironmentBlobExample();
environmentBlobExample.createCriteria().andIdIn(ids);
List<EnvironmentBlob> environmentBlobs = environmentBlobMapper.selectByExampleWithBLOBs(environmentBlobExample);
Map<String, EnvironmentBlob> environmentBlobMap = environmentBlobs.stream().collect(Collectors.toMap(EnvironmentBlob::getId, Function.identity()));
environments.forEach(environment -> {
EnvironmentOptionsDTO environmentOptionsDTO = new EnvironmentOptionsDTO();
BeanUtils.copyBean(environmentOptionsDTO, environment);
EnvironmentBlob environmentBlob = environmentBlobMap.get(environment.getId());
if (environmentBlob != null) {
EnvironmentConfig environmentConfig = JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class);
if (environmentConfig != null && CollectionUtils.isNotEmpty(environmentConfig.getHttpConfig())) {
environmentOptionsDTO.setDomain(environmentConfig.getHttpConfig());
}
}
environmentOptions.add(environmentOptionsDTO);
});
return environmentOptions;
}
}

View File

@ -96,6 +96,7 @@ public class EnvironmentControllerTests extends BaseTest {
private static final String validate = prefix + "/database/validate";
private static final String getOptions = prefix + "/database/driver-options/";
private static final String SCRIPTS = prefix + "/scripts/{0}";
private static final String listOption = prefix + "/get-options/";
private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest();
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
private static String MOCKID;
@ -1200,4 +1201,12 @@ public class EnvironmentControllerTests extends BaseTest {
request.setCreateUser(ADMIN.name());
return pluginService.add(request, mockMultipartFile);
}
@Test
@Order(17)
public void testGetOptions() throws Exception {
MvcResult mvcResult = responseGet(listOption + DEFAULT_PROJECT_ID);
List<EnvironmentOptionsDTO> options = getResultDataArray(mvcResult, EnvironmentOptionsDTO.class);
Assertions.assertFalse(options.isEmpty());
}
}

View File

@ -208,7 +208,7 @@ public class EnvironmentGroupControllerTests extends BaseTest {
public void testGet() throws Exception {
//环境不存在
MvcResult mvcResult = this.responseGet(get + getEnvironmentGroup());
List<EnvironmentGroupInfo> response = parseObjectFromMvcResult(mvcResult, List.class);
EnvironmentGroupDTO response = parseObjectFromMvcResult(mvcResult, EnvironmentGroupDTO.class);
Assertions.assertNotNull(response);
}