refactor(项目管理): 项目管理公共脚本模块调整完善

This commit is contained in:
lan-yonghui 2024-01-15 19:18:49 +08:00 committed by Craftsman
parent e6c918a8b6
commit 350e2cbbad
9 changed files with 45 additions and 42 deletions

View File

@ -36,7 +36,7 @@ public class CustomFunction implements Serializable {
@Schema(description = "脚本语言类型")
private String type;
@Schema(description = "脚本状态(进行中/已完成")
@Schema(description = "脚本状态(草稿/测试通过")
private String status;
@Schema(description = "创建时间")

View File

@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS custom_function
`tags` VARCHAR(1000) COMMENT '标签',
`description` VARCHAR(500) COMMENT '函数描述',
`type` VARCHAR(50) DEFAULT NULL COMMENT '脚本语言类型',
`status` VARCHAR(50) COMMENT '脚本状态(进行中/已完成' ,
`status` VARCHAR(50) COMMENT '脚本状态(草稿/测试通过' ,
`create_time` BIGINT COMMENT '创建时间' ,
`update_time` BIGINT COMMENT '更新时间' ,
`create_user` VARCHAR(50) COMMENT '创建人' ,

View File

@ -102,7 +102,7 @@ public class ApiDefinitionMockController {
}
@PostMapping("/upload/temp/file")
@Operation(summary = "上传接口定义所需的文件资源并返回文件ID")
@Operation(summary = "上传接口 Mock 所需的文件资源并返回文件ID")
@RequiresPermissions(logical = Logical.OR, value = {PermissionConstants.PROJECT_API_DEFINITION_MOCK_ADD, PermissionConstants.PROJECT_API_DEFINITION_MOCK_UPDATE})
public String uploadTempFile(@RequestParam("file") MultipartFile file) {
return apiDefinitionMockService.uploadTempFile(file);

View File

@ -27,6 +27,6 @@ public class CustomFunctionPageRequest extends BasePageRequest implements Serial
@Schema(description = "脚本语言类型")
private String type;
@Schema(description = "脚本状态(进行中/已完成")
@Schema(description = "脚本状态(草稿/测试通过")
private String status;
}

View File

@ -35,7 +35,7 @@ public class CustomFunctionRequest implements Serializable {
@Schema(description = "脚本语言类型")
private String type;
@Schema(description = "脚本状态(进行中/已完成")
@Schema(description = "脚本状态(草稿/测试通过")
private String status;
@Schema(description = "标签")

View File

@ -0,0 +1,11 @@
package io.metersphere.project.enums;
/**
* @author: LAN
* @date: 2024/1/12 11:03
* @version: 1.0
*/
public enum CustomFunctionStatus {
DRAFT,
PASSED
}

View File

@ -15,17 +15,6 @@
<include refid="queryWhereCondition"/>
</select>
<select id="getIdsByApiIds" resultType="java.lang.String">
select
m.id
from api_definition_mock m
where m.api_definition_id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
<sql id="queryWhereCondition">
<if test="request.keyword != null and request.keyword != ''">
and (

View File

@ -7,6 +7,7 @@ import io.metersphere.project.dto.customfunction.CustomFunctionDTO;
import io.metersphere.project.dto.customfunction.request.CustomFunctionPageRequest;
import io.metersphere.project.dto.customfunction.request.CustomFunctionRequest;
import io.metersphere.project.dto.customfunction.request.CustomFunctionUpdateRequest;
import io.metersphere.project.enums.CustomFunctionStatus;
import io.metersphere.project.enums.result.ProjectResultCode;
import io.metersphere.project.mapper.CustomFunctionBlobMapper;
import io.metersphere.project.mapper.CustomFunctionMapper;
@ -43,7 +44,15 @@ public class CustomFunctionService {
ExtCustomFunctionMapper extCustomFunctionMapper;
public List<CustomFunctionDTO> getPage(CustomFunctionPageRequest request) {
return extCustomFunctionMapper.list(request);
List<CustomFunctionDTO> list = extCustomFunctionMapper.list(request);
if (!CollectionUtils.isEmpty(list)) {
processCustomFunction(list);
}
return list;
}
private void processCustomFunction(List<CustomFunctionDTO> list) {
list.forEach(item -> handleCustomFunctionBlob(item.getId(), item));
}
public CustomFunctionDTO get(String id) {
@ -57,12 +66,16 @@ public class CustomFunctionService {
public void handleCustomFunctionBlob(String id, CustomFunctionDTO customFunctionDTO) {
Optional<CustomFunctionBlob> customFunctionBlobOptional = Optional.ofNullable(customFunctionBlobMapper.selectByPrimaryKey(id));
customFunctionBlobOptional.ifPresent(blob -> {
customFunctionDTO.setParams(new String(blob.getParams(), StandardCharsets.UTF_8));
customFunctionDTO.setScript(new String(blob.getScript(), StandardCharsets.UTF_8));
customFunctionDTO.setResult(new String(blob.getResult(), StandardCharsets.UTF_8));
customFunctionDTO.setParams(toStringOrDefault(blob.getParams()));
customFunctionDTO.setScript(toStringOrDefault(blob.getScript()));
customFunctionDTO.setResult(toStringOrDefault(blob.getResult()));
});
}
private String toStringOrDefault(byte[] bytes) {
return (bytes != null) ? new String(bytes, StandardCharsets.UTF_8) : null;
}
public CustomFunction add(CustomFunctionRequest request, String userId) {
ProjectService.checkResourceExist(request.getProjectId());
@ -70,6 +83,7 @@ public class CustomFunctionService {
BeanUtils.copyBean(customFunction, request);
checkAddExist(customFunction);
customFunction.setId(IDGenerator.nextStr());
customFunction.setStatus(request.getStatus() != null ? request.getStatus() : CustomFunctionStatus.DRAFT.toString());
customFunction.setCreateTime(System.currentTimeMillis());
customFunction.setUpdateTime(System.currentTimeMillis());
customFunction.setCreateUser(userId);
@ -78,17 +92,7 @@ public class CustomFunctionService {
customFunction.setTags(request.getTags());
}
customFunctionMapper.insertSelective(customFunction);
CustomFunctionBlob customFunctionBlob = new CustomFunctionBlob();
customFunctionBlob.setId(customFunction.getId());
if(request.getParams() != null) {
customFunctionBlob.setParams(request.getParams().getBytes());
}
if(request.getScript() != null) {
customFunctionBlob.setScript(request.getScript().getBytes());
}
if(request.getResult() != null) {
customFunctionBlob.setResult(request.getResult().getBytes());
}
CustomFunctionBlob customFunctionBlob = createCustomFunctionBlob(customFunction, request.getParams(), request.getScript(), request.getResult());
customFunctionBlobMapper.insertSelective(customFunctionBlob);
return customFunction;
@ -106,18 +110,17 @@ public class CustomFunctionService {
customFunction.setTags(request.getTags());
}
customFunctionMapper.updateByPrimaryKeySelective(customFunction);
CustomFunctionBlob customFunctionBlob = createCustomFunctionBlob(customFunction, request.getParams(), request.getScript(), request.getResult());
customFunctionBlobMapper.updateByPrimaryKeySelective(customFunctionBlob);
}
private CustomFunctionBlob createCustomFunctionBlob(CustomFunction customFunction, String params, String script, String result) {
CustomFunctionBlob customFunctionBlob = new CustomFunctionBlob();
customFunctionBlob.setId(customFunction.getId());
if(request.getParams() != null) {
customFunctionBlob.setParams(request.getParams().getBytes());
}
if(request.getScript() != null) {
customFunctionBlob.setScript(request.getScript().getBytes());
}
if(request.getResult() != null) {
customFunctionBlob.setResult(request.getResult().getBytes());
}
customFunctionBlobMapper.updateByPrimaryKeySelective(customFunctionBlob);
customFunctionBlob.setParams(params != null ? params.getBytes() : null);
customFunctionBlob.setScript(script != null ? script.getBytes() : null);
customFunctionBlob.setResult(result != null ? result.getBytes() : null);
return customFunctionBlob;
}
public void updateStatus(CustomFunctionUpdateRequest request, String userId) {

View File

@ -316,7 +316,7 @@ public class CustomFunctionControllerTests extends BaseTest {
private void configureKeywordSearch(CustomFunctionPageRequest request) {
request.setKeyword("100");
request.setKeyword("test");
request.setSort(Map.of("status", "asc"));
}