Rabbi_Gerzi/backend/app/Controllers/ElementController.php

40 lines
1 KiB
PHP

<?php
namespace App\Controllers;
use App\Element\UseCases\GetElement\GetElement;
use App\Element\UseCases\GetElement\GetElementRequest;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
use Illuminate\Http\JsonResponse;
class ElementController
{
public function __construct(private GetElement $getElement)
{
}
public function show(?int $id): JsonResponse
{
try {
$element = $this->getElement->execute(
new GetElementRequest(id: $id)
);
} catch (BadRequestException $exception) {
return new JsonResponse([
'error' => $exception->getMessage(),
], 400);
} catch (NotFoundException $exception) {
return new JsonResponse([
'error' => $exception->getMessage(),
], 404);
}
return new JsonResponse([
'element' => [
'id' => $element->getId(),
'title' => $element->getTitle(),
],
], 200);
}
}