Skip to content

Commit

Permalink
Fix a logic error in getting all paths of a resource item on a resour…
Browse files Browse the repository at this point in the history
…ce set.
  • Loading branch information
gudaoxuri committed May 7, 2024
1 parent c84d848 commit 77a4868
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 33 deletions.
29 changes: 27 additions & 2 deletions backend/basic/src/rbum/dto/rbum_filer_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ pub struct RbumSetFilterReq {
/// Resource relation filter
///
/// 资源关联过滤
pub rel: Option<RbumItemRelFilterReq>,
pub rel: Option<RbumItemRelSimpleFilterReq>,
/// Resource kind id
///
/// 资源类型id
Expand All @@ -325,7 +325,7 @@ pub struct RbumSetCateFilterReq {
/// Resource relation filter
///
/// 资源关联过滤
pub rel: Option<RbumItemRelFilterReq>,
pub rel: Option<RbumItemRelSimpleFilterReq>,
/// Resource set id
///
/// 资源集id
Expand Down Expand Up @@ -509,6 +509,31 @@ pub struct RbumSetItemRelFilterReq {
pub rel_item_ids: Option<Vec<String>>,
}

/// Simple Resource item relation filter
///
/// 简单的资源项关联过滤器
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[cfg_attr(feature = "default", derive(poem_openapi::Object))]
#[serde(default)]
pub struct RbumItemRelSimpleFilterReq {
/// Whether the related party is a ``from`` party
///
/// 关联方是否是 ``from`` 方
pub rel_by_from: bool,
/// Associated tag
///
/// 关联的标签
pub tag: Option<String>,
/// ``from`` party kind
///
/// ``from`` 方的类型
pub from_rbum_kind: Option<RbumRelFromKind>,
/// Associated object id
///
/// 关联的对象id
pub rel_item_id: Option<String>,
}

/// Resource item relation filter
///
/// 资源项关联过滤器
Expand Down
8 changes: 4 additions & 4 deletions backend/basic/src/rbum/dto/rbum_set_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ pub struct RbumSetDetailResp {
#[derive(Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "default", derive(poem_openapi::Object, sea_orm::FromQueryResult))]
pub struct RbumSetPathResp {
/// Resource set id
/// Node id
///
/// 资源集id
/// 节点id
pub id: String,
/// Resource set name
/// Node name
///
/// 资源集名称
/// 节点名称
pub name: String,

pub own_paths: String,
Expand Down
6 changes: 3 additions & 3 deletions backend/basic/src/rbum/dto/rbum_set_item_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ pub struct RbumSetItemSummaryResp {
/// Associated [resource set category(node)](crate::rbum::dto::rbum_set_cate_dto::RbumSetCateDetailResp) id
///
/// 关联[资源集分类(节点)](crate::rbum::dto::rbum_set_cate_dto::RbumSetCateDetailResp) id
pub rel_rbum_set_cate_id: String,
pub rel_rbum_set_cate_id: Option<String>,
/// Associated [resource set category(node)](crate::rbum::dto::rbum_set_cate_dto::RbumSetCateDetailResp) sys_code
///
/// 关联[资源集分类(节点)](crate::rbum::dto::rbum_set_cate_dto::RbumSetCateDetailResp) sys_code
pub rel_rbum_set_cate_sys_code: String,
pub rel_rbum_set_cate_sys_code: Option<String>,
/// Associated [resource set category(node)](crate::rbum::dto::rbum_set_cate_dto::RbumSetCateDetailResp) name
///
/// 关联[资源集分类(节点)](crate::rbum::dto::rbum_set_cate_dto::RbumSetCateDetailResp) name
pub rel_rbum_set_cate_name: String,
pub rel_rbum_set_cate_name: Option<String>,
/// Associated [resource item](crate::rbum::dto::rbum_item_dto::RbumItemDetailResp) id
///
/// 关联[资源项](crate::rbum::dto::rbum_item_dto::RbumItemDetailResp) id
Expand Down
2 changes: 1 addition & 1 deletion backend/basic/src/rbum/serv/rbum_rel_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ impl RbumRelServ {
)
.await?
.into_iter()
.map(|i| i.rel_rbum_set_cate_id)
.map(|i| i.rel_rbum_set_cate_id.unwrap_or_default())
.collect::<Vec<String>>()
} else if check_req.from_rbum_kind == RbumRelFromKind::SetCate {
vec![check_req.from_rbum_id.clone()]
Expand Down
74 changes: 55 additions & 19 deletions backend/basic/src/rbum/serv/rbum_set_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,22 +779,6 @@ impl RbumCrudOperation<rbum_set_item::ActiveModel, RbumSetItemAddReq, RbumSetIte
rbum_set_item::Entity.table_name()
}

async fn package_add(add_req: &RbumSetItemAddReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<rbum_set_item::ActiveModel> {
let rel_sys_code = if add_req.rel_rbum_set_cate_id.is_empty() {
"".to_string()
} else {
RbumSetCateServ::get_sys_code(add_req.rel_rbum_set_cate_id.as_str(), funs, ctx).await?
};
Ok(rbum_set_item::ActiveModel {
id: Set(TardisFuns::field.nanoid()),
rel_rbum_set_id: Set(add_req.rel_rbum_set_id.to_string()),
rel_rbum_set_cate_code: Set(rel_sys_code),
rel_rbum_item_id: Set(add_req.rel_rbum_item_id.to_string()),
sort: Set(add_req.sort),
..Default::default()
})
}

async fn before_add_rbum(add_req: &mut RbumSetItemAddReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
Self::check_scope(&add_req.rel_rbum_set_id, RbumSetServ::get_table_name(), funs, ctx).await?;
Self::check_scope(&add_req.rel_rbum_item_id, RbumItemServ::get_table_name(), funs, ctx).await?;
Expand Down Expand Up @@ -822,6 +806,22 @@ impl RbumCrudOperation<rbum_set_item::ActiveModel, RbumSetItemAddReq, RbumSetIte
Ok(())
}

async fn package_add(add_req: &RbumSetItemAddReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<rbum_set_item::ActiveModel> {
let rel_sys_code = if add_req.rel_rbum_set_cate_id.is_empty() {
"".to_string()
} else {
RbumSetCateServ::get_sys_code(add_req.rel_rbum_set_cate_id.as_str(), funs, ctx).await?
};
Ok(rbum_set_item::ActiveModel {
id: Set(TardisFuns::field.nanoid()),
rel_rbum_set_id: Set(add_req.rel_rbum_set_id.to_string()),
rel_rbum_set_cate_code: Set(rel_sys_code),
rel_rbum_item_id: Set(add_req.rel_rbum_item_id.to_string()),
sort: Set(add_req.sort),
..Default::default()
})
}

async fn package_modify(id: &str, modify_req: &RbumSetItemModifyReq, _: &TardisFunsInst, _: &TardisContext) -> TardisResult<rbum_set_item::ActiveModel> {
Ok(rbum_set_item::ActiveModel {
id: Set(id.to_string()),
Expand Down Expand Up @@ -982,9 +982,32 @@ impl RbumCrudOperation<rbum_set_item::ActiveModel, RbumSetItemAddReq, RbumSetIte
}

impl RbumSetItemServ {
/// Fetch all the paths of the resource item in the resource set
///
/// 获取资源项在某一资源集上的所有路径
///
/// Return format:
///
/// * The first-level array is all the paths corresponding to the resource item (a resource item can hang multiple paths)
/// * The second-level data is each node (such as the path is l1/l2/l3, then the corresponding node is l1, l2, l3)
///
/// * 第一层数组为资源项对应的所有路径(一个资源项可以挂多个路径)
/// * 第二层数据为每个节点(比如路径为 l1/l2/l3, 那么对应的节点为 l1, l2, l3)
///
/// ```json
/// [
/// [
/// {
/// "id": "Node Id",
/// "name": "Node Name"
/// }
/// ]
/// ]
/// ```
pub async fn find_set_paths(rbum_item_id: &str, rbum_set_id: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<Vec<Vec<RbumSetPathResp>>> {
let rbum_set_cate_sys_codes: Vec<String> = Self::find_rbums(
&RbumSetItemFilterReq {
rel_rbum_item_can_not_exist: Some(true),
rel_rbum_set_id: Some(rbum_set_id.to_string()),
rel_rbum_item_ids: Some(vec![rbum_item_id.to_string()]),
..Default::default()
Expand All @@ -996,10 +1019,15 @@ impl RbumSetItemServ {
)
.await?
.into_iter()
.map(|item| item.rel_rbum_set_cate_sys_code)
.map(|item| item.rel_rbum_set_cate_sys_code.unwrap_or_default())
.collect();
let mut result: Vec<Vec<RbumSetPathResp>> = Vec::with_capacity(rbum_set_cate_sys_codes.len());
for rbum_set_cate_sys_code in rbum_set_cate_sys_codes {
if rbum_set_cate_sys_code.is_empty() {
// Mount on the root node and directly give an empty array
result.push(vec![]);
continue;
}
let rbum_set_paths = RbumSetCateServ::find_rbums(
&RbumSetCateFilterReq {
rel_rbum_set_id: Some(rbum_set_id.to_string()),
Expand Down Expand Up @@ -1065,8 +1093,16 @@ impl RbumSetItemServ {
ctx,
)
.await?;
let set_items_a = set_items.iter().filter(|item| item.rel_rbum_item_id == rbum_item_a_id).map(|item| item.rel_rbum_set_cate_sys_code.clone()).collect::<Vec<String>>();
let set_items_b = set_items.iter().filter(|item| item.rel_rbum_item_id == rbum_item_b_id).map(|item| item.rel_rbum_set_cate_sys_code.clone()).collect::<Vec<String>>();
let set_items_a = set_items
.iter()
.filter(|item| item.rel_rbum_item_id == rbum_item_a_id)
.map(|item| item.rel_rbum_set_cate_sys_code.clone().unwrap_or_default())
.collect::<Vec<String>>();
let set_items_b = set_items
.iter()
.filter(|item| item.rel_rbum_item_id == rbum_item_b_id)
.map(|item| item.rel_rbum_set_cate_sys_code.clone().unwrap_or_default())
.collect::<Vec<String>>();

Ok(set_items_a.iter().any(|sys_code_a| {
set_items_b.iter().any(|sys_code_b| {
Expand Down
9 changes: 5 additions & 4 deletions backend/basic/tests/test_rbum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,11 @@ async fn test_rbum_set_item(context: &TardisContext) -> TardisResult<()> {

info!("【test_rbum_set_item】 : Test Find Set Paths : RbumSetItemServ::get_rbum");
let set_paths = RbumSetItemServ::find_set_paths(&item_account_a1_id, &set_id, &funs, context).await?;
assert_eq!(set_paths.len(), 1);
assert_eq!(set_paths[0].len(), 2);
assert!(set_paths.first().unwrap().iter().any(|i| i.name == "l2"));
assert!(set_paths.first().unwrap().iter().any(|i| i.name == "l1"));
assert_eq!(set_paths.len(), 2);
assert_eq!(set_paths[0].len(), 0);
assert_eq!(set_paths[1].len(), 2);
assert!(set_paths[2].iter().any(|i| i.name == "l2"));
assert!(set_paths[2].iter().any(|i| i.name == "l1"));

info!("【test_rbum_set_item】 : Test Modify : RbumSetItemServ::modify_rbum");
RbumSetItemServ::modify_rbum(&id, &mut RbumSetItemModifyReq { sort: 10 }, &funs, context).await?;
Expand Down

0 comments on commit 77a4868

Please sign in to comment.