From a556d02625bd2a6aad56258bb73a2cbfaebc9755 Mon Sep 17 00:00:00 2001 From: lan-yonghui Date: Mon, 20 Nov 2023 17:12:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=AE=A1=E7=90=86=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=88=A0=E6=94=B9=E5=A4=8D=E5=88=B6=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definition/ApiDefinitionController.java | 4 +- .../api/dto/definition/ApiCaseComputeDTO.java | 7 +- .../definition/ApiDefinitionAddRequest.java | 6 +- .../ApiDefinitionBatchMoveRequest.java | 3 + .../definition/ApiDefinitionBatchRequest.java | 12 +++ .../ApiDefinitionBatchUpdateRequest.java | 7 +- .../definition/ApiDefinitionCopyRequest.java | 2 + .../ApiDefinitionDeleteRequest.java | 2 + .../definition/ApiDefinitionPageRequest.java | 7 +- .../ApiDefinitionUpdateRequest.java | 3 + .../api/mapper/ExtApiDefinitionMapper.java | 3 +- .../api/mapper/ExtApiDefinitionMapper.xml | 8 +- .../definition/ApiDefinitionLogService.java | 6 +- .../definition/ApiDefinitionService.java | 96 +++++++------------ .../ApiDefinitionControllerTests.java | 10 +- .../resources/dml/init_api_definition.sql | 2 +- 16 files changed, 96 insertions(+), 82 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java index b713fa0467..9dcbb25b95 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java @@ -63,14 +63,14 @@ public class ApiDefinitionController { } @PostMapping(value = "/delete") - @Operation(summary = "接口测试-接口管理-删除接口定义") + @Operation(summary = "接口测试-接口管理-删除接口定义到回收站") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DELETE) @Log(type = OperationLogType.DELETE, expression = "#msClass.delLog(#request)", msClass = ApiDefinitionLogService.class) public void delete(@Validated @RequestBody ApiDefinitionDeleteRequest request) { apiDefinitionService.delete(request, SessionUtils.getUserId()); } @PostMapping(value = "/batch-del") - @Operation(summary = "接口测试-接口管理-批量删除接口定义") + @Operation(summary = "接口测试-接口管理-批量删除接口定义到回收站") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_DELETE) @Log(type = OperationLogType.DELETE, expression = "#msClass.batchDelLog(#request)", msClass = ApiDefinitionLogService.class) public void batchDelete(@Validated @RequestBody ApiDefinitionBatchRequest request) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiCaseComputeDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiCaseComputeDTO.java index be0e583f2e..6dbebca251 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiCaseComputeDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiCaseComputeDTO.java @@ -3,13 +3,18 @@ package io.metersphere.api.dto.definition; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; +import java.io.Serial; +import java.io.Serializable; + /** * @author: LAN * @date: 2023/11/8 19:17 * @version: 1.0 */ @Data -public class ApiCaseComputeDTO { +public class ApiCaseComputeDTO implements Serializable { + @Serial + private static final long serialVersionUID = 1L; @Schema(description = "接口ID") private String apiDefinitionId; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java index d4164f7d2d..aee54576e3 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionAddRequest.java @@ -6,8 +6,9 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; import lombok.Data; +import java.io.Serial; import java.io.Serializable; -import java.util.List; +import java.util.LinkedHashSet; /** * @author lan @@ -15,6 +16,7 @@ import java.util.List; @Data public class ApiDefinitionAddRequest implements Serializable { + @Serial private static final long serialVersionUID = 1L; @@ -56,7 +58,7 @@ public class ApiDefinitionAddRequest implements Serializable { private String description; @Schema(description = "标签") - private List<@NotBlank String> tags; + private LinkedHashSet<@NotBlank String> tags; @Schema(description = "请求内容") @NotBlank diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchMoveRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchMoveRequest.java index 8c6b321d74..3200a11062 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchMoveRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchMoveRequest.java @@ -4,6 +4,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; +import java.io.Serial; + /** * @author lan */ @@ -11,6 +13,7 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = false) public class ApiDefinitionBatchMoveRequest extends ApiDefinitionBatchRequest { + @Serial private static final long serialVersionUID = 1L; @Schema(description = "模块ID", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchRequest.java index c7777ebc5a..8f4fc5853e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchRequest.java @@ -1,5 +1,6 @@ package io.metersphere.api.dto.definition; +import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotBlank; @@ -7,7 +8,9 @@ import jakarta.validation.constraints.Size; import lombok.Data; import lombok.EqualsAndHashCode; +import java.io.Serial; import java.io.Serializable; +import java.util.List; /** * @author lan @@ -16,6 +19,7 @@ import java.io.Serializable; @EqualsAndHashCode(callSuper = false) public class ApiDefinitionBatchRequest extends TableBatchProcessDTO implements Serializable { + @Serial private static final long serialVersionUID = 1L; @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) @@ -23,6 +27,14 @@ public class ApiDefinitionBatchRequest extends TableBatchProcessDTO implements S @Size(min = 1, max = 50, message = "{api_definition.project_id.length_range}") private String projectId; + @Schema(description = "接口协议", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{api_debug.protocol.not_blank}") + @Size(min = 1, max = 20, message = "{api_debug.protocol.length_range}") + private String protocol = ModuleConstants.NODE_PROTOCOL_HTTP; + + @Schema(description = "模块ID(根据模块树查询时要把当前节点以及子节点都放在这里。)") + private List moduleIds; + @Schema(description = "删除列表版本/删除全部版本") private Boolean deleteAll = false; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchUpdateRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchUpdateRequest.java index 3ee92fc441..5c0f050fec 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchUpdateRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionBatchUpdateRequest.java @@ -6,7 +6,7 @@ import jakarta.validation.constraints.Size; import lombok.Data; import lombok.EqualsAndHashCode; -import java.util.List; +import java.util.LinkedHashSet; /** * @author lan @@ -17,6 +17,9 @@ public class ApiDefinitionBatchUpdateRequest extends ApiDefinitionBatchRequest { private static final long serialVersionUID = 1L; + @Schema(description = "所需更新的字段名", requiredMode = Schema.RequiredMode.REQUIRED) + private String type; + @Schema(description = "http协议类型post/get/其它协议则是协议名(mqtt)") private String method; @@ -29,7 +32,7 @@ public class ApiDefinitionBatchUpdateRequest extends ApiDefinitionBatchRequest { private String versionId; @Schema(description = "标签") - private List<@NotBlank String> tags; + private LinkedHashSet<@NotBlank String> tags; @Schema(description = "是否追加", requiredMode = Schema.RequiredMode.REQUIRED) private boolean append = false; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionCopyRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionCopyRequest.java index 71c5b9eb13..0a24127426 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionCopyRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionCopyRequest.java @@ -5,6 +5,7 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; import lombok.Data; +import java.io.Serial; import java.io.Serializable; /** @@ -13,6 +14,7 @@ import java.io.Serializable; @Data public class ApiDefinitionCopyRequest implements Serializable { + @Serial private static final long serialVersionUID = 1L; @Schema(description = "接口pk", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDeleteRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDeleteRequest.java index 2d5b51c83d..289ae9bc79 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDeleteRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDeleteRequest.java @@ -5,6 +5,7 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; import lombok.Data; +import java.io.Serial; import java.io.Serializable; /** @@ -13,6 +14,7 @@ import java.io.Serializable; @Data public class ApiDefinitionDeleteRequest implements Serializable { + @Serial private static final long serialVersionUID = 1L; @Schema(description = "接口pk", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionPageRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionPageRequest.java index 4ec9e5da09..568d6764d5 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionPageRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionPageRequest.java @@ -8,6 +8,8 @@ import jakarta.validation.constraints.Size; import lombok.Data; import lombok.EqualsAndHashCode; +import java.util.List; + /** * @author lan */ @@ -15,8 +17,6 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = false) public class ApiDefinitionPageRequest extends BasePageRequest { - private static final long serialVersionUID = 1L; - @Schema(description = "接口pk") @Size(min = 1, max = 50, message = "{api_definition.id.length_range}") private String id; @@ -42,4 +42,7 @@ public class ApiDefinitionPageRequest extends BasePageRequest { @Schema(description = "版本引用fk") @Size(min = 1, max = 50, message = "{api_definition.ref_id.length_range}") private String refId; + + @Schema(description = "模块ID(根据模块树查询时要把当前节点以及子节点都放在这里。)") + private List moduleIds; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionUpdateRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionUpdateRequest.java index 8ce8ffb31e..f567a41c7b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionUpdateRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionUpdateRequest.java @@ -6,6 +6,8 @@ import jakarta.validation.constraints.Size; import lombok.Data; import lombok.EqualsAndHashCode; +import java.io.Serial; + /** * @author lan */ @@ -13,6 +15,7 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = false) public class ApiDefinitionUpdateRequest extends ApiDefinitionAddRequest { + @Serial private static final long serialVersionUID = 1L; @Schema(description = "接口pk", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.java b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.java index ce5d165180..e66a99cb02 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.java @@ -16,7 +16,7 @@ public interface ExtApiDefinitionMapper { Long getPos(@Param("projectId") String projectId); - List getIds(@Param("request") TableBatchProcessDTO request, @Param("projectId") String projectId, @Param("deleted") boolean deleted); + List getIds(@Param("request") TableBatchProcessDTO request, @Param("projectId") String projectId, @Param("protocol") String protocol, @Param("deleted") boolean deleted); List getRefIds(@Param("ids") List ids); @@ -29,6 +29,7 @@ public interface ExtApiDefinitionMapper { void batchDelete(@Param("ids") List ids, @Param("userId") String userId); void clearLatestVersion(@Param("refId") String refId, @Param("projectId") String projectId); + void updateLatestVersion(@Param("id") String id, @Param("projectId") String projectId); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml index eb664b0e4e..3df7181e8b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml +++ b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml @@ -61,7 +61,7 @@ @@ -153,6 +153,12 @@ AND api_definition.protocol = #{request.protocol} + + and api_definition.module_id in + + #{nodeId} + + diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java index ee707f45e1..44ec56b3d3 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java @@ -112,7 +112,7 @@ public class ApiDefinitionLogService { * @return */ public List batchDelLog(ApiDefinitionBatchRequest request) { - List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId()); + List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId(), request.getProtocol()); List dtoList = new ArrayList<>(); if (CollectionUtils.isNotEmpty(ids)) { ApiDefinitionExample example = new ApiDefinitionExample(); @@ -144,7 +144,7 @@ public class ApiDefinitionLogService { * @return */ public List batchUpdateLog(ApiDefinitionBatchUpdateRequest request) { - List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId()); + List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId(), request.getProtocol()); ApiDefinitionExample example = new ApiDefinitionExample(); example.createCriteria().andIdIn(ids); List apiDefinitions = apiDefinitionMapper.selectByExample(example); @@ -188,7 +188,7 @@ public class ApiDefinitionLogService { } public List batchMoveLog(ApiDefinitionBatchMoveRequest request) { - List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId()); + List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId(), request.getProtocol()); ApiDefinitionExample example = new ApiDefinitionExample(); example.createCriteria().andIdIn(ids); List apiDefinitions = apiDefinitionMapper.selectByExample(example); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index ef3e389bcb..485495ee0f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -11,12 +11,14 @@ import io.metersphere.api.mapper.ExtApiDefinitionMapper; import io.metersphere.api.util.ApiDataUtils; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.project.mapper.ExtBaseProjectVersionMapper; -import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.service.ProjectService; import io.metersphere.sdk.constants.ApplicationNumScope; import io.metersphere.sdk.constants.StorageType; import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.util.*; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.LogUtils; +import io.metersphere.sdk.util.Translator; import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.file.FileRequest; import io.metersphere.system.file.MinioRepository; @@ -55,9 +57,6 @@ public class ApiDefinitionService { @Resource private ApiDefinitionFollowerMapper apiDefinitionFollowerMapper; - @Resource - private ProjectMapper projectMapper; - @Resource private ExtBaseProjectVersionMapper extBaseProjectVersionMapper; @@ -73,11 +72,13 @@ public class ApiDefinitionService { @Resource private SqlSessionFactory sqlSessionFactory; + @Resource + private ApiTestCaseService apiTestCaseService; + public List getApiDefinitionPage(ApiDefinitionPageRequest request, Boolean deleted){ List list = extApiDefinitionMapper.list(request, deleted); if (!CollectionUtils.isEmpty(list)) { - convertUserIdToName(list); - calculateApiCase(list, request.getProjectId()); + processApiDefinitions(list, request.getProjectId()); } return list; } @@ -152,9 +153,9 @@ public class ApiDefinitionService { public void batchUpdate(ApiDefinitionBatchUpdateRequest request, String userId) { ProjectService.checkResourceExist(request.getProjectId()); - List ids = getBatchApiIds(request, request.getProjectId()); + List ids = getBatchApiIds(request, request.getProjectId(), request.getProtocol()); if (CollectionUtils.isNotEmpty(ids)) { - if (CollectionUtils.isNotEmpty(request.getTags())) { + if (request.getType().equals("tags")) { handleTags(request, userId, ids); } else { ApiDefinition apiDefinition = new ApiDefinition(); @@ -197,66 +198,43 @@ public class ApiDefinitionService { return apiDefinition; } - public void batchCopy(ApiDefinitionBatchUpdateRequest request, String userId) { - List ids = getBatchApiIds(request, request.getProjectId()); - if (CollectionUtils.isNotEmpty(ids)) { - // TODO 批量复制 - List refId = extApiDefinitionMapper.getRefIds(ids); - } - } - public void delete(ApiDefinitionDeleteRequest request, String userId) { checkApiDefinition(request.getId()); handleDeleteApiDefinition(Collections.singletonList(request.getId()),request.getDeleteAll(), userId); } public void batchDelete(ApiDefinitionBatchRequest request, String userId) { - List ids = getBatchApiIds(request, request.getProjectId()); + List ids = getBatchApiIds(request, request.getProjectId(), request.getProtocol()); if (CollectionUtils.isNotEmpty(ids)) { handleDeleteApiDefinition(ids, request.getDeleteAll(), userId); } } public void batchMove(ApiDefinitionBatchMoveRequest request, String userId) { - List ids = getBatchApiIds(request, request.getProjectId()); + List ids = getBatchApiIds(request, request.getProjectId(), request.getProtocol()); if (CollectionUtils.isNotEmpty(ids)) { List refId = extApiDefinitionMapper.getRefIds(ids); extApiDefinitionMapper.batchMove(request, refId, userId); } } - - public void recycleDelete(){ - // todo 删除接口用例 - - // todo 是否有自定义字段关联关系,删除自定义字段关系 - - // todo 是否删除关注人(如果删除,回收站恢复的时候如何处理) - } - - private void convertUserIdToName(List list) { + private void processApiDefinitions(List list, String projectId) { Set userIds = extractUserIds(list); Map userMap = userLoginService.getUserNameMap(new ArrayList<>(userIds)); - list.forEach(item -> { - item.setCreateUserName(userMap.get(item.getCreateUser())); - item.setDeleteUserName(userMap.get(item.getDeleteUser())); - item.setUpdateUserName(userMap.get(item.getUpdateUser())); - }); - } - - private Set extractUserIds(List list) { - return list.stream() - .flatMap(apiDefinition -> Stream.of(apiDefinition.getUpdateUser(), apiDefinition.getDeleteUser(), apiDefinition.getCreateUser())) - .collect(Collectors.toSet()); - } - - private void calculateApiCase(List list, String projectId) { - List ids = list.stream().map(ApiDefinitionDTO::getId).toList(); - List apiCaseComputeList = extApiDefinitionMapper.selectApiCaseByIdsAndStatusIsNotTrash(ids, projectId); + List apiDefinitionIds = list.stream().map(ApiDefinitionDTO::getId).toList(); + List apiCaseComputeList = extApiDefinitionMapper.selectApiCaseByIdsAndStatusIsNotTrash(apiDefinitionIds, projectId); Map resultMap = apiCaseComputeList.stream().collect(Collectors.toMap(ApiCaseComputeDTO::getApiDefinitionId, Function.identity())); list.forEach(item -> { + // Convert User IDs to Names + item.setCreateUserName(userMap.get(item.getCreateUser())); + item.setDeleteUserName(userMap.get(item.getDeleteUser())); + item.setUpdateUserName(userMap.get(item.getUpdateUser())); + + // Convert tags + + // Calculate API Case Metrics ApiCaseComputeDTO apiCaseComputeDTO = resultMap.get(item.getId()); if (apiCaseComputeDTO != null) { item.setCaseTotal(apiCaseComputeDTO.getCaseTotal()); @@ -279,6 +257,12 @@ public class ApiDefinitionService { }); } + private Set extractUserIds(List list) { + return list.stream() + .flatMap(apiDefinition -> Stream.of(apiDefinition.getUpdateUser(), apiDefinition.getDeleteUser(), apiDefinition.getCreateUser())) + .collect(Collectors.toSet()); + } + public Long getNextOrder(String projectId) { Long pos = extApiDefinitionMapper.getPos(projectId); return (pos == null ? 0 : pos) + ORDER_STEP; @@ -350,10 +334,10 @@ public class ApiDefinitionService { return apiDefinitionBlobMapper.selectByPrimaryKey(apiId); } - public List getBatchApiIds(T dto, String projectId) { + public List getBatchApiIds(T dto, String projectId, String protocol) { TableBatchProcessDTO request = (TableBatchProcessDTO) dto; if (request.isSelectAll()) { - List ids = extApiDefinitionMapper.getIds(request, projectId, false); + List ids = extApiDefinitionMapper.getIds(request, projectId, protocol, false); if (CollectionUtils.isNotEmpty(request.getExcludeIds())) { ids.removeAll(request.getExcludeIds()); } @@ -399,7 +383,7 @@ public class ApiDefinitionService { ids.forEach(id -> { ApiDefinition apiDefinition = new ApiDefinition(); if (StringUtils.isNotBlank(collect.get(id).getTags())) { - List tags = JSON.parseArray(collect.get(id).getTags(), String.class); + LinkedHashSet tags = new LinkedHashSet<>((JSON.parseArray(collect.get(id).getTags(), String.class))); tags.addAll(request.getTags()); apiDefinition.setTags(JSON.toJSONString(tags)); } else { @@ -494,22 +478,6 @@ public class ApiDefinitionService { extApiDefinitionMapper.updateLatestVersion(id, projectId); } - - private void deleteAll(String apiDefinitionId, String userId) { - // 查找全部版本 - List apiDefinitionVersions = getApiDefinitionVersion(apiDefinitionId); - - if (!apiDefinitionVersions.isEmpty()) { - // 获取所有版本的ID - List ids = apiDefinitionVersions.stream() - .map(ApiDefinitionVersionDTO::getId) - .toList(); - - // 删除所有版本 - ids.forEach(id -> doDelete(id, userId)); - } - } - private void doDelete(String id, String userId) { ApiDefinition apiDefinition = new ApiDefinition(); apiDefinition.setDeleted(true); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index eab689859a..1fd2569e76 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -162,7 +162,7 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setModuleId("root"); request.setVersionId(defaultVersion); request.setDescription("描述内容"); - request.setTags(List.of("tag1", "tag2")); + request.setTags(new LinkedHashSet<>(List.of("tag1", "tag2"))); return request; } @@ -272,25 +272,29 @@ public class ApiDefinitionControllerTests extends BaseTest { apiDefinitionBatchUpdateRequest.setSelectIds(List.of("1001","1002","1005")); apiDefinitionBatchUpdateRequest.setExcludeIds(List.of("1005")); apiDefinitionBatchUpdateRequest.setSelectAll(false); + apiDefinitionBatchUpdateRequest.setType("tags"); // 修改标签,追加 apiDefinitionBatchUpdateRequest.setSelectIds(List.of("1001","1002")); - apiDefinitionBatchUpdateRequest.setTags(List.of("tag-append","tag-append1")); + apiDefinitionBatchUpdateRequest.setTags(new LinkedHashSet<>(List.of("tag-append","tag-append1"))); apiDefinitionBatchUpdateRequest.setAppend(true); this.requestPostWithOk(BATCH_UPDATE, apiDefinitionBatchUpdateRequest); assertBatchUpdateApiDefinition(apiDefinitionBatchUpdateRequest, List.of("1001","1002")); // 修改标签,覆盖 apiDefinitionBatchUpdateRequest.setSelectIds(List.of("1003","1004")); - apiDefinitionBatchUpdateRequest.setTags(List.of("tag-append","tag-append1")); + apiDefinitionBatchUpdateRequest.setTags(new LinkedHashSet<>(List.of("tag-append","tag-append1"))); apiDefinitionBatchUpdateRequest.setAppend(false); this.requestPostWithOk(BATCH_UPDATE, apiDefinitionBatchUpdateRequest); assertBatchUpdateApiDefinition(apiDefinitionBatchUpdateRequest, List.of("1003","1004")); // 修改协议类型 + apiDefinitionBatchUpdateRequest.setType("method"); apiDefinitionBatchUpdateRequest.setMethod("batch-method"); this.requestPostWithOk(BATCH_UPDATE, apiDefinitionBatchUpdateRequest); // 修改状态 + apiDefinitionBatchUpdateRequest.setType("status"); apiDefinitionBatchUpdateRequest.setStatus(ApiDefinitionStatus.DEBUGGING.getValue()); this.requestPostWithOk(BATCH_UPDATE, apiDefinitionBatchUpdateRequest); // 修改版本 + apiDefinitionBatchUpdateRequest.setType("version"); apiDefinitionBatchUpdateRequest.setVersionId("batch-version"); this.requestPostWithOk(BATCH_UPDATE, apiDefinitionBatchUpdateRequest); // 修改全部 diff --git a/backend/services/api-test/src/test/resources/dml/init_api_definition.sql b/backend/services/api-test/src/test/resources/dml/init_api_definition.sql index fe8cf1f032..6f3ea6ef95 100644 --- a/backend/services/api-test/src/test/resources/dml/init_api_definition.sql +++ b/backend/services/api-test/src/test/resources/dml/init_api_definition.sql @@ -1,7 +1,6 @@ -- 插入测试数据 TRUNCATE TABLE api_definition; -TRUNCATE TABLE project_version; TRUNCATE TABLE api_test_case; TRUNCATE TABLE api_report; @@ -12,6 +11,7 @@ INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `statu INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1005', 'test-5', 'HTTP', 'POST', '/api/admin/5', 'Underway', 1005, '[\"test5\",\"te\"]', 1, '100001100001', 'root', b'0', '1005704995741369851', '1004', NULL, 1699500298164, 'admin', 1699500298164, 'admin', NULL, NULL, b'0'); INSERT INTO `api_definition` (`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) VALUES ('1006', 'test-6', 'HTTP', 'GET', '/api/admin/6', 'Completed', 1006, '[\"test6\",\"te\"]', 1, '100001100001', 'root', b'1', '100570499574136985', '1006', NULL, 1699500298164, 'admin', 1699500298164, 'admin', NULL, NULL, b'0'); +DELETE FROM `project_version` WHERE `id` = '100570499574136985'; INSERT INTO project_version (id, project_id, name, description, status, latest, publish_time, start_time, end_time, create_time, create_user) VALUES ('100570499574136985', '100001100001', 'v1.0.0', NULL, 'open', 1, UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin'); INSERT INTO `api_test_case` (`id`, `name`, `priority`, `num`, `tags`, `status`, `last_report_status`, `last_report_id`, `pos`, `project_id`, `api_definition_id`, `version_id`, `environment_id`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_time`, `delete_user`, `deleted`) VALUES ('12df5721-c5e6-a38b-e999-3eafcb992094', '查询windows主机', 'P0', 100002001, NULL, 'PENDING', NULL, '10001', 10000, '100001100001', '1001', '100570499574136985', 'admin', 1699500298164, 'admin', 1699500298164, 'admin', NULL, NULL, b'0');