add method to get nodes of text id in text controller
This commit is contained in:
parent
c99e1eeeb5
commit
d4f5b22034
1 changed files with 24 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ namespace App\Text;
|
|||
use App\Text\TextRepository;
|
||||
use App\Text\UseCases\CreateText;
|
||||
use App\Text\UseCases\CreateTextRequest;
|
||||
use App\Node\NodeRepository;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
|
|
@ -12,6 +13,7 @@ class TextController
|
|||
{
|
||||
public function __construct(
|
||||
private TextRepository $textRepository,
|
||||
private NodeRepository $nodeRepository,
|
||||
) {}
|
||||
|
||||
public function getTexts(Response $response): Response
|
||||
|
|
@ -44,6 +46,28 @@ class TextController
|
|||
return $response->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
public function getNodes(Response $response, int $textId): Response
|
||||
{
|
||||
$text = $this->textRepository->find($textId);
|
||||
|
||||
if ($text === null) {
|
||||
return $response->withStatus(404);
|
||||
}
|
||||
|
||||
$nodes = $this->nodeRepository->findByTextId($textId);
|
||||
|
||||
$data = array_map(function ($node) {
|
||||
return [
|
||||
'id' => $node->getId(),
|
||||
'title' => $node->getTitle(),
|
||||
'parentNodeId' => $node->getParentNode()?->getId(),
|
||||
];
|
||||
}, $nodes);
|
||||
|
||||
$response->getBody()->write(json_encode(array_values($data)));
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
public function createText(
|
||||
Request $request,
|
||||
Response $response,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue