Skip to content

Commit

Permalink
[type:feat]Add plugin of namespace (#5677)
Browse files Browse the repository at this point in the history
* add

* add

* add

* fix cr

---------

Co-authored-by: ‘xcsnx’ <‘1192709687@qq.com’>
Co-authored-by: moremind <hefengen@apache.org>
  • Loading branch information
3 people authored Oct 9, 2024
1 parent e37981f commit 2159721
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 2159721

Please sign in to comment.