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\TextRepository;
|
||||||
use App\Text\UseCases\CreateText;
|
use App\Text\UseCases\CreateText;
|
||||||
use App\Text\UseCases\CreateTextRequest;
|
use App\Text\UseCases\CreateTextRequest;
|
||||||
|
use App\Node\NodeRepository;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
|
||||||
|
|
@ -12,6 +13,7 @@ class TextController
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private TextRepository $textRepository,
|
private TextRepository $textRepository,
|
||||||
|
private NodeRepository $nodeRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function getTexts(Response $response): Response
|
public function getTexts(Response $response): Response
|
||||||
|
|
@ -44,6 +46,28 @@ class TextController
|
||||||
return $response->withHeader('Content-Type', 'application/json');
|
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(
|
public function createText(
|
||||||
Request $request,
|
Request $request,
|
||||||
Response $response,
|
Response $response,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue