refactor(接口测试): 优化接口调试的模块拖拽

This commit is contained in:
wxg0103 2023-11-01 14:04:34 +08:00 committed by Craftsman
parent 721f9115c1
commit a9cbfd37be
6 changed files with 45 additions and 96 deletions

View File

@ -1,9 +1,10 @@
package io.metersphere.api.mapper;
import io.metersphere.api.domain.ApiDebugModule;
import io.metersphere.api.dto.debug.ApiDebugRequest;
import io.metersphere.api.dto.debug.ApiTreeNode;
import io.metersphere.project.dto.ModuleCountDTO;
import io.metersphere.project.dto.NodeSortQueryParam;
import io.metersphere.system.dto.sdk.BaseModule;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import org.apache.ibatis.annotations.Param;
@ -22,13 +23,9 @@ public interface ExtApiDebugModuleMapper {
Long getMaxPosByParentId(String parentId);
List<String> selectIdsByProjectId(String projectId);
BaseModule selectBaseModuleById(String dragNodeId);
ApiDebugModule getLastModuleByParentId(String id);
ApiDebugModule getNextModuleInParentId(@Param("parentId") String parentId, @Param("pos") long pos);
ApiDebugModule getPreviousModuleInParentId(@Param("parentId") String parentId, @Param("pos") long pos);
BaseModule selectModuleByParentIdAndPosOperator(NodeSortQueryParam nodeSortQueryParam);
List<ApiTreeNode> selectApiDebugByProtocolAndUser(String protocol, String userId);

View File

@ -60,23 +60,6 @@
ORDER BY pos DESC
LIMIT 1
</select>
<select id="getNextModuleInParentId" resultType="io.metersphere.api.domain.ApiDebugModule">
SELECT *
from api_debug_module
WHERE parent_id = #{parentId}
AND pos &gt; #{pos}
order by pos asc
limit 1
</select>
<select id="getPreviousModuleInParentId" resultType="io.metersphere.api.domain.ApiDebugModule">
SELECT *
from api_debug_module
WHERE parent_id = #{parentId}
AND pos &lt; #{pos}
order by pos
desc
limit 1
</select>
<select id="selectApiDebugByProtocolAndUser" resultType="io.metersphere.api.dto.debug.ApiTreeNode">
SELECT id,
NAME,
@ -96,6 +79,30 @@
<include refid="debug_page_request"/>
GROUP BY f.module_id
</select>
<select id="selectBaseModuleById" resultType="io.metersphere.system.dto.sdk.BaseModule">
SELECT id, name, pos, project_Id, parent_id
FROM api_debug_module
WHERE id = #{0}
</select>
<select id="selectModuleByParentIdAndPosOperator"
parameterType="io.metersphere.project.dto.NodeSortQueryParam"
resultType="io.metersphere.system.dto.sdk.BaseModule">
SELECT id, name, pos, project_Id, parent_id
FROM api_debug_module
WHERE parent_id = #{parentId}
<if test="operator == 'moreThan'">
AND pos &gt; #{pos}
</if>
<if test="operator == 'lessThan'">
AND pos &lt; #{pos}
</if>
ORDER BY pos
<if test="operator == 'lessThan' or operator == 'latest'">
DESC
</if>
LIMIT 1
</select>
<sql id="debug_page_request">
<where>

View File

@ -15,7 +15,6 @@ import io.metersphere.project.service.ModuleTreeService;
import io.metersphere.sdk.constants.ModuleConstants;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.sdk.BaseModule;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.dto.sdk.request.NodeMoveRequest;
import io.metersphere.system.uid.IDGenerator;
@ -203,77 +202,23 @@ public class ApiDebugModuleService extends ModuleTreeService {
}
public void moveNode(NodeMoveRequest request, String currentUser) {
BaseModule module;
BaseModule parentModule;
BaseModule previousNode = null;
BaseModule nextNode = null;
ApiDebugModule dragNode = apiDebugModuleMapper.selectByPrimaryKey(request.getDragNodeId());
if (dragNode == null) {
throw new MSException(Translator.get(MODULE_NO_EXIST) + ": " + request.getDragNodeId());
} else {
module = new BaseModule(dragNode.getId(), dragNode.getName(), dragNode.getPos(), dragNode.getProjectId(), dragNode.getParentId());
}
if (StringUtils.equals(request.getDragNodeId(), request.getDropNodeId())) {
//两种节点不能一样
throw new MSException(Translator.get("invalid_parameter"));
}
ApiDebugModule dropNode = apiDebugModuleMapper.selectByPrimaryKey(request.getDropNodeId());
if (dropNode == null) {
throw new MSException(Translator.get(MODULE_NO_EXIST) + ": " + request.getDropNodeId());
}
if (request.getDropPosition() == 0) {
//dropPosition=0: 放到dropNode节点内最后一个节点之后
parentModule = new BaseModule(dropNode.getId(), dropNode.getName(), dropNode.getPos(), dropNode.getProjectId(), dropNode.getParentId());
ApiDebugModule previousModule = extApiDebugModuleMapper.getLastModuleByParentId(parentModule.getId());
if (previousModule != null) {
previousNode = new BaseModule(previousModule.getId(), previousModule.getName(), previousModule.getPos(), previousModule.getProjectId(), previousModule.getParentId());
}
} else {
if (StringUtils.equals(dropNode.getParentId(), ModuleConstants.ROOT_NODE_PARENT_ID)) {
parentModule = new BaseModule(ModuleConstants.ROOT_NODE_PARENT_ID, ModuleConstants.ROOT_NODE_PARENT_ID, 0, module.getProjectId(), ModuleConstants.ROOT_NODE_PARENT_ID);
} else {
ApiDebugModule parent = apiDebugModuleMapper.selectByPrimaryKey(dropNode.getParentId());
parentModule = new BaseModule(parent.getId(), parent.getName(), parent.getPos(), parent.getProjectId(), parent.getParentId());
}
if (request.getDropPosition() == 1) {
//dropPosition=1: 放到dropNode节点后原dropNode后面的节点之前
previousNode = new BaseModule(dropNode.getId(), dropNode.getName(), dropNode.getPos(), dropNode.getProjectId(), dropNode.getParentId());
ApiDebugModule nextModule = extApiDebugModuleMapper.getNextModuleInParentId(previousNode.getParentId(), previousNode.getPos());
if (nextModule != null) {
nextNode = new BaseModule(nextModule.getId(), nextModule.getName(), nextModule.getPos(), nextModule.getProjectId(), nextModule.getParentId());
}
} else if (request.getDropPosition() == -1) {
//dropPosition=-1: 放到dropNode节点前原dropNode前面的节点之后
nextNode = new BaseModule(dropNode.getId(), dropNode.getName(), dropNode.getPos(), dropNode.getProjectId(), dropNode.getParentId());
ApiDebugModule previousModule = extApiDebugModuleMapper.getPreviousModuleInParentId(nextNode.getParentId(), nextNode.getPos());
if (previousModule != null) {
previousNode = new BaseModule(previousModule.getId(), previousModule.getName(), previousModule.getPos(), previousModule.getProjectId(), previousModule.getParentId());
}
} else {
throw new MSException(Translator.get("invalid_parameter"));
}
}
NodeSortDTO nodeSortDTO = super.getNodeSortDTO(request,
extApiDebugModuleMapper::selectBaseModuleById,
extApiDebugModuleMapper::selectModuleByParentIdAndPosOperator);
ApiDebugModuleExample example = new ApiDebugModuleExample();
example.createCriteria().andParentIdEqualTo(parentModule.getId()).andIdEqualTo(module.getId());
example.createCriteria().andParentIdEqualTo(nodeSortDTO.getParent().getId()).andIdEqualTo(request.getDragNodeId());
//节点换到了别的节点下,要先更新parent节点.
if (apiDebugModuleMapper.countByExample(example) == 0) {
ApiDebugModule fileModule = new ApiDebugModule();
fileModule.setId(module.getId());
fileModule.setParentId(parentModule.getId());
fileModule.setId(request.getDragNodeId());
fileModule.setParentId(nodeSortDTO.getParent().getId());
apiDebugModuleMapper.updateByPrimaryKeySelective(fileModule);
}
NodeSortDTO nodeMoveDTO = new NodeSortDTO(module, parentModule, previousNode, nextNode);
super.sort(nodeMoveDTO);
super.sort(nodeSortDTO);
//记录日志
apiDebugModuleLogService.saveMoveLog(nodeMoveDTO, currentUser);
apiDebugModuleLogService.saveMoveLog(nodeSortDTO, currentUser);
}
/**

View File

@ -86,6 +86,7 @@ public abstract class ModuleTreeService {
private static final String MOVE_POS_OPERATOR_LESS = "lessThan";
private static final String MOVE_POS_OPERATOR_MORE = "moreThan";
private static final String MOVE_POS_OPERATOR_LATEST = "latest";
private static final String DRAG_NODE_NOT_EXIST = "drag_node.not.exist";
/**
@ -104,12 +105,12 @@ public abstract class ModuleTreeService {
BaseModule dragNode = selectIdNodeFunc.apply(request.getDragNodeId());
if (dragNode == null) {
throw new MSException("drag_node.not.exist" + ":" + request.getDragNodeId());
throw new MSException(Translator.get(DRAG_NODE_NOT_EXIST) + ":" + request.getDragNodeId());
}
BaseModule dropNode = selectIdNodeFunc.apply(request.getDropNodeId());
if (dropNode == null) {
throw new MSException("drop_node.not.exist" + ":" + request.getDropNodeId());
throw new MSException(Translator.get(DRAG_NODE_NOT_EXIST) + ":" + request.getDropNodeId());
}
BaseModule parentModule;

View File

@ -8,11 +8,8 @@ import io.metersphere.project.mapper.ExtProjectMapper;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.project.mapper.ProjectTestResourcePoolMapper;
import io.metersphere.project.request.ProjectSwitchRequest;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.ApplicationScope;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory;
@ -20,6 +17,9 @@ import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.system.dto.ProjectDTO;
import io.metersphere.system.dto.UpdateProjectRequest;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.system.mapper.OrganizationMapper;
import io.metersphere.system.mapper.TestResourcePoolMapper;
import io.metersphere.system.mapper.TestResourcePoolOrganizationMapper;
@ -67,8 +67,7 @@ public class ProjectService {
//判断用户是否是系统管理员
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(userId).andRoleIdEqualTo(InternalUserRole.ADMIN.name());
List<UserRoleRelation> list = userRoleRelationMapper.selectByExample(userRoleRelationExample);
if (CollectionUtils.isNotEmpty(list)) {
if (userRoleRelationMapper.countByExample(userRoleRelationExample) > 0) {
ProjectExample example = new ProjectExample();
example.createCriteria().andOrganizationIdEqualTo(organizationId).andEnableEqualTo(true);
return projectMapper.selectByExample(example);

View File

@ -8,12 +8,12 @@ import io.metersphere.project.request.ProjectSwitchRequest;
import io.metersphere.sdk.constants.ApplicationScope;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.dto.ProjectDTO;
import io.metersphere.system.dto.UpdateProjectRequest;
import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.system.log.constants.OperationLogType;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;