This is an automated email from the ASF dual-hosted git repository. hefengen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new 2159721fb3 [type:feat]Add plugin of namespace (#5677) 2159721fb3 is described below commit 2159721fb3897eaf0e3cc5ead8088b4142712464 Author: xcsnx <1192709...@qq.com> AuthorDate: Wed Oct 9 22:17:15 2024 +0800 [type:feat]Add plugin of namespace (#5677) * add * add * add * fix cr --------- Co-authored-by: ‘xcsnx’ <‘1192709...@qq.com’> Co-authored-by: moremind <hefen...@apache.org> --- .../controller/NamespacePluginController.java | 18 +++++++++++++++++ .../admin/mapper/NamespacePluginRelMapper.java | 8 ++++++++ .../admin/model/entity/NamespacePluginRelDO.java | 23 ++++++++++++++++++++++ .../admin/service/NamespacePluginService.java | 11 ++++++++++- .../service/impl/NamespacePluginServiceImpl.java | 22 +++++++++++++++++++-- .../mappers/namespace-plugin-rel-sqlmap.xml | 12 ++--------- .../shenyu/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 32f2463c53..81c0ea0a01 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.commons.lang3.StringUtils; 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 class NamespacePluginController implements PagedController<NamespacePlugi 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 c565156ddd..80dd147209 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<String> 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 a857a134f0..08d2700d68 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 final class NamespacePluginRelDO extends BaseDO { }).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 32b7ad17d9..e1916ff323 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<NamespacePluginQuery NamespacePluginVO findById(String id); /** - * Create or update string. + * Update string. + * + * @param namespaceId namespaceId. + * @param pluginId pluginId. + * @return the string + */ + String create(String namespaceId, String pluginId); + + /** + * Create string. * * @param namespacePluginDTO the plugin namespace dto * @return the string diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java index 686d5de042..3ac7db7b17 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java @@ -21,9 +21,11 @@ import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper; +import org.apache.shenyu.admin.mapper.PluginMapper; import org.apache.shenyu.admin.model.dto.PluginDTO; import org.apache.shenyu.admin.model.dto.NamespacePluginDTO; import org.apache.shenyu.admin.model.entity.NamespacePluginRelDO; +import org.apache.shenyu.admin.model.entity.PluginDO; import org.apache.shenyu.admin.model.page.CommonPager; import org.apache.shenyu.admin.model.page.PageResultUtils; import org.apache.shenyu.admin.model.query.NamespacePluginQuery; @@ -57,19 +59,35 @@ public class NamespacePluginServiceImpl implements NamespacePluginService { private final NamespacePluginEventPublisher namespacePluginEventPublisher; + private final PluginMapper pluginMapper; + public NamespacePluginServiceImpl(final NamespacePluginRelMapper namespacePluginRelMapper, final PluginHandleService pluginHandleService, - final NamespacePluginEventPublisher namespacePluginEventPublisher) { + final NamespacePluginEventPublisher namespacePluginEventPublisher, + final PluginMapper pluginMapper) { this.namespacePluginRelMapper = namespacePluginRelMapper; this.pluginHandleService = pluginHandleService; this.namespacePluginEventPublisher = namespacePluginEventPublisher; + this.pluginMapper = pluginMapper; } @Override public NamespacePluginVO findById(final String id) { return namespacePluginRelMapper.selectById(id); } - + + @Override + public String create(final String namespaceId, final String pluginId) { + NamespacePluginVO namespacePluginVO = namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginId, namespaceId); + if (!Objects.isNull(namespacePluginVO)) { + return AdminConstants.NAMESPACE_PLUGIN_EXIST; + } + PluginDO pluginDO = pluginMapper.selectById(pluginId); + NamespacePluginRelDO namespacePluginRelDO = NamespacePluginRelDO.buildNamespacePluginRelDO(pluginDO, namespaceId); + namespacePluginRelMapper.insertSelective(namespacePluginRelDO); + return ShenyuResultMessage.CREATE_SUCCESS; + } + @Override @Transactional(rollbackFor = Exception.class) public String update(final NamespacePluginDTO namespacePluginDTO) { diff --git a/shenyu-admin/src/main/resources/mappers/namespace-plugin-rel-sqlmap.xml b/shenyu-admin/src/main/resources/mappers/namespace-plugin-rel-sqlmap.xml index 6a5b886bcf..afd63abbb2 100644 --- a/shenyu-admin/src/main/resources/mappers/namespace-plugin-rel-sqlmap.xml +++ b/shenyu-admin/src/main/resources/mappers/namespace-plugin-rel-sqlmap.xml @@ -226,16 +226,8 @@ <select id="selectByIds" parameterType="java.util.List" resultType="org.apache.shenyu.admin.model.vo.NamespacePluginVO"> 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 + <include refid="Base_Column_List"/> + WHERE id IN <foreach item="id" collection="ids" open="(" separator="," close=")"> #{id, jdbcType=VARCHAR} </foreach> 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 526c14a03b..c33f0cec92 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. */