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 {
|
||||
$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'];
|
||||
$textId = isset($data['textId']) ? (int) $data['textId'] : null;
|
||||
$parentNodeId = isset($data['parentNodeId']) ? (int) $data['parentNodeId'] : null;
|
||||
$titlePrefix = isset($data['titlePrefix']) ? (string) $data['titlePrefix'] : null;
|
||||
$count = isset($data['count']) ? (int) $data['count'] : null;
|
||||
|
||||
try {
|
||||
$nodes = $bulkCreateNodesUseCase->execute(new BulkCreateNodesRequest(
|
||||
|
|
@ -109,6 +94,9 @@ class NodeController
|
|||
titlePrefix: $titlePrefix,
|
||||
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) {
|
||||
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
||||
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue