Skip to content

Commit

Permalink
[type:fix]fix namespace delete (apache#5740)
Browse files Browse the repository at this point in the history
* fix

* fix

* fix

* fix

* fix

* fix

---------

Co-authored-by: ‘xcsnx’ <‘[email protected]’>
Co-authored-by: aias00 <[email protected]>
Co-authored-by: aias00 <[email protected]>
  • Loading branch information
4 people authored Nov 14, 2024
1 parent a1aee4c commit 6f0d13f
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ public interface AuthPathMapper extends ExistProvider {
* @return the list
*/
List<AuthPathDO> findByAuthId(String authId);

/**
* Find by namespace id list.
*
* @param namespaceId the namespaceId
* @return the list
*/
List<AuthPathDO> findByNamespaceId(String namespaceId);

/**
* Find by namespace id list.
*
* @param namespaceIds the namespaceIds
* @return the list
*/
List<AuthPathDO> findByNamespaceIds(List<String> namespaceIds);

/**
* find all the {@link AuthPathDO} by authIdList.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public interface DiscoveryMapper extends ExistProvider {
*/
List<DiscoveryDO> selectAllByNamespaceId(String namespaceId);

/**
* selectAllByNamespaceIds.
*
* @param namespaceIds the namespaceIds
* @return discoveryDOList
*/
List<DiscoveryDO> selectAllByNamespaceIds(List<String> namespaceIds);

/**
* select discovery by plugin name and level.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public interface MetaDataMapper extends ExistProvider {
* @return the list
*/
List<MetaDataDO> findAllByNamespaceId(String namespaceId);

/**
* Find all list.
*
* @param namespaceIds the namespaceIds
* @return the list
*/
List<MetaDataDO> findAllByNamespaceIds(List<String> namespaceIds);

/**
* Find meta data do by path and namespaceId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,16 @@ public interface NamespacePluginRelMapper extends ExistProvider {
* @return {@linkplain List}
*/
List<NamespacePluginVO> selectAllByNamespaceId(String namespaceId);

/**
* select all.
*
* @return {@linkplain List}
*/
List<NamespacePluginVO> selectAllByNamespaceId();


/**
* select all by namespaceId.
* select all by namespaceId list.
*
* @param namespaceId namespaceId.
* @param namespaceIds namespaceIds.
* @return {@linkplain List}
*/
List<NamespacePluginVO> selectByNamespaceId(String namespaceId);
List<NamespacePluginVO> selectAllByNamespaceIds(List<String> namespaceIds);

List<NamespacePluginVO> selectAll();

/**
* enable data by a list of ids.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ public interface RuleMapper extends ExistProvider {
*/
List<RuleDO> selectAllByNamespaceId(String namespaceId);

/**
* list all.
*
* @param namespaceIds the namespaceIds
* @return {@linkplain List}
*/
List<RuleDO> selectAllByNamespaceIds(List<String> namespaceIds);

/**
* search by condition.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ SelectorDO findByNameAndPluginIdAndNamespaceId(@Param("name") String name,
*/
List<SelectorDO> selectAllByNamespaceId(String namespaceId);

/**
* list all by namespaceId list.
*
* @param namespaceIds the namespaceIds
* @return {@linkplain List}
*/
List<SelectorDO> selectAllByNamespaceIds(List<String> namespaceIds);

/**
* select by condition.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ public List<PluginData> listAll(final String namespaceId) {

@Override
public List<PluginData> listAll() {
return ListUtil.map(namespacePluginRelMapper.selectAllByNamespaceId(), PluginTransfer.INSTANCE::mapToData);
return ListUtil.map(namespacePluginRelMapper.selectAll(), PluginTransfer.INSTANCE::mapToData);
}

@Override
public List<NamespacePluginVO> listByNamespaceId(final String namespaceId) {
return namespacePluginRelMapper.selectByNamespaceId(namespaceId);
return namespacePluginRelMapper.selectAllByNamespaceId(namespaceId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -87,11 +124,35 @@ public CommonPager<NamespaceVO> listByPage(final NamespaceQuery namespaceQuery)
@Override
public String delete(final List<String> 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<NamespaceDO> namespaceDOS = namespaceMapper.selectByIds(ids);
if (CollectionUtils.isEmpty(namespaceDOS)) {
return AdminConstants.SYS_NAMESPACE_ID_NOT_EXIST;
List<String> 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<NamespacePluginVO> namespacePluginVOS = namespacePluginRelMapper.selectAllByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(namespacePluginVOS)) {
throw new ShenyuAdminException("Plugins exist under those namespace!");
}
List<SelectorDO> selectorDOS = selectorMapper.selectAllByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(selectorDOS)) {
throw new ShenyuAdminException("selector exist under those namespace!");
}
List<RuleDO> ruleDOList = ruleMapper.selectAllByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(ruleDOList)) {
throw new ShenyuAdminException("rule exist under those namespace!");
}
List<MetaDataDO> metaDataDOList = metaDataMapper.findAllByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(metaDataDOList)) {
throw new ShenyuAdminException("metaData exist under those namespace!");
}
List<AuthPathDO> authPathDOList = authPathMapper.findByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(authPathDOList)) {
throw new ShenyuAdminException("authPath exist under those namespace!");
}
List<DiscoveryDO> discoveryDOList = discoveryMapper.selectAllByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(discoveryDOList)) {
throw new ShenyuAdminException("discovery exist under those namespace!");
}
namespaceMapper.deleteByIds(ids);
return ShenyuResultMessage.DELETE_SUCCESS;
Expand All @@ -110,11 +171,11 @@ public NamespaceVO findByNamespaceId(final String namespaceId) {
@Override
public List<NamespaceVO> list(final String name) {
List<String> namespaceIds = namespaceUserService.listNamespaceIdByUserId(SessionUtil.visitorId());

if (CollectionUtils.isEmpty(namespaceIds)) {
return Lists.newArrayList();
}

List<NamespaceDO> namespaceDOS = namespaceMapper.selectByNamespaceIdsAndName(namespaceIds, name);
if (CollectionUtils.isEmpty(namespaceDOS)) {
return Lists.newArrayList();
Expand Down Expand Up @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions shenyu-admin/src/main/resources/mappers/auth-path-sqlmap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@
WHERE auth_id = #{authId, jdbcType=VARCHAR}
</select>

<select id="findByNamespaceId" parameterType="java.lang.String" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM auth_path
WHERE namespace_id = #{namespaceId, jdbcType=VARCHAR}
</select>

<select id="findByNamespaceIds" parameterType="java.lang.String" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM auth_path
WHERE namespace_id IN
<foreach item="namespaceId" collection="namespaceIds" open="(" separator="," close=")">
#{namespaceId, jdbcType=VARCHAR}
</foreach>
</select>

<select id="findByAuthIdList" parameterType="java.lang.String" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
Expand Down
10 changes: 10 additions & 0 deletions shenyu-admin/src/main/resources/mappers/discovery-sqlmap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
WHERE namespace_id = #{namespaceId, jdbcType=VARCHAR}
</select>

<select id="selectAllByNamespaceIds" resultType="org.apache.shenyu.admin.model.entity.DiscoveryDO">
SELECT
<include refid="Base_Column_List"/>
FROM discovery
WHERE namespace_id IN
<foreach item="namespaceId" collection="namespaceIds" open="(" separator="," close=")">
#{namespaceId, jdbcType=VARCHAR}
</foreach>
</select>

<select id="selectBySelectorNameAndPluginName" resultType="org.apache.shenyu.admin.model.entity.DiscoveryDO">
SELECT
d.id,
Expand Down
10 changes: 10 additions & 0 deletions shenyu-admin/src/main/resources/mappers/meta-data-sqlmap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@
WHERE namespace_id = #{namespaceId, jdbcType=VARCHAR}
</select>

<select id="findAllByNamespaceIds" resultMap="BaseResultMap">
SElECT
<include refid="Base_Column_List"/>
FROM meta_data
WHERE namespace_id IN
<foreach item="namespaceId" collection="namespaceIds" open="(" separator="," close=")">
#{namespaceId, jdbcType=VARCHAR}
</foreach>
</select>


<select id="selectByQuery" parameterType="org.apache.shenyu.admin.model.query.MetaDataQuery"
resultMap="BaseResultMap">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,51 @@
</delete>

<select id="selectAllByNamespaceId" 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,
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
</select>

<select id="selectAllByNamespaceIds" 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,
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 IN
<foreach item="namespaceId" collection="namespaceIds" open="(" separator="," close=")">
#{namespaceId, jdbcType=VARCHAR}
</foreach>
ORDER BY
sort,
npr.id ASC
</select>

<select id="selectAll" resultType="org.apache.shenyu.admin.model.vo.NamespacePluginVO">
SELECT
npr.id as id,
npr.namespace_id AS namespaceId,
Expand Down
10 changes: 10 additions & 0 deletions shenyu-admin/src/main/resources/mappers/rule-sqlmap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@
WHERE namespace_id = #{namespaceId, jdbcType=VARCHAR}
</select>

<select id="selectAllByNamespaceIds" resultType="org.apache.shenyu.admin.model.entity.RuleDO" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM rule
WHERE namespace_id IN
<foreach item="namespaceId" collection="namespaceIds" open="(" separator="," close=")">
#{namespaceId, jdbcType=VARCHAR}
</foreach>
</select>

<select id="existed" resultType="java.lang.Boolean">
SELECT TRUE
FROM rule
Expand Down
Loading

0 comments on commit 6f0d13f

Please sign in to comment.