fix(接口测试): 接口分享左侧树查询问题

This commit is contained in:
song-cc-rock 2024-10-16 14:29:19 +08:00 committed by Craftsman
parent a60b7b2f01
commit 36019548ca
7 changed files with 95 additions and 25 deletions

View File

@ -5,12 +5,8 @@ import com.github.pagehelper.PageHelper;
import io.metersphere.api.domain.ApiDocShare;
import io.metersphere.api.dto.definition.ApiDocShareDTO;
import io.metersphere.api.dto.definition.ApiDocShareDetail;
import io.metersphere.api.dto.definition.ApiModuleRequest;
import io.metersphere.api.dto.definition.request.ApiDocShareCheckRequest;
import io.metersphere.api.dto.definition.request.ApiDocShareEditRequest;
import io.metersphere.api.dto.definition.request.ApiDocShareModuleRequest;
import io.metersphere.api.dto.definition.request.ApiDocSharePageRequest;
import io.metersphere.api.service.definition.ApiDefinitionModuleService;
import io.metersphere.api.dto.definition.request.*;
import io.metersphere.api.service.definition.ApiDefinitionExportService;
import io.metersphere.api.service.definition.ApiDocShareLogService;
import io.metersphere.api.service.definition.ApiDocShareService;
import io.metersphere.sdk.constants.PermissionConstants;
@ -47,7 +43,7 @@ public class ApiDocShareController {
@Resource
private ApiDocShareService apiDocShareService;
@Resource
private ApiDefinitionModuleService apiDefinitionModuleService;
private ApiDefinitionExportService apiDefinitionExportService;
@PostMapping(value = "/page")
@Operation(summary = "接口测试-接口管理-接口定义-分页获取分享列表")
@ -94,21 +90,34 @@ public class ApiDocShareController {
}
@GetMapping("/detail/{id}")
@Operation(summary = "接口测试-接口管理-接口定义-查看链接")
@Operation(summary = "接口测试-接口管理-接口定义-分享-查看链接")
@Parameter(name = "id", description = "分享ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
public ApiDocShareDetail detail(@PathVariable String id) {
return apiDocShareService.detail(id);
}
@PostMapping("/module/tree")
@Operation(summary = "接口测试-接口管理-接口定义-模块树")
public List<BaseTreeNode> getShareDocTree(@Validated @RequestBody ApiModuleRequest request) {
return apiDefinitionModuleService.getTree(request, false, true);
@Operation(summary = "接口测试-接口管理-接口定义-分享-模块树")
public List<BaseTreeNode> getShareDocTree(@Validated @RequestBody ApiDocShareModuleRequest request) {
return apiDocShareService.getShareTree(request);
}
@PostMapping("/module/count")
@Operation(summary = "接口测试-接口管理-接口定义-模块树数量")
@Operation(summary = "接口测试-接口管理-接口定义-分享-模块树数量")
public Map<String, Long> getShareDocTreeCount(@Validated @RequestBody ApiDocShareModuleRequest request) {
return apiDocShareService.getShareTreeCount(request);
}
@PostMapping("/export/{type}")
@Operation(summary = "接口测试-接口管理-接口定义-分享-导出")
public String export(@RequestBody ApiDocShareExportRequest request, @PathVariable String type) {
return apiDocShareService.export(request, type, SessionUtils.getUserId());
}
@GetMapping("/stop/{taskId}")
@Operation(summary = "接口测试-接口管理-导出-停止导出")
public void caseStopExport(@PathVariable String taskId) {
apiDefinitionExportService.stopExport(taskId, SessionUtils.getUserId());
}
}

View File

@ -0,0 +1,17 @@
package io.metersphere.api.dto.definition.request;
import io.metersphere.api.dto.definition.ApiDefinitionBatchExportRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author song-cc-rock
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class ApiDocShareExportRequest extends ApiDefinitionBatchExportRequest {
@Schema(description = "分享ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String shareId;
}

View File

@ -102,5 +102,5 @@ public interface ExtApiDefinitionMapper {
List<ApiDefinition> getListBySelectIds(@Param("projectId") String projectId, @Param("ids") List<String> ids, @Param("protocols") List<String> protocols);
Long countByShareParam(@Param("projectId") String projectId, @Param("condition") String condition);
List<String> getIdsByShareParam(@Param("projectId") String projectId, @Param("condition") String condition);
}

View File

@ -752,8 +752,8 @@
</if>
</select>
<select id="countByShareParam" resultType="java.lang.Long">
select count(*) from api_definition where project_id = #{projectId} and deleted = 0
<select id="getIdsByShareParam" resultType="java.lang.String">
select id from api_definition where project_id = #{projectId} and deleted = 0
<if test="condition != null and condition != ''">
and ${condition}
</if>

View File

@ -4,7 +4,7 @@
<mapper namespace="io.metersphere.api.mapper.ExtApiDocShareMapper">
<select id="list" resultType="io.metersphere.api.dto.definition.ApiDocShareDTO">
select id, name, is_private isPrivate, create_user createUser, create_time createTime,
select id, name, is_private isPrivate, create_user createUser, create_time createTime, allow_export allowExport,
api_range apiRange, range_match_symbol rangeMatchSymbol, range_match_val rangeMatchVal,
invalid_time invalidTime, invalid_unit invalidUnit, project_id projectId, password
from api_doc_share

View File

@ -3,10 +3,7 @@ package io.metersphere.api.service.definition;
import io.metersphere.api.domain.ApiDocShare;
import io.metersphere.api.dto.definition.ApiDocShareDTO;
import io.metersphere.api.dto.definition.ApiDocShareDetail;
import io.metersphere.api.dto.definition.request.ApiDocShareCheckRequest;
import io.metersphere.api.dto.definition.request.ApiDocShareEditRequest;
import io.metersphere.api.dto.definition.request.ApiDocShareModuleRequest;
import io.metersphere.api.dto.definition.request.ApiDocSharePageRequest;
import io.metersphere.api.dto.definition.request.*;
import io.metersphere.api.mapper.ApiDocShareMapper;
import io.metersphere.api.mapper.ExtApiDefinitionMapper;
import io.metersphere.api.mapper.ExtApiDocShareMapper;
@ -16,8 +13,10 @@ import io.metersphere.sdk.dto.CombineSearch;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -42,6 +41,8 @@ public class ApiDocShareService {
private ExtApiDocShareMapper extApiDocShareMapper;
@Resource
private ApiDefinitionModuleService apiDefinitionModuleService;
@Resource
private ApiDefinitionExportService apiDefinitionExportService;
public static final String RANGE_ALL = "ALL";
@ -123,6 +124,16 @@ public class ApiDocShareService {
return detail;
}
/**
* 查询分享左侧模块树
* @param request 请求参数
* @return 模块树节点数量
*/
public List<BaseTreeNode> getShareTree(ApiDocShareModuleRequest request) {
ApiDocShare docShare = checkExit(request.getShareId());
return apiDefinitionModuleService.getTree(buildModuleParam(request, docShare), false, true);
}
/**
* 查询分享左侧模块树节点数量
* @param request 请求参数
@ -133,6 +144,23 @@ public class ApiDocShareService {
return apiDefinitionModuleService.moduleCount(buildModuleParam(request, docShare), false);
}
/**
* 导出接口定义
* @param request 请求参数
* @param type 类型
* @param currentUser 当前用户
* @return 接口定义导出返回
*/
public String export(ApiDocShareExportRequest request, String type, String currentUser) {
if (request.isSelectAll()) {
ApiDocShare docShare = checkExit(request.getShareId());
List<String> shareIds = getShareIdsByParam(docShare);
request.setSelectAll(false);
request.setSelectIds(shareIds);
}
return apiDefinitionExportService.exportApiDefinition(request, type, currentUser);
}
/**
* 构建分享额外信息
* @param docShares 分享列表
@ -153,6 +181,16 @@ public class ApiDocShareService {
* @return 数量
*/
public Integer countApiShare(ApiDocShareDTO docShare) {
List<String> shareIds = getShareIdsByParam(docShare);
return CollectionUtils.isEmpty(shareIds) ? 0 : shareIds.size();
}
/**
* 根据分享信息获取分享的定义ID集合
* @param docShare 分享信息
* @return 分享的定义ID集合
*/
public List<String> getShareIdsByParam(ApiDocShare docShare) {
StringBuilder condition = new StringBuilder();
if (!StringUtils.equals(docShare.getApiRange(), RANGE_ALL) && !StringUtils.isBlank(docShare.getRangeMatchVal())) {
switch (docShare.getApiRange()) {
@ -183,7 +221,7 @@ public class ApiDocShareService {
}
}
}
return extApiDefinitionMapper.countByShareParam(docShare.getProjectId(), condition.toString()).intValue();
return extApiDefinitionMapper.getIdsByShareParam(docShare.getProjectId(), condition.toString());
}
/**

View File

@ -1,10 +1,8 @@
package io.metersphere.api.controller;
import io.metersphere.api.domain.ApiDocShare;
import io.metersphere.api.dto.definition.request.ApiDocShareCheckRequest;
import io.metersphere.api.dto.definition.request.ApiDocShareEditRequest;
import io.metersphere.api.dto.definition.request.ApiDocShareModuleRequest;
import io.metersphere.api.dto.definition.request.ApiDocSharePageRequest;
import io.metersphere.api.dto.definition.request.*;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
@ -36,6 +34,7 @@ public class ApiDocShareControllerTests extends BaseTest {
private final static String DETAIL = BASE_PATH + "detail/";
private final static String MODULE_TREE = BASE_PATH + "module/tree";
private final static String MODULE_COUNT = BASE_PATH + "module/count";
private final static String EXPORT = BASE_PATH + "export/Swagger";
@Order(1)
@Test
@ -73,6 +72,13 @@ public class ApiDocShareControllerTests extends BaseTest {
this.requestPostWithOk(CHECK, checkRequest);
this.requestGetWithOk(DETAIL + docShare.getId());
this.requestPostWithOk(MODULE_TREE, moduleRequest);
ApiDocShareExportRequest exportRequest = new ApiDocShareExportRequest();
BeanUtils.copyBean(exportRequest, moduleRequest);
exportRequest.setSelectAll(true);
this.requestPostWithOk(EXPORT, exportRequest);
exportRequest.setSelectAll(false);
exportRequest.setSelectIds(List.of("export-id"));
this.requestPost(EXPORT, exportRequest);
this.requestGetWithOk(DELETE + docShare.getId());
// 不存在的ID
this.requestGet(DELETE + "not-exist-id").andExpect(status().is5xxServerError());