refactor bulk create nodes controller to catch BadRequestException
This commit is contained in:
parent
1761bfad7f
commit
8a90c5bab4
1 changed files with 7 additions and 19 deletions
|
|
@ -82,25 +82,10 @@ class NodeController
|
||||||
): Response {
|
): Response {
|
||||||
$data = json_decode((string) $request->getBody(), true) ?? [];
|
$data = json_decode((string) $request->getBody(), true) ?? [];
|
||||||
|
|
||||||
$titlePrefix = trim($data['titlePrefix'] ?? '');
|
$textId = isset($data['textId']) ? (int) $data['textId'] : null;
|
||||||
if ($titlePrefix === '') {
|
$parentNodeId = isset($data['parentNodeId']) ? (int) $data['parentNodeId'] : null;
|
||||||
$response->getBody()->write(json_encode(['error' => 'Title prefix is required']));
|
$titlePrefix = isset($data['titlePrefix']) ? (string) $data['titlePrefix'] : null;
|
||||||
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
$count = isset($data['count']) ? (int) $data['count'] : null;
|
||||||
}
|
|
||||||
|
|
||||||
$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 {
|
try {
|
||||||
$nodes = $bulkCreateNodesUseCase->execute(new BulkCreateNodesRequest(
|
$nodes = $bulkCreateNodesUseCase->execute(new BulkCreateNodesRequest(
|
||||||
|
|
@ -109,6 +94,9 @@ class NodeController
|
||||||
titlePrefix: $titlePrefix,
|
titlePrefix: $titlePrefix,
|
||||||
count: $count,
|
count: $count,
|
||||||
));
|
));
|
||||||
|
} catch (BadRequestException $e) {
|
||||||
|
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
||||||
|
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
||||||
} catch (DomainException $e) {
|
} catch (DomainException $e) {
|
||||||
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
||||||
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
|
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue