add use case and request
This commit is contained in:
parent
5c2b6c9edd
commit
ce56e460ff
2 changed files with 58 additions and 0 deletions
45
app/Node/UseCases/BulkCreateNodes.php
Normal file
45
app/Node/UseCases/BulkCreateNodes.php
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Node\UseCases;
|
||||||
|
|
||||||
|
use App\Node\CreateNodeDto;
|
||||||
|
use App\Node\Node;
|
||||||
|
use App\Node\NodeRepository;
|
||||||
|
use App\Text\TextRepository;
|
||||||
|
use DomainException;
|
||||||
|
|
||||||
|
class BulkCreateNodes
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private NodeRepository $nodeRepo,
|
||||||
|
private TextRepository $textRepo,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Node[]
|
||||||
|
* @throws DomainException
|
||||||
|
*/
|
||||||
|
public function execute(BulkCreateNodesRequest $request): array
|
||||||
|
{
|
||||||
|
$text = $this->textRepo->find($request->textId);
|
||||||
|
if ($text === null) {
|
||||||
|
throw new DomainException("Text with id: {$request->textId} doesnt exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
$parentNode = $this->nodeRepo->find($request->parentNodeId);
|
||||||
|
if ($parentNode === null) {
|
||||||
|
throw new DomainException("Node with id: {$request->parentNodeId} doesnt exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
$created = [];
|
||||||
|
for ($i = 1; $i <= $request->count; $i++) {
|
||||||
|
$created[] = $this->nodeRepo->create(new CreateNodeDto(
|
||||||
|
text: $text,
|
||||||
|
title: "{$request->titlePrefix} {$i}",
|
||||||
|
parentNode: $parentNode,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $created;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
app/Node/UseCases/BulkCreateNodesRequest.php
Normal file
13
app/Node/UseCases/BulkCreateNodesRequest.php
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Node\UseCases;
|
||||||
|
|
||||||
|
class BulkCreateNodesRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $textId,
|
||||||
|
public int $parentNodeId,
|
||||||
|
public string $titlePrefix,
|
||||||
|
public int $count,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue