refactor create text controller to catch BadRequestException
This commit is contained in:
parent
82dab3b90f
commit
6009fb7ddd
1 changed files with 11 additions and 10 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Text;
|
namespace App\Text;
|
||||||
|
|
||||||
|
use App\Exceptions\BadRequestException;
|
||||||
use App\Text\TextRepository;
|
use App\Text\TextRepository;
|
||||||
use App\Text\UseCases\CreateText;
|
use App\Text\UseCases\CreateText;
|
||||||
use App\Text\UseCases\CreateTextRequest;
|
use App\Text\UseCases\CreateTextRequest;
|
||||||
|
|
@ -50,21 +51,21 @@ class TextController
|
||||||
CreateText $createTextUseCase,
|
CreateText $createTextUseCase,
|
||||||
): Response {
|
): Response {
|
||||||
$data = $request->getParsedBody();
|
$data = $request->getParsedBody();
|
||||||
$name = $data['name'] ?? '';
|
$name = $data['name'] ?? null;
|
||||||
|
|
||||||
if (!empty($name)) {
|
try {
|
||||||
$text = $createTextUseCase->execute(new CreateTextRequest(
|
$text = $createTextUseCase->execute(new CreateTextRequest(
|
||||||
name: $name,
|
name: $name,
|
||||||
));
|
));
|
||||||
|
} catch (BadRequestException $e) {
|
||||||
$response->getBody()->write(json_encode([
|
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
||||||
'id' => $text->getId(),
|
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
||||||
'name' => $text->getName(),
|
|
||||||
]));
|
|
||||||
return $response->withHeader('Content-Type', 'application/json');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->getBody()->write(json_encode(['error' => 'Name is required']));
|
$response->getBody()->write(json_encode([
|
||||||
return $response->withStatus(400);
|
'id' => $text->getId(),
|
||||||
|
'name' => $text->getName(),
|
||||||
|
]));
|
||||||
|
return $response->withHeader('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue