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;
|
||||
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Text\TextRepository;
|
||||
use App\Text\UseCases\CreateText;
|
||||
use App\Text\UseCases\CreateTextRequest;
|
||||
|
|
@ -50,21 +51,21 @@ class TextController
|
|||
CreateText $createTextUseCase,
|
||||
): Response {
|
||||
$data = $request->getParsedBody();
|
||||
$name = $data['name'] ?? '';
|
||||
$name = $data['name'] ?? null;
|
||||
|
||||
if (!empty($name)) {
|
||||
try {
|
||||
$text = $createTextUseCase->execute(new CreateTextRequest(
|
||||
name: $name,
|
||||
));
|
||||
|
||||
$response->getBody()->write(json_encode([
|
||||
'id' => $text->getId(),
|
||||
'name' => $text->getName(),
|
||||
]));
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
} catch (BadRequestException $e) {
|
||||
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
|
||||
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode(['error' => 'Name is required']));
|
||||
return $response->withStatus(400);
|
||||
$response->getBody()->write(json_encode([
|
||||
'id' => $text->getId(),
|
||||
'name' => $text->getName(),
|
||||
]));
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue