implement create node method in node controller
This commit is contained in:
parent
571c0d1196
commit
bdf386e510
1 changed files with 41 additions and 0 deletions
|
|
@ -3,8 +3,12 @@
|
||||||
namespace App\Node;
|
namespace App\Node;
|
||||||
|
|
||||||
use App\Node\NodeRepository;
|
use App\Node\NodeRepository;
|
||||||
|
use App\Node\UseCases\CreateNode;
|
||||||
|
use App\Node\UseCases\CreateNodeRequest;
|
||||||
use App\Text\TextRepository;
|
use App\Text\TextRepository;
|
||||||
|
use DomainException;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
|
||||||
class NodeController
|
class NodeController
|
||||||
{
|
{
|
||||||
|
|
@ -34,4 +38,41 @@ class NodeController
|
||||||
$response->getBody()->write(json_encode(array_values($data)));
|
$response->getBody()->write(json_encode(array_values($data)));
|
||||||
return $response->withHeader('Content-Type', 'application/json');
|
return $response->withHeader('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function createNode(
|
||||||
|
Request $request,
|
||||||
|
Response $response,
|
||||||
|
CreateNode $createNodeUseCase,
|
||||||
|
): Response {
|
||||||
|
$data = $request->getParsedBody();
|
||||||
|
$title = $data['title'] ?? '';
|
||||||
|
|
||||||
|
if (empty($title)) {
|
||||||
|
$response->getBody()->write(json_encode(['error' => 'Title is required']));
|
||||||
|
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
||||||
|
}
|
||||||
|
|
||||||
|
$textId = (int) ($data['textId'] ?? 0);
|
||||||
|
$parentNodeId = isset($data['parentNodeId']) && $data['parentNodeId'] !== null
|
||||||
|
? (int) $data['parentNodeId']
|
||||||
|
: null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$node = $createNodeUseCase->execute(new CreateNodeRequest(
|
||||||
|
textId: $textId,
|
||||||
|
title: $title,
|
||||||
|
parentNodeId: $parentNodeId,
|
||||||
|
));
|
||||||
|
} catch (DomainException $e) {
|
||||||
|
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
||||||
|
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
|
||||||
|
}
|
||||||
|
|
||||||
|
$response->getBody()->write(json_encode([
|
||||||
|
'id' => $node->getId(),
|
||||||
|
'title' => $node->getTitle(),
|
||||||
|
'parentNodeId' => $node->getParentNode()?->getId(),
|
||||||
|
]));
|
||||||
|
return $response->withStatus(201)->withHeader('Content-Type', 'application/json');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue