test and impl that only one root node exists per text
in practice this means only one node with a null parentNode
This commit is contained in:
parent
4f83ae7926
commit
d1eb648b73
2 changed files with 37 additions and 0 deletions
|
|
@ -15,6 +15,9 @@ class CreateNode
|
|||
private TextRepository $textRepo,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function execute(CreateNodeRequest $request): Node
|
||||
{
|
||||
$textId = $request->textId;
|
||||
|
|
@ -23,6 +26,7 @@ class CreateNode
|
|||
throw new DomainException("Text with id: $textId doesnt exist");
|
||||
}
|
||||
if ($request->parentNodeId === null) {
|
||||
$this->validateNoOtherRootNodeExists($textId);
|
||||
return $this->nodeRepo->create(new CreateNodeDto(
|
||||
text: $text,
|
||||
title: $request->title,
|
||||
|
|
@ -41,4 +45,21 @@ class CreateNode
|
|||
parentNode: $parentNode,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DomainException
|
||||
*/
|
||||
private function validateNoOtherRootNodeExists(int $textId): void
|
||||
{
|
||||
$nodesOfText = $this->nodeRepo->findByTextId($textId);
|
||||
$exists = array_find(
|
||||
$nodesOfText,
|
||||
function (Node $node) {
|
||||
return $node->getParentNode() === null;
|
||||
}
|
||||
);
|
||||
if ($exists) {
|
||||
throw new DomainException('A root node already exists for this text');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue