From 2159721fb3897eaf0e3cc5ead8088b4142712464 Mon Sep 17 00:00:00 2001 From: xcsnx <1192709687@qq.com> Date: Wed, 9 Oct 2024 22:17:15 +0800 Subject: [PATCH] [type:feat]Add plugin of namespace (#5677) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add * add * add * fix cr --------- Co-authored-by: ‘xcsnx’ <‘1192709687@qq.com’> Co-authored-by: moremind --- .../controller/NamespacePluginController.java | 18 +++++++++++++++ .../mapper/NamespacePluginRelMapper.java | 8 +++++++ .../model/entity/NamespacePluginRelDO.java | 23 +++++++++++++++++++ .../admin/service/NamespacePluginService.java | 11 ++++++++- .../impl/NamespacePluginServiceImpl.java | 22 ++++++++++++++++-- .../mappers/namespace-plugin-rel-sqlmap.xml | 12 ++-------- .../common/constant/AdminConstants.java | 5 ++++ 7 files changed, 86 insertions(+), 13 deletions(-) diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/NamespacePluginController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/NamespacePluginController.java index 32f2463c53a6..81c0ea0a016e 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/NamespacePluginController.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/NamespacePluginController.java @@ -23,6 +23,7 @@ import org.apache.shenyu.admin.aspect.annotation.RestApi; import org.apache.shenyu.admin.mapper.NamespaceMapper; import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper; +import org.apache.shenyu.admin.mapper.PluginMapper; import org.apache.shenyu.admin.model.dto.BatchCommonDTO; import org.apache.shenyu.admin.model.dto.BatchNamespaceCommonDTO; import org.apache.shenyu.admin.model.dto.NamespacePluginDTO; @@ -130,6 +131,23 @@ public ShenyuAdminResult updatePlugin(@Existed(message = "namespace plugin relat return ShenyuAdminResult.success(namespacePluginService.update(namespacePluginDTO)); } + /** + * add plugin of namespace. + * + * @param namespaceId namespaceId. + * @param pluginId pluginId. + * @return {@linkplain ShenyuAdminResult} + */ + @PutMapping("/{namespaceId}/{pluginId}") + @RequiresPermissions("system:plugin:edit") + public ShenyuAdminResult addPlugin(@Existed(message = "namespace is not exist", provider = NamespaceMapper.class) + @PathVariable("namespaceId") final String namespaceId, + @Existed(message = "plugin is not exist", provider = PluginMapper.class) + @PathVariable("pluginId") final String pluginId) { + return ShenyuAdminResult.success(namespacePluginService.create(namespaceId, pluginId)); + } + + /** * delete plugins of namespace. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java index c565156ddd2f..80dd147209eb 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java @@ -153,4 +153,12 @@ public interface NamespacePluginRelMapper extends ExistProvider { * @return the count of enabled datas */ int updateEnableByIdList(@Param("idList") List idList, @Param("enabled") Boolean enabled); + + /** + * insert selective plugin. + * + * @param namespacePluginRelDO {@linkplain NamespacePluginRelDO} + * @return rows int + */ + int insertSelective(NamespacePluginRelDO namespacePluginRelDO); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/NamespacePluginRelDO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/NamespacePluginRelDO.java index a857a134f0b1..08d2700d68ed 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/NamespacePluginRelDO.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/NamespacePluginRelDO.java @@ -184,6 +184,29 @@ public static NamespacePluginRelDO buildNamespacePluginRelDO(final NamespacePlug }).orElse(null); } + /** + * build buildNamespacePluginRelDO. + * + * @param pluginDO {@linkplain PluginDO} + * @param namespaceId {@linkplain String} + * @return {@linkplain NamespacePluginRelDO} + */ + public static NamespacePluginRelDO buildNamespacePluginRelDO(final PluginDO pluginDO, final String namespaceId) { + return Optional.ofNullable(pluginDO).map(item -> { + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + return NamespacePluginRelDO.builder() + .id(item.getId()) + .config(item.getConfig()) + .enabled(item.getEnabled()) + .sort(item.getSort()) + .namespaceId(namespaceId) + .pluginId(item.getId()) + .dateUpdated(currentTime) + .dateCreated(currentTime) + .build(); + }).orElse(null); + } + @Override public int hashCode() { return Objects.hash(super.hashCode(), namespaceId, pluginId, config, enabled, sort); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java index 32b7ad17d9ad..e1916ff3230e 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java @@ -44,7 +44,16 @@ public interface NamespacePluginService extends PageService SELECT - npr.id AS id, - npr.namespace_id AS namespaceId, - npr.plugin_id AS pluginId, - npr.config AS config, - npr.sort AS sort, - npr.enabled AS enabled, - npr.date_created AS dateCreated, - npr.date_updated AS dateUpdated - FROM namespace_plugin_rel npr - WHERE npr.id IN + + WHERE id IN #{id, jdbcType=VARCHAR} diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java index 526c14a03b08..c33f0cec921e 100644 --- a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java +++ b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java @@ -57,6 +57,11 @@ public final class AdminConstants { */ public static final String SYS_PLUGIN_ID_NOT_EXIST = "The plugin(s) does not exist!"; + /** + * The constant NAMESPACE_PLUGIN_EXIST. + */ + public static final String NAMESPACE_PLUGIN_EXIST = "The plugin of namespace is exist!"; + /** * The constant SYS_NAMESPACE_ID_NOT_EXIST. */