55 lines
1.7 KiB
PHP
55 lines
1.7 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 {
|
|
$result = $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);
|
|
}
|
|
|
|
$element = $result->getElement();
|
|
$childElements = [];
|
|
foreach ($result->getChildElements() as $childElement) {
|
|
$childElements[] = [
|
|
'id' => $childElement->getId(),
|
|
'title' => $childElement->getTitle(),
|
|
'description' => $childElement->getDescription(),
|
|
];
|
|
}
|
|
|
|
return new JsonResponse([
|
|
'childElements' => $childElements,
|
|
'element' => [
|
|
'id' => $element->getId(),
|
|
'title' => $element->getTitle(),
|
|
'description' => $element->getDescription(),
|
|
'richText' => $element->getRichText(),
|
|
'pdfPath' => $element->getPdfPath(),
|
|
'youtubeUrl' => $element->getYoutubeUrl(),
|
|
],
|
|
], 200);
|
|
}
|
|
}
|