refactor getNodes into a new NodeController and update refs
This commit is contained in:
parent
acdfc14442
commit
38d06fce43
4 changed files with 40 additions and 27 deletions
37
app/Node/NodeController.php
Normal file
37
app/Node/NodeController.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace App\Node;
|
||||
|
||||
use App\Node\NodeRepository;
|
||||
use App\Text\TextRepository;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
|
||||
class NodeController
|
||||
{
|
||||
public function __construct(
|
||||
private NodeRepository $nodeRepository,
|
||||
private TextRepository $textRepository,
|
||||
) {}
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,6 @@ 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;
|
||||
|
||||
|
|
@ -13,7 +12,6 @@ class TextController
|
|||
{
|
||||
public function __construct(
|
||||
private TextRepository $textRepository,
|
||||
private NodeRepository $nodeRepository,
|
||||
) {}
|
||||
|
||||
public function getTexts(Response $response): Response
|
||||
|
|
@ -46,28 +44,6 @@ 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,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use Psr\Http\Message\ServerRequestInterface as Request;
|
|||
use DI\Bridge\Slim\Bridge;
|
||||
use App\View\ViewController;
|
||||
use App\Text\TextController;
|
||||
use App\Node\NodeRepository;
|
||||
use App\Node\NodeController;
|
||||
|
||||
$container = require __DIR__.'/container.php';
|
||||
$app = Bridge::create($container);
|
||||
|
|
@ -19,7 +19,7 @@ $app->get('/admin/texts/{textId}', [ViewController::class, 'text']);
|
|||
|
||||
$app->get('/api/texts', [TextController::class, 'getTexts']);
|
||||
$app->get('/api/texts/{textId}', [TextController::class, 'getText']);
|
||||
$app->get('/api/texts/{textId}/nodes', [TextController::class, 'getNodes']);
|
||||
$app->get('/api/texts/{textId}/nodes', [NodeController::class, 'getNodes']);
|
||||
$app->post('/api/texts', [TextController::class, 'createText']);
|
||||
|
||||
return $app;
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ class TextControllerTest extends TestCase
|
|||
$this->textRepo->create(new CreateTextDto(
|
||||
name: 'test text',
|
||||
));
|
||||
$this->controller = new TextController($this->textRepo, new FakeNodeRepository);
|
||||
$this->controller = new TextController($this->textRepo);
|
||||
}
|
||||
|
||||
public function test_get_one_text(): void
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue