Skip to content

Commit

Permalink
feat-IUpdateAbility-编辑树节点的时候可以校验“不能编辑该节点的父节点为其子孙节点”
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 22, 2024
1 parent 55c1f94 commit 1d2040a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.ximatai.muyun.ability.ITreeAbility;
import net.ximatai.muyun.ability.curd.std.ICURDAbility;
import net.ximatai.muyun.core.Scaffold;
import net.ximatai.muyun.core.exception.MyException;
import net.ximatai.muyun.database.IDatabaseOperations;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.TableWrapper;
Expand All @@ -24,6 +25,7 @@

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

@QuarkusTest
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true)
Expand Down Expand Up @@ -55,6 +57,19 @@ void setUp() {
aa1ID = testController.create(Map.of("pid", aaID, "v_name", "A.a.1"));
}

@Test
void testEndlessLoop() {
String x = testController.create(Map.of("v_name", "x"));
String y = testController.create(Map.of("v_name", "y", "pid", x));
String z = testController.create(Map.of("v_name", "z", "pid", y));

MyException exception = assertThrows(MyException.class, () -> {
testController.update(x, Map.of("pid", z));
});

assertEquals(exception.getMessage(), "不能编辑该节点的父节点为其子孙节点");
}

@Test
void testTree() {
List<TreeNode> response = given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.ximatai.muyun.ability.ITreeAbility;
import net.ximatai.muyun.core.exception.MyException;
import net.ximatai.muyun.model.DataChangeChannel;
import net.ximatai.muyun.model.TreeNode;
import org.eclipse.microprofile.openapi.annotations.Operation;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -47,6 +48,15 @@ default Integer update(@PathParam("id") String id, Map body) {
if (id.equals(body.get(treeAbility.getParentKeyColumn().getName()))) {
throw new MyException("树结构的父节点不能是它自身");
}

String pid = (String) body.get(treeAbility.getParentKeyColumn().getName());
if (pid != null) {
List<TreeNode> tree = treeAbility.tree(id, false, null, null);
if (isIdInTree(tree, pid)) {
throw new MyException("不能编辑该节点的父节点为其子孙节点");
}
}

}

if (this instanceof IDataCheckAbility dataCheckAbility) {
Expand Down Expand Up @@ -77,4 +87,17 @@ default Integer update(@PathParam("id") String id, Map body) {
return result;
}

private boolean isIdInTree(List<TreeNode> tree, String id) {
for (TreeNode node : tree) {
if (node.getId().equals(id)) {
return true;
} else if (node.getChildren() != null && !node.getChildren().isEmpty()) {
if (isIdInTree(node.getChildren(), id)) {
return true;
}
}
}
return false;
}

}

0 comments on commit 1d2040a

Please sign in to comment.