From 0e28ab169c31cc79572832d6366cb5ea414e9197 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 10 Mar 2021 01:23:34 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96=20config=20=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-ui/src/views/infra/config/index.vue | 48 ++--- .../config/InfConfigController.java | 9 +- .../dal/mysql/config/InfConfigMapper.java | 19 +- .../service/config/InfConfigService.java | 44 ++--- .../config/impl/InfConfigServiceImpl.java | 42 ++--- .../service/config/InfConfigServiceTest.java | 164 +++++++++--------- 6 files changed, 150 insertions(+), 176 deletions(-) diff --git a/ruoyi-ui/src/views/infra/config/index.vue b/ruoyi-ui/src/views/infra/config/index.vue index 592a40f6b..6b880d24c 100644 --- a/ruoyi-ui/src/views/infra/config/index.vue +++ b/ruoyi-ui/src/views/infra/config/index.vue @@ -2,24 +2,12 @@
- + - + @@ -56,7 +44,7 @@ icon="el-icon-plus" size="mini" @click="handleAdd" - v-hasPermi="['infra:config:add']" + v-hasPermi="['infra:config:create']" >新增 @@ -95,31 +83,15 @@ - + diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java index 7731d3b9a..a0a9ec62f 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java @@ -3,7 +3,6 @@ package cn.iocoder.dashboard.modules.infra.controller.config; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils; -import cn.iocoder.dashboard.framework.idempotent.core.annotation.Idempotent; import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog; import cn.iocoder.dashboard.modules.infra.controller.config.vo.*; import cn.iocoder.dashboard.modules.infra.convert.config.InfConfigConvert; @@ -19,7 +18,6 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; - import java.io.IOException; import java.util.List; @@ -44,18 +42,17 @@ public class InfConfigController { return success(configService.createConfig(reqVO)); } - @ApiOperation("修改参数配置") @PutMapping("/update") + @ApiOperation("修改参数配置") @PreAuthorize("@ss.hasPermission('infra:config:update')") - @Idempotent(timeout = 60) public CommonResult updateConfig(@Valid @RequestBody InfConfigUpdateReqVO reqVO) { configService.updateConfig(reqVO); return success(true); } + @DeleteMapping("/delete") @ApiOperation("删除参数配置") @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) - @DeleteMapping("/delete") @PreAuthorize("@ss.hasPermission('infra:config:delete')") public CommonResult deleteConfig(@RequestParam("id") Long id) { configService.deleteConfig(id); @@ -70,9 +67,9 @@ public class InfConfigController { return success(InfConfigConvert.INSTANCE.convert(configService.getConfig(id))); } + @GetMapping(value = "/get-value-by-key") @ApiOperation(value = "根据参数键名查询参数值", notes = "敏感配置,不允许返回给前端") @ApiImplicitParam(name = "key", value = "参数键", required = true, example = "yunai.biz.username", dataTypeClass = String.class) - @GetMapping(value = "/get-value-by-key") public CommonResult getConfigKey(@RequestParam("key") String key) { InfConfigDO config = configService.getConfigByKey(key); if (config == null) { diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/mysql/config/InfConfigMapper.java b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/mysql/config/InfConfigMapper.java index 6a984448d..9357f9cce 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/dal/mysql/config/InfConfigMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/dal/mysql/config/InfConfigMapper.java @@ -14,20 +14,21 @@ import java.util.List; @Mapper public interface InfConfigMapper extends BaseMapperX { - default PageResult selectPage(InfConfigPageReqVO reqVO) { - return selectPage(reqVO, - new QueryWrapperX().likeIfPresent("name", reqVO.getName()) - .likeIfPresent("`key`", reqVO.getKey()) - .eqIfPresent("`type`", reqVO.getType()) - .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime())); - } - default InfConfigDO selectByKey(String key) { return selectOne(new QueryWrapper().eq("`key`", key)); } + default PageResult selectPage(InfConfigPageReqVO reqVO) { + return selectPage(reqVO, new QueryWrapperX() + .likeIfPresent("name", reqVO.getName()) + .likeIfPresent("`key`", reqVO.getKey()) + .eqIfPresent("`type`", reqVO.getType()) + .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime())); + } + default List selectList(InfConfigExportReqVO reqVO) { - return selectList(new QueryWrapperX().likeIfPresent("name", reqVO.getName()) + return selectList(new QueryWrapperX() + .likeIfPresent("name", reqVO.getName()) .likeIfPresent("`key`", reqVO.getKey()) .eqIfPresent("`type`", reqVO.getType()) .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime())); diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigService.java b/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigService.java index 9f768da4d..d7d120c19 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigService.java @@ -7,6 +7,7 @@ import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigPageReqV import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigUpdateReqVO; import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO; +import javax.validation.Valid; import java.util.List; /** @@ -17,20 +18,26 @@ import java.util.List; public interface InfConfigService { /** - * 获得参数配置分页列表 + * 创建参数配置 * - * @param reqVO 分页条件 - * @return 分页列表 + * @param reqVO 创建信息 + * @return 配置编号 */ - PageResult getConfigPage(InfConfigPageReqVO reqVO); + Long createConfig(@Valid InfConfigCreateReqVO reqVO); /** - * 获得参数配置列表 + * 更新参数配置 * - * @param reqVO 列表 - * @return 列表 + * @param reqVO 更新信息 */ - List getConfigList(InfConfigExportReqVO reqVO); + void updateConfig(@Valid InfConfigUpdateReqVO reqVO); + + /** + * 删除参数配置 + * + * @param id 配置编号 + */ + void deleteConfig(Long id); /** * 获得参数配置 @@ -49,25 +56,20 @@ public interface InfConfigService { InfConfigDO getConfigByKey(String key); /** - * 创建参数配置 + * 获得参数配置分页列表 * - * @param reqVO 创建信息 - * @return 配置编号 + * @param reqVO 分页条件 + * @return 分页列表 */ - Long createConfig(InfConfigCreateReqVO reqVO); + PageResult getConfigPage(@Valid InfConfigPageReqVO reqVO); /** - * 更新参数配置 + * 获得参数配置列表 * - * @param reqVO 更新信息 + * @param reqVO 列表 + * @return 列表 */ - void updateConfig(InfConfigUpdateReqVO reqVO); + List getConfigList(@Valid InfConfigExportReqVO reqVO); - /** - * 删除参数配置 - * - * @param id 配置编号 - */ - void deleteConfig(Long id); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java index 27e49771d..211ec52d2 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.infra.service.config.InfConfigService; import com.google.common.annotations.VisibleForTesting; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; @@ -27,6 +28,7 @@ import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.*; */ @Service @Slf4j +@Validated public class InfConfigServiceImpl implements InfConfigService { @Resource @@ -35,26 +37,6 @@ public class InfConfigServiceImpl implements InfConfigService { @Resource private InfConfigProducer configProducer; - @Override - public PageResult getConfigPage(InfConfigPageReqVO reqVO) { - return configMapper.selectPage(reqVO); - } - - @Override - public List getConfigList(InfConfigExportReqVO reqVO) { - return configMapper.selectList(reqVO); - } - - @Override - public InfConfigDO getConfig(Long id) { - return configMapper.selectById(id); - } - - @Override - public InfConfigDO getConfigByKey(String key) { - return configMapper.selectByKey(key); - } - @Override public Long createConfig(InfConfigCreateReqVO reqVO) { // 校验正确性 @@ -93,6 +75,26 @@ public class InfConfigServiceImpl implements InfConfigService { configProducer.sendConfigRefreshMessage(); } + @Override + public InfConfigDO getConfig(Long id) { + return configMapper.selectById(id); + } + + @Override + public InfConfigDO getConfigByKey(String key) { + return configMapper.selectByKey(key); + } + + @Override + public PageResult getConfigPage(InfConfigPageReqVO reqVO) { + return configMapper.selectPage(reqVO); + } + + @Override + public List getConfigList(InfConfigExportReqVO reqVO) { + return configMapper.selectList(reqVO); + } + private void checkCreateOrUpdate(Long id, String key) { // 校验自己存在 checkConfigExists(id); diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java index a01330527..12bcce62e 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java @@ -47,88 +47,6 @@ public class InfConfigServiceTest extends BaseDbUnitTest { @MockBean private InfConfigProducer configProducer; - @Test - public void testGetConfigPage() { - // mock 数据 - InfConfigDO dbConfig = randomInfConfigDO(o -> { // 等会查询到 - o.setName("芋艿"); - o.setKey("yunai"); - o.setType(InfConfigTypeEnum.SYSTEM.getType()); - o.setCreateTime(buildTime(2021, 2, 1)); - }); - configMapper.insert(dbConfig); - // 测试 name 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆"))); - // 测试 key 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou"))); - // 测试 type 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); - // 测试 createTime 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); - // 准备参数 - InfConfigPageReqVO reqVO = new InfConfigPageReqVO(); - reqVO.setName("艿"); - reqVO.setKey("nai"); - reqVO.setType(InfConfigTypeEnum.SYSTEM.getType()); - reqVO.setBeginTime(buildTime(2021, 1, 15)); - reqVO.setEndTime(buildTime(2021, 2, 15)); - - // 调用 - PageResult pageResult = configService.getConfigPage(reqVO); - // 断言 - assertEquals(1, pageResult.getTotal()); - assertEquals(1, pageResult.getList().size()); - assertPojoEquals(dbConfig, pageResult.getList().get(0)); - } - - @Test - public void testGetConfigList() { - // mock 数据 - InfConfigDO dbConfig = randomInfConfigDO(o -> { // 等会查询到 - o.setName("芋艿"); - o.setKey("yunai"); - o.setType(InfConfigTypeEnum.SYSTEM.getType()); - o.setCreateTime(buildTime(2021, 2, 1)); - }); - configMapper.insert(dbConfig); - // 测试 name 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆"))); - // 测试 key 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou"))); - // 测试 type 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); - // 测试 createTime 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); - // 准备参数 - InfConfigExportReqVO reqVO = new InfConfigExportReqVO(); - reqVO.setName("艿"); - reqVO.setKey("nai"); - reqVO.setType(InfConfigTypeEnum.SYSTEM.getType()); - reqVO.setBeginTime(buildTime(2021, 1, 15)); - reqVO.setEndTime(buildTime(2021, 2, 15)); - - // 调用 - List list = configService.getConfigList(reqVO); - // 断言 - assertEquals(1, list.size()); - assertPojoEquals(dbConfig, list.get(0)); - } - - @Test - public void testGetConfigByKey() { - // mock 数据 - InfConfigDO dbConfig = randomInfConfigDO(); - configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据 - // 准备参数 - String key = dbConfig.getKey(); - - // 调用 - InfConfigDO config = configService.getConfigByKey(key); - // 断言 - assertNotNull(config); - assertPojoEquals(dbConfig, config); - } - @Test public void testCreateConfig_success() { // 准备参数 @@ -243,6 +161,88 @@ public class InfConfigServiceTest extends BaseDbUnitTest { CONFIG_KEY_DUPLICATE); } + @Test + public void testGetConfigPage() { + // mock 数据 + InfConfigDO dbConfig = randomInfConfigDO(o -> { // 等会查询到 + o.setName("芋艿"); + o.setKey("yunai"); + o.setType(InfConfigTypeEnum.SYSTEM.getType()); + o.setCreateTime(buildTime(2021, 2, 1)); + }); + configMapper.insert(dbConfig); + // 测试 name 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆"))); + // 测试 key 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou"))); + // 测试 type 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); + // 测试 createTime 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); + // 准备参数 + InfConfigPageReqVO reqVO = new InfConfigPageReqVO(); + reqVO.setName("艿"); + reqVO.setKey("nai"); + reqVO.setType(InfConfigTypeEnum.SYSTEM.getType()); + reqVO.setBeginTime(buildTime(2021, 1, 15)); + reqVO.setEndTime(buildTime(2021, 2, 15)); + + // 调用 + PageResult pageResult = configService.getConfigPage(reqVO); + // 断言 + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(dbConfig, pageResult.getList().get(0)); + } + + @Test + public void testGetConfigList() { + // mock 数据 + InfConfigDO dbConfig = randomInfConfigDO(o -> { // 等会查询到 + o.setName("芋艿"); + o.setKey("yunai"); + o.setType(InfConfigTypeEnum.SYSTEM.getType()); + o.setCreateTime(buildTime(2021, 2, 1)); + }); + configMapper.insert(dbConfig); + // 测试 name 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆"))); + // 测试 key 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou"))); + // 测试 type 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); + // 测试 createTime 不匹配 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); + // 准备参数 + InfConfigExportReqVO reqVO = new InfConfigExportReqVO(); + reqVO.setName("艿"); + reqVO.setKey("nai"); + reqVO.setType(InfConfigTypeEnum.SYSTEM.getType()); + reqVO.setBeginTime(buildTime(2021, 1, 15)); + reqVO.setEndTime(buildTime(2021, 2, 15)); + + // 调用 + List list = configService.getConfigList(reqVO); + // 断言 + assertEquals(1, list.size()); + assertPojoEquals(dbConfig, list.get(0)); + } + + @Test + public void testGetConfigByKey() { + // mock 数据 + InfConfigDO dbConfig = randomInfConfigDO(); + configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据 + // 准备参数 + String key = dbConfig.getKey(); + + // 调用 + InfConfigDO config = configService.getConfigByKey(key); + // 断言 + assertNotNull(config); + assertPojoEquals(dbConfig, config); + } + // ========== 随机对象 ========== @SafeVarargs