diff --git a/src/app/tag/controllers/TagController.php b/src/app/tag/controllers/TagController.php index fef2dbe2b134d2f8d2f15c1335e1f5a7779def9c..da991d4c29bf9a06e2e185a7395e5b2215cf85ff 100644 --- a/src/app/tag/controllers/TagController.php +++ b/src/app/tag/controllers/TagController.php @@ -175,6 +175,20 @@ class TagController return $response->withStatus(400)->withJson(['errors' => 'Parent tag does not exist']); } $parent = $parent['id']; + + if ($parent == $args['id']) { + return $response->withStatus(400)->withJson(['errors' => 'Tag cannot be its own parent']); + } + + $children = TagModel::get([ + 'select' => ['id'], + 'where' => ['parent_id = ?'], + 'data' => [$args['id']] + ]); + $children = array_column($children, 'id'); + if (in_array($parent, $children)) { + return $response->withStatus(400)->withJson(['errors' => 'Parent tag cannot also be a children']); + } } TagModel::update([