implement node controller method for bulk create nodes
This commit is contained in:
parent
ce56e460ff
commit
53cb002d0d
1 changed files with 53 additions and 0 deletions
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace App\Node;
|
||||
|
||||
use App\Node\UseCases\BulkCreateNodesRequest;
|
||||
use App\Node\NodeRepository;
|
||||
use App\Node\UseCases\BulkCreateNodes;
|
||||
use App\Node\UseCases\CreateNode;
|
||||
use App\Node\UseCases\CreateNodeRequest;
|
||||
use App\Text\TextRepository;
|
||||
|
|
@ -75,4 +77,55 @@ class NodeController
|
|||
]));
|
||||
return $response->withStatus(201)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
public function bulkCreateNodes(
|
||||
Request $request,
|
||||
Response $response,
|
||||
BulkCreateNodes $bulkCreateNodesUseCase,
|
||||
): Response {
|
||||
$data = json_decode((string) $request->getBody(), true) ?? [];
|
||||
|
||||
$titlePrefix = trim($data['titlePrefix'] ?? '');
|
||||
if ($titlePrefix === '') {
|
||||
$response->getBody()->write(json_encode(['error' => 'Title prefix is required']));
|
||||
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
$count = isset($data['count']) ? (int) $data['count'] : 0;
|
||||
if ($count < 1) {
|
||||
$response->getBody()->write(json_encode(['error' => 'Count must be at least 1']));
|
||||
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
if (!isset($data['parentNodeId']) || $data['parentNodeId'] === null) {
|
||||
$response->getBody()->write(json_encode(['error' => 'parentNodeId is required']));
|
||||
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
$textId = (int) ($data['textId'] ?? 0);
|
||||
$parentNodeId = (int) $data['parentNodeId'];
|
||||
|
||||
try {
|
||||
$nodes = $bulkCreateNodesUseCase->execute(new BulkCreateNodesRequest(
|
||||
textId: $textId,
|
||||
parentNodeId: $parentNodeId,
|
||||
titlePrefix: $titlePrefix,
|
||||
count: $count,
|
||||
));
|
||||
} catch (DomainException $e) {
|
||||
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
||||
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
$result = array_map(function ($node) {
|
||||
return [
|
||||
'id' => $node->getId(),
|
||||
'title' => $node->getTitle(),
|
||||
'parentNodeId' => $node->getParentNode()?->getId(),
|
||||
];
|
||||
}, $nodes);
|
||||
|
||||
$response->getBody()->write(json_encode(array_values($result)));
|
||||
return $response->withStatus(201)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue