From 6f0d13f4ee7c2f1f2360bbbae38cde5d5b733843 Mon Sep 17 00:00:00 2001 From: xcsnx <1192709687@qq.com> Date: Thu, 14 Nov 2024 22:27:29 +0800 Subject: [PATCH] [type:fix]fix namespace delete (#5740) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix * fix * fix * fix * fix * fix --------- Co-authored-by: ‘xcsnx’ <‘1192709687@qq.com’> Co-authored-by: aias00 Co-authored-by: aias00 --- .../shenyu/admin/mapper/AuthPathMapper.java | 16 ++++ .../shenyu/admin/mapper/DiscoveryMapper.java | 8 ++ .../shenyu/admin/mapper/MetaDataMapper.java | 8 ++ .../mapper/NamespacePluginRelMapper.java | 17 ++-- .../shenyu/admin/mapper/RuleMapper.java | 8 ++ .../shenyu/admin/mapper/SelectorMapper.java | 8 ++ .../impl/NamespacePluginServiceImpl.java | 4 +- .../service/impl/NamespaceServiceImpl.java | 83 ++++++++++++++++--- .../resources/mappers/auth-path-sqlmap.xml | 17 ++++ .../resources/mappers/discovery-sqlmap.xml | 10 +++ .../resources/mappers/meta-data-sqlmap.xml | 10 +++ .../mappers/namespace-plugin-rel-sqlmap.xml | 45 ++++++++++ .../main/resources/mappers/rule-sqlmap.xml | 10 +++ .../resources/mappers/selector-sqlmap.xml | 10 +++ 14 files changed, 230 insertions(+), 24 deletions(-) diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/AuthPathMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/AuthPathMapper.java index 3a92625612da..8c0ba91d1116 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/AuthPathMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/AuthPathMapper.java @@ -79,6 +79,22 @@ public interface AuthPathMapper extends ExistProvider { * @return the list */ List findByAuthId(String authId); + + /** + * Find by namespace id list. + * + * @param namespaceId the namespaceId + * @return the list + */ + List findByNamespaceId(String namespaceId); + + /** + * Find by namespace id list. + * + * @param namespaceIds the namespaceIds + * @return the list + */ + List findByNamespaceIds(List namespaceIds); /** * find all the {@link AuthPathDO} by authIdList. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/DiscoveryMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/DiscoveryMapper.java index a4100af1b360..d0f7c63c3d33 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/DiscoveryMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/DiscoveryMapper.java @@ -63,6 +63,14 @@ public interface DiscoveryMapper extends ExistProvider { */ List selectAllByNamespaceId(String namespaceId); + /** + * selectAllByNamespaceIds. + * + * @param namespaceIds the namespaceIds + * @return discoveryDOList + */ + List selectAllByNamespaceIds(List namespaceIds); + /** * select discovery by plugin name and level. * diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java index 723cdd815628..e4f33fa26640 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java @@ -72,6 +72,14 @@ public interface MetaDataMapper extends ExistProvider { * @return the list */ List findAllByNamespaceId(String namespaceId); + + /** + * Find all list. + * + * @param namespaceIds the namespaceIds + * @return the list + */ + List findAllByNamespaceIds(List namespaceIds); /** * Find meta data do by path and namespaceId. 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 bcddba5f0a2f..81cd5c292153 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 @@ -137,21 +137,16 @@ public interface NamespacePluginRelMapper extends ExistProvider { * @return {@linkplain List} */ List selectAllByNamespaceId(String namespaceId); - - /** - * select all. - * - * @return {@linkplain List} - */ - List selectAllByNamespaceId(); - + /** - * select all by namespaceId. + * select all by namespaceId list. * - * @param namespaceId namespaceId. + * @param namespaceIds namespaceIds. * @return {@linkplain List} */ - List selectByNamespaceId(String namespaceId); + List selectAllByNamespaceIds(List namespaceIds); + + List selectAll(); /** * enable data by a list of ids. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/RuleMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/RuleMapper.java index bce950e7a2e9..39384cf49435 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/RuleMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/RuleMapper.java @@ -163,6 +163,14 @@ public interface RuleMapper extends ExistProvider { */ List selectAllByNamespaceId(String namespaceId); + /** + * list all. + * + * @param namespaceIds the namespaceIds + * @return {@linkplain List} + */ + List selectAllByNamespaceIds(List namespaceIds); + /** * search by condition. * diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java index fb29e6672422..f1072eab1cfc 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java @@ -194,6 +194,14 @@ SelectorDO findByNameAndPluginIdAndNamespaceId(@Param("name") String name, */ List selectAllByNamespaceId(String namespaceId); + /** + * list all by namespaceId list. + * + * @param namespaceIds the namespaceIds + * @return {@linkplain List} + */ + List selectAllByNamespaceIds(List namespaceIds); + /** * select by condition. * 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 77bff91335c9..474f52cedc34 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 @@ -130,12 +130,12 @@ public List listAll(final String namespaceId) { @Override public List listAll() { - return ListUtil.map(namespacePluginRelMapper.selectAllByNamespaceId(), PluginTransfer.INSTANCE::mapToData); + return ListUtil.map(namespacePluginRelMapper.selectAll(), PluginTransfer.INSTANCE::mapToData); } @Override public List listByNamespaceId(final String namespaceId) { - return namespacePluginRelMapper.selectByNamespaceId(namespaceId); + return namespacePluginRelMapper.selectAllByNamespaceId(namespaceId); } @Override diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java index e0176198b133..d10a27cff441 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java @@ -21,13 +21,25 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.admin.exception.ShenyuAdminException; +import org.apache.shenyu.admin.mapper.AuthPathMapper; +import org.apache.shenyu.admin.mapper.DiscoveryMapper; +import org.apache.shenyu.admin.mapper.MetaDataMapper; import org.apache.shenyu.admin.mapper.NamespaceMapper; +import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper; +import org.apache.shenyu.admin.mapper.RuleMapper; +import org.apache.shenyu.admin.mapper.SelectorMapper; import org.apache.shenyu.admin.model.dto.NamespaceDTO; +import org.apache.shenyu.admin.model.entity.AuthPathDO; +import org.apache.shenyu.admin.model.entity.DiscoveryDO; +import org.apache.shenyu.admin.model.entity.MetaDataDO; import org.apache.shenyu.admin.model.entity.NamespaceDO; +import org.apache.shenyu.admin.model.entity.RuleDO; +import org.apache.shenyu.admin.model.entity.SelectorDO; import org.apache.shenyu.admin.model.event.namespace.NamespaceCreatedEvent; import org.apache.shenyu.admin.model.page.CommonPager; import org.apache.shenyu.admin.model.page.PageResultUtils; import org.apache.shenyu.admin.model.query.NamespaceQuery; +import org.apache.shenyu.admin.model.vo.NamespacePluginVO; import org.apache.shenyu.admin.model.vo.NamespaceVO; import org.apache.shenyu.admin.service.NamespaceService; import org.apache.shenyu.admin.service.NamespaceUserService; @@ -51,17 +63,42 @@ public class NamespaceServiceImpl implements NamespaceService { private final NamespaceMapper namespaceMapper; - + private final NamespaceUserService namespaceUserService; - + private final NamespaceEventPublisher namespaceEventPublisher; + private final SelectorMapper selectorMapper; + + private final RuleMapper ruleMapper; + + private final AuthPathMapper authPathMapper; + + private final MetaDataMapper metaDataMapper; + + private final DiscoveryMapper discoveryMapper; + + private final NamespacePluginRelMapper namespacePluginRelMapper; + + public NamespaceServiceImpl(final NamespaceMapper namespaceMapper, final NamespaceUserService namespaceUserService, - final NamespaceEventPublisher namespaceEventPublisher) { + final NamespaceEventPublisher namespaceEventPublisher, + final NamespacePluginRelMapper namespacePluginRelMapper, + final SelectorMapper selectorMapper, + final RuleMapper ruleMapper, + final AuthPathMapper authPathMapper, + final MetaDataMapper metaDataMapper, + final DiscoveryMapper discoveryMapper) { this.namespaceMapper = namespaceMapper; this.namespaceUserService = namespaceUserService; this.namespaceEventPublisher = namespaceEventPublisher; + this.namespacePluginRelMapper = namespacePluginRelMapper; + this.selectorMapper = selectorMapper; + this.ruleMapper = ruleMapper; + this.authPathMapper = authPathMapper; + this.metaDataMapper = metaDataMapper; + this.discoveryMapper = discoveryMapper; } @Override @@ -87,11 +124,35 @@ public CommonPager listByPage(final NamespaceQuery namespaceQuery) @Override public String delete(final List ids) { if (ids.contains(Constants.DEFAULT_NAMESPACE_PRIMARY_KEY)) { - return AdminConstants.SYS_DEFAULT_NAMESPACE_ID_DELETE; + throw new ShenyuAdminException(AdminConstants.SYS_DEFAULT_NAMESPACE_ID_DELETE); } - List namespaceDOS = namespaceMapper.selectByIds(ids); - if (CollectionUtils.isEmpty(namespaceDOS)) { - return AdminConstants.SYS_NAMESPACE_ID_NOT_EXIST; + List namespaceIdList = namespaceMapper.selectByIds(ids).stream().map(NamespaceDO::getNamespaceId).collect(Collectors.toList()); + if (CollectionUtils.isEmpty(namespaceIdList)) { + throw new ShenyuAdminException(AdminConstants.SYS_NAMESPACE_ID_NOT_EXIST); + } + List namespacePluginVOS = namespacePluginRelMapper.selectAllByNamespaceIds(namespaceIdList); + if (CollectionUtils.isNotEmpty(namespacePluginVOS)) { + throw new ShenyuAdminException("Plugins exist under those namespace!"); + } + List selectorDOS = selectorMapper.selectAllByNamespaceIds(namespaceIdList); + if (CollectionUtils.isNotEmpty(selectorDOS)) { + throw new ShenyuAdminException("selector exist under those namespace!"); + } + List ruleDOList = ruleMapper.selectAllByNamespaceIds(namespaceIdList); + if (CollectionUtils.isNotEmpty(ruleDOList)) { + throw new ShenyuAdminException("rule exist under those namespace!"); + } + List metaDataDOList = metaDataMapper.findAllByNamespaceIds(namespaceIdList); + if (CollectionUtils.isNotEmpty(metaDataDOList)) { + throw new ShenyuAdminException("metaData exist under those namespace!"); + } + List authPathDOList = authPathMapper.findByNamespaceIds(namespaceIdList); + if (CollectionUtils.isNotEmpty(authPathDOList)) { + throw new ShenyuAdminException("authPath exist under those namespace!"); + } + List discoveryDOList = discoveryMapper.selectAllByNamespaceIds(namespaceIdList); + if (CollectionUtils.isNotEmpty(discoveryDOList)) { + throw new ShenyuAdminException("discovery exist under those namespace!"); } namespaceMapper.deleteByIds(ids); return ShenyuResultMessage.DELETE_SUCCESS; @@ -110,11 +171,11 @@ public NamespaceVO findByNamespaceId(final String namespaceId) { @Override public List list(final String name) { List namespaceIds = namespaceUserService.listNamespaceIdByUserId(SessionUtil.visitorId()); - + if (CollectionUtils.isEmpty(namespaceIds)) { return Lists.newArrayList(); } - + List namespaceDOS = namespaceMapper.selectByNamespaceIdsAndName(namespaceIds, name); if (CollectionUtils.isEmpty(namespaceDOS)) { return Lists.newArrayList(); @@ -142,9 +203,9 @@ private NamespaceVO create(final NamespaceDTO namespaceDTO) { .dateUpdated(currentTime) .build(); namespaceMapper.insert(namespaceDO); - + namespaceEventPublisher.publish(new NamespaceCreatedEvent(namespaceDO, SessionUtil.visitorId())); - + return NamespaceTransfer.INSTANCE.mapToVo(namespaceDO); } diff --git a/shenyu-admin/src/main/resources/mappers/auth-path-sqlmap.xml b/shenyu-admin/src/main/resources/mappers/auth-path-sqlmap.xml index ce7619e27b21..d33c02543be1 100644 --- a/shenyu-admin/src/main/resources/mappers/auth-path-sqlmap.xml +++ b/shenyu-admin/src/main/resources/mappers/auth-path-sqlmap.xml @@ -45,6 +45,23 @@ WHERE auth_id = #{authId, jdbcType=VARCHAR} + + + + + + + + + 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, + p.role AS role, + p.name AS name, + p.plugin_jar AS pluginJar + FROM namespace_plugin_rel npr + LEFT JOIN plugin p ON p.id = npr.plugin_id + AND npr.namespace_id=#{namespaceId} + ORDER BY + sort, + npr.id ASC + + + + + + + + +