测试API模块提交

dev
liyongbin 2023-12-26 21:44:05 +08:00
parent ce78b4e3fd
commit 1b2ac94b2d
10 changed files with 436 additions and 105 deletions

View File

@ -1,21 +0,0 @@
package net.srt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @ClassName : ${NAME}
* @Description : ${description}
* @Author : FJJ
* @Date: 2023-12-22 20:44
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}

View File

@ -25,95 +25,149 @@ import java.util.List;
public class ApiConfigController {
private final ApiConfigService apiConfigService;
/**
*
* @param query
* @return
*/
@GetMapping("page")
@Operation(summary = "分页")
@Operation(summary = "分页查询接口配置列表")
@PreAuthorize("hasAuthority('data-service:api-config:page')")
public Result<PageResult<ApiConfigVo>> page(@Valid ApiConfigQuery query) {
PageResult<ApiConfigVo> page = apiConfigService.page(query);
return Result.ok(page);
PageResult<ApiConfigVo> page = apiConfigService.page(query); // 调用service层方法获取分页结果
return Result.ok(page); // 封装返回结果并返回
}
/**
* resourceId
* @param query
* @return
*/
@GetMapping("page-resource")
@Operation(summary = "根据resourceId分页获取")
@Operation(summary = "根据resourceId分页获取接口配置列表")
public Result<PageResult<ApiConfigVo>> pageResource(@Valid ApiConfigQuery query){
PageResult<ApiConfigVo> page = apiConfigService.pageResource(query);
return Result.ok(page);
PageResult<ApiConfigVo> page = apiConfigService.pageResource(query); // 调用service层方法根据resourceId分页获取接口配置列表
return Result.ok(page); // 封装返回结果并返回
}
// @GetMapping("{id}")
// @Operation(summary = "信息")
// public ResponseEntity<?> get(@PathVariable("id") Long id) {
// ApiConfigEntity entity = apiConfigService.getById(id);
// if (entity != null && entity.getStatus() == 1) {
// return ResponseEntity.ok(ApiConfigConvert.INSTANCE.convert(entity));
// } else {
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Resource not found");
// }
// }
/**
* id
* @param id id
* @return id
*/
@GetMapping("{id}")
@Operation(summary = "信息")
@Operation(summary = "根据id获取接口配置信息")
@PreAuthorize("hasAnyAuthority('data-service:api-config:info')")
public Result<ApiConfigVo> get(@PathVariable("id") Long id){
ApiConfigEntity entity=apiConfigService.getById(id);
return Result.ok(ApiConfigConvert.INSTANCE.convert(entity));
ApiConfigEntity entity=apiConfigService.getById(id); // 根据id获取接口配置实体对象
return Result.ok(ApiConfigConvert.INSTANCE.convert(entity)); // 封装返回结果并返回
}
/**
*
* @param vo
* @return
*/
@PostMapping
@Operation(summary = "保存")
@Operation(summary = "保存接口配置信息")
@PreAuthorize("hasAnyAuthority('data-service:api-config:save')")
public Result<String> save(@RequestBody ApiConfigVo vo) {
apiConfigService.save(vo);
return Result.ok();
apiConfigService.save(vo); // 调用service层方法保存接口配置信息
return Result.ok(); // 封装返回结果并返回
}
/**
*
* @param vo
* @return
*/
@PutMapping
@Operation(summary = "修改")
@Operation(summary = "修改接口配置信息")
@PreAuthorize("hasAnyAuthority('data-service:api-config:update')")
public Result<String> update(@RequestBody ApiConfigVo vo){
apiConfigService.update(vo);
return Result.ok();
apiConfigService.update(vo); // 调用service层方法修改接口配置信息
return Result.ok(); // 封装返回结果并返回
}
/**
*
* @param idList id
* @return
*/
@DeleteMapping
@Operation(summary = "删除")
@Operation(summary = "删除接口配置信息")
@PreAuthorize("hasAnyAuthority('data-service:api-config:delete')")
public Result<String> delete(@RequestBody List<Long> idList){
apiConfigService.delete(idList);
return Result.ok();
apiConfigService.delete(idList); // 调用service层方法删除接口配置信息
return Result.ok(); // 封装返回结果并返回
}
/**
* IP
* @return IP
*/
@GetMapping("getIpPort")
@Operation(summary = "获取服务的IP和端口号")
public Result<String> getIpPort() {
return Result.ok(apiConfigService.getIpPort());
return Result.ok(apiConfigService.getIpPort()); // 封装返回结果并返回
}
@Operation(summary = "获取服务的ip和端口号")
/**
* IP
* @return IP
*/
@GetMapping("/ip-port")
@Operation(summary = "获取服务的IP和端口号")
public Result<String> ipPort() {
return Result.ok(apiConfigService.ipPort());
return Result.ok(apiConfigService.ipPort()); // 封装返回结果并返回
}
@Operation(summary = "上线")
@PreAuthorize("hasAnyAuthority('data-service:api-config:online')")
/**
* 线id
* @param id 线id
* @return 线
*/
@PutMapping("/{id}/online")
@Operation(summary = "上线指定id的接口配置")
@PreAuthorize("hasAnyAuthority('data-service:api-config:online')")
public Result<String> online(@PathVariable Long id) {
apiConfigService.online(id);
return Result.ok();
apiConfigService.online(id); // 调用service层方法上线指定id的接口配置
return Result.ok(); // 封装返回结果并返回
}
@Operation(summary = "下线")
@PreAuthorize("hasAnyAuthority('data-service:api-config:offline')")
/**
* 线id
* @param id 线id
* @return 线
*/
@PutMapping("/{id}/offline")
@Operation(summary = "下线指定id的接口配置")
@PreAuthorize("hasAnyAuthority('data-service:api-config:offline')")
public Result<String> offline(@PathVariable Long id){
apiConfigService.offline(id);
return Result.ok();
apiConfigService.offline(id); // 调用service层方法下线指定id的接口配置
return Result.ok(); // 封装返回结果并返回
}
@Operation(summary = "执行sql")
/**
* SQL
* @param dto SQL
* @return SQL
*/
@PostMapping("/sql/execute")
@Operation(summary = "执行SQL查询")
public Result<JdbcSelectResult> sqlExecute(@RequestBody SqlDto dto) {
return Result.ok(apiConfigService.sqlExecute(dto));
return Result.ok(apiConfigService.sqlExecute(dto)); // 封装返回结果并返回
}
@Operation(summary = "导出 api 文档")
/**
* idAPI
* @param ids APIid
* @param response HTTP
*/
@PostMapping(value = "/export-docs")
@Operation(summary = "导出API文档")
public void exportDocs(@RequestBody List<Long> ids, HttpServletResponse response) {
apiConfigService.exportDocs(ids, response);
apiConfigService.exportDocs(ids, response); // 调用service层方法导出API文档
}
}

View File

@ -28,42 +28,66 @@ import java.util.List;
@AllArgsConstructor
public class ApiGroupController {
private final ApiGroupService apiGroupService;
/**
*
* @return
*/
@GetMapping("api-group")
@Operation(summary = "查询文件分组树")
public Result<List<TreeNodeVo>> listTree() {
return Result.ok(apiGroupService.listTree());
return Result.ok(apiGroupService.listTree()); // 调用service层方法查询文件分组树并返回结果
}
/**
* id
* @param id id
* @return id
*/
@GetMapping("{id}")
@Operation(summary = "信息")
@PreAuthorize("hasAuthority('data-service:api-group:info')")
public Result<ApiGroupVo> get(@PathVariable("id") Long id){
ApiGroupEntity entity = apiGroupService.getById(id);
return Result.ok(ApiGroupConvert.INSTANCE.convert(entity));
ApiGroupEntity entity = apiGroupService.getById(id); // 根据id获取文件分组实体对象
return Result.ok(ApiGroupConvert.INSTANCE.convert(entity)); // 封装返回结果并返回
}
/**
*
* @param vo
* @return
*/
@PostMapping
@Operation(summary = "保存")
@PreAuthorize("hasAuthority('data-service:api-group:save')")
public Result<String> save(@RequestBody ApiGroupVo vo) {
apiGroupService.save(vo);
return Result.ok();
apiGroupService.save(vo); // 调用service层方法保存文件分组信息
return Result.ok(); // 封装返回结果并返回
}
/**
*
* @param vo
* @return
*/
@PutMapping
@Operation(summary = "修改")
@PreAuthorize("hasAuthority('data-service:api-group:update')")
public Result<String> update(@RequestBody @Valid ApiGroupVo vo) {
apiGroupService.update(vo);
return Result.ok();
apiGroupService.update(vo); // 调用service层方法修改文件分组信息
return Result.ok(); // 封装返回结果并返回
}
/**
*
* @param id id
* @return
*/
@DeleteMapping("/{id}")
@Operation(summary = "删除")
@PreAuthorize("hasAuthority('data-service:api-group:delete')")
public Result<String> delete(@PathVariable Long id) {
apiGroupService.delete(id);
return Result.ok();
apiGroupService.delete(id); // 调用service层方法删除文件分组信息
return Result.ok(); // 封装返回结果并返回
}
}

View File

@ -81,4 +81,5 @@ public class DataServiceAppController {
dataServiceAppService.cancelAuth(authId);
return Result.ok();
}
}

View File

@ -12,37 +12,164 @@ import java.util.Date;
@Data
public class ApiConfigDto implements Serializable {
private static final long serialVersionUID = 1L;
/**
* id
*/
private Long id;
/**
* id
*/
private Long groupId;
/**
*
*/
private String path;
/**
* sqlhttp
*/
private String type;
/**
*
*/
private String name;
/**
*
*/
private String note;
/**
* SQL
*/
private String sqlText;
/**
* SQL
*/
private String sqlSeparator;
/**
* SQL
*/
private Integer sqlMaxRow;
/**
* SQL
*/
private String sqlParam;
/**
* JSON
*/
private String jsonParam;
/**
* HTTP
*/
private String responseResult;
/**
*
*/
private String contentType;
/**
* 10
*/
private Integer status;
/**
*
*/
private Date releaseTime;
/**
* id
*/
private Long releaseUserId;
/**
*
*/
private Integer sqlDbType;
/**
* id
*/
private Long databaseId;
/**
* 10
*/
private Integer privates;
/**
* 10
*/
private Integer openTrans;
/**
* id
*/
private Long projectId;
/**
*
*/
private Integer version;
/**
* 10
*/
private Integer deleted;
/**
* id
*/
private Long creator;
/**
*
*/
private Date createTime;
/**
* id
*/
private Long updater;
/**
*
*/
private Date updateTime;
/**
*
*/
private Integer requestedTimes;
/**
*
*/
private Integer requestedSuccessTimes;
/**
*
*/
private Integer requestedFailedTimes;
/**
* id
*/
private Long authId;
/**
*
*/
private String group;
}

View File

@ -10,8 +10,24 @@ import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
public class AppToken implements Serializable {
/**
* id
*/
private Long appId;
/**
*
*/
private String appKey;
/**
* 访
*/
private String token;
/**
*
*/
private Long expireAt;
}

View File

@ -4,12 +4,44 @@ import lombok.Data;
@Data
public class SqlDto {
/**
*
*/
private Integer sqlDbType;
/**
* id
*/
private Long projectId;
/**
* SQL
*/
private String statement;
/**
* SQL
*/
private String sqlSeparator;
/**
* id
*/
private Long databaseId;
/**
* 10
*/
private Integer openTrans;
/**
* JSON
*/
private String jsonParams;
/**
* SQL
*/
private Integer sqlMaxRow;
}

View File

@ -43,6 +43,10 @@ public class ApiConfigServiceImpl extends BaseServiceImpl<ApiConfigDao, ApiConfi
private final DiscoveryClient discoveryClient;
private final ApiConfigDao apiConfigDao;
private final Map<String, ApiConfigEntity> mappings = new ConcurrentHashMap<>();
/**
* IP
* @return IP
*/
@Override
public String getIpPort() {
List<ServiceInstance> instances = discoveryClient.getInstances(ServerNames.GATEWAY_SERVER_NAME);
@ -51,7 +55,10 @@ public class ApiConfigServiceImpl extends BaseServiceImpl<ApiConfigDao, ApiConfi
/**
* API线
* @param id APIID
*/
public void online(Long id) {
ApiConfigEntity apiConfigEntity = apiConfigDao.getById(id);
if (apiConfigEntity != null) {
@ -93,36 +100,41 @@ public class ApiConfigServiceImpl extends BaseServiceImpl<ApiConfigDao, ApiConfi
private String getRouteKey(String type, String path) {
return type.toUpperCase() + "_" + path.toLowerCase();
}
/**
* API线
* @param id APIID
*/
@Override
public void offline(Long id) { // 修正参数类型为 Long id
public void offline(Long id) {
// 根据id获取API配置实体对象
ApiConfigEntity apiConfigEntity = apiConfigDao.getById(id);
if (apiConfigEntity != null) {
// 将API配置实体对象的状态设置为下线
apiConfigEntity.setStatus(0);
apiConfigEntity.setReleaseTime(null);
apiConfigEntity.setReleaseUserId(null);
// 更新API配置实体对象
apiConfigDao.updateById(apiConfigEntity);
} else {
// 如果没有找到对应的API配置实体对象抛出404异常
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Resource not found");
}
}
// @Override
// public void online(Long id) {
// ApiConfigEntity apiConfigEntity = new ApiConfigEntity();
// apiConfigEntity.setId(id);
// apiConfigEntity.setStatus(1);
// apiConfigEntity.setReleaseTime(new Date());
// apiConfigEntity.setReleaseUserId(SecurityUser.getUserId());
//
// apiConfigDao.updateById(apiConfigEntity);
// }
/**
* IP
* @return IP
*/
@Override
public String ipPort() {
// 获取网关服务实例列表
List<ServiceInstance> instances = discoveryClient.getInstances(ServerNames.GATEWAY_SERVER_NAME);
// 获取第一个网关服务实例的主机和端口,并返回拼接后的字符串
return instances.get(0).getHost() + ":" + instances.get(0).getPort();
}
@Override
public JdbcSelectResult sqlExecute(SqlDto dto) {
return null;
@ -168,12 +180,21 @@ public class ApiConfigServiceImpl extends BaseServiceImpl<ApiConfigDao, ApiConfi
}
}
/**
* API
* @param query
* @return API
*/
@Override
public PageResult<ApiConfigVo> page(ApiConfigQuery query) {
// 执行分页查询,并获取分页对象
IPage<ApiConfigEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
return new PageResult<>(ApiConfigConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
// 将实体对象列表转换为VO对象列表
List<ApiConfigVo> apiConfigVos = ApiConfigConvert.INSTANCE.convertList(page.getRecords());
// 返回VO对象列表和总记录数构成的分页结果对象
return new PageResult<>(apiConfigVos, page.getTotal());
}
private LambdaQueryWrapper getWrapper(ApiConfigQuery query) {
LambdaQueryWrapper<ApiConfigEntity> wrapper = Wrappers.lambdaQuery();
wrapper.like(StringUtil.isNotBlank(query.getName()), ApiConfigEntity::getName, query.getName());
@ -190,17 +211,32 @@ public class ApiConfigServiceImpl extends BaseServiceImpl<ApiConfigDao, ApiConfi
return wrapper;
}
/**
* API
* @param vo APIVO
*/
@Override
public void save(ApiConfigVo vo) {
// 将VO对象转换为实体对象
ApiConfigEntity entity = ApiConfigConvert.INSTANCE.convert(vo);
// 设置项目ID
entity.setProjectId(getProjectId());
// 执行插入操作
baseMapper.insert(entity);
}
/**
* API
* @param vo APIVO
*/
@Override
public void update(ApiConfigVo vo) {
// 将VO对象转换为实体对象
ApiConfigEntity entity = ApiConfigConvert.INSTANCE.convert(vo);
// 设置项目ID
entity.setProjectId(getProjectId());
// 执行更新操作
updateById(entity);
}
@ -209,31 +245,52 @@ public class ApiConfigServiceImpl extends BaseServiceImpl<ApiConfigDao, ApiConfi
removeByIds(idList);
}
/**
* API
* @param query
* @return API
*/
@Override
public PageResult<ApiConfigVo> pageResource(ApiConfigQuery query) {
// 查询参数
// 获取查询参数
Map<String, Object> params = getParams(query);
// 获取分页对象
IPage<ApiConfigEntity> page = getPage(query);
params.put(Constant.PAGE, page);
// 数据列表
// 查询数据列表
List<ApiConfigEntity> list = baseMapper.getResourceList(params);
// 将数据列表转换为VO对象列表
List<ApiConfigVo> apiConfigVos = ApiConfigConvert.INSTANCE.convertList(list);
// 遍历VO对象列表设置所属组信息
for (ApiConfigVo apiConfigVo : apiConfigVos) {
ApiGroupEntity groupEntity = apiGroupDao.selectById(apiConfigVo.getGroupId());
apiConfigVo.setGroup(groupEntity != null ? groupEntity.getPath() : null);
}
// 返回VO对象列表和总记录数构成的分页结果对象
return new PageResult<>(apiConfigVos, page.getTotal());
}
/**
* IDAPI
* @param id ID
* @return API
*/
@Override
public List<ApiConfigEntity> listActiveByGroupId(Long id) {
// 创建LambdaQueryWrapper对象
LambdaQueryWrapper<ApiConfigEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ApiConfigEntity::getStatus, 1).eq(ApiConfigEntity::getGroupId, id).orderByDesc(ApiConfigEntity::getId);
// 设置查询条件状态为1有效组ID为指定ID并按照ID降序排序
wrapper.eq(ApiConfigEntity::getStatus, 1)
.eq(ApiConfigEntity::getGroupId, id)
.orderByDesc(ApiConfigEntity::getId);
// 应用数据范围控制排除组织ID
dataScopeWithoutOrgId(wrapper);
// 执行查询并返回结果列表
return baseMapper.selectList(wrapper);
}
private Map<String, Object> getParams(ApiConfigQuery query) {
Map<String, Object> params = new HashMap<>();
params.put("ifMarket", query.getIfMarket());

View File

@ -25,43 +25,78 @@ import java.util.List;
@AllArgsConstructor
public class ApiGroupServiceImpl extends BaseServiceImpl<ApiGroupDao, ApiGroupEntity> implements ApiGroupService{
private final ApiConfigService apiConfigService;
/**
* API
* @return API
*/
@Override
public List<TreeNodeVo> listTree() {
// 查询所有API分组实体对象并转换为树节点VO对象列表
List<TreeNodeVo> treeNodeVos = getTreeNodeVos();
// 构建树形结构,并返回根节点列表
return BuildTreeUtils.buildTree(treeNodeVos);
}
/**
* APIVO
* @return APIVO
*/
private List<TreeNodeVo> getTreeNodeVos() {
LambdaQueryWrapper<ApiGroupEntity> wrapper = new LambdaQueryWrapper<>();
dataScopeWithoutOrgId(wrapper);
wrapper.orderByAsc(ApiGroupEntity::getOrderNo);
dataScopeWithoutOrgId(wrapper); // 加入数据权限过滤
wrapper.orderByAsc(ApiGroupEntity::getOrderNo); // 按orderNo升序排序
List<ApiGroupEntity> apiGroupEntities = baseMapper.selectList(wrapper);
// 将实体对象列表转换为VO对象列表
return BeanUtil.copyListProperties(apiGroupEntities, TreeNodeVo::new, (oldItem, newItem) -> {
// 设置节点名称
newItem.setLabel(oldItem.getName());
// 设置节点值
newItem.setValue(oldItem.getId());
newItem.setDisabled(oldItem.getType() == 1);
if (newItem.getPath().contains("/")) {
newItem.setDisabled(oldItem.getType() == 1); // 如果是虚拟节点,设置禁用状态
if (newItem.getPath().contains("/")) { // 设置父级节点路径
newItem.setParentPath(newItem.getPath().substring(0, newItem.getPath().lastIndexOf("/")));
}
});
}
/**
* API
* @param vo APIVO
*/
@Override
public void save(ApiGroupVo vo) {
// 将VO对象转换为实体对象
ApiGroupEntity entity = ApiGroupConvert.INSTANCE.convert(vo);
// 递归生成路径
entity.setPath(recursionPath(entity, null));
// 设置项目ID
entity.setProjectId(getProjectId());
// 执行插入操作
baseMapper.insert(entity); // 使用 insertSelective() 方法进行插入操作
}
/**
* API
* @param vo APIVO
*/
@Override
public void update(ApiGroupVo vo) {
// 将VO对象转换为实体对象
ApiGroupEntity entity = ApiGroupConvert.INSTANCE.convert(vo);
// 递归生成路径
entity.setPath(recursionPath(entity, null));
// 设置项目ID
entity.setProjectId(getProjectId());
// 执行更新操作
updateById(entity);
}
/**
* API
* @param groupEntity
* @param path
* @return
*/
private String recursionPath(ApiGroupEntity groupEntity, String path) {
if (StringUtil.isBlank(path)) {
path = groupEntity.getName();
@ -74,22 +109,28 @@ public class ApiGroupServiceImpl extends BaseServiceImpl<ApiGroupDao, ApiGroupEn
return path;
}
/**
* IDAPI
* @param id APIID
*/
@Override
public void delete(Long id) {
//查询有没有子节点
// 查询是否存在子节点
LambdaQueryWrapper<ApiGroupEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ApiGroupEntity::getParentId, id).last(" limit 1");
ApiGroupEntity one = baseMapper.selectOne(wrapper);
if (one != null) {
throw new ServerException("存在子节点,不允许删除!");
}
//查询有没有api与之关联
// 查询是否有API配置与之关联
LambdaQueryWrapper<ApiConfigEntity> serviceApiConfigWrapper = new LambdaQueryWrapper<>();
serviceApiConfigWrapper.eq(ApiConfigEntity::getGroupId, id).last(" limit 1");
ApiConfigEntity apiConfigEntity = apiConfigService.getOne(serviceApiConfigWrapper);
if (apiConfigEntity != null) {
throw new ServerException("节点下有 api 与之关联,不允许删除!");
}
// 执行删除操作
removeById(id);
}
}

View File

@ -14,8 +14,8 @@ spring:
nacos:
discovery:
server-addr: 101.34.77.101:8848
# 命名空间默认public
namespace: 09dff3e2-9790-4d4f-beb6-9baeb01ae040
# 命名空间默认public
namespace: a60b2ca1-a8c6-47ae-94f1-741105674655
service: ${spring.application.name}
group: srt2.0
config: