120 lines
3.7 KiB
PHP
120 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Element\Element;
|
|
use App\Element\UseCases\GetElement\GetElement;
|
|
use App\Element\UseCases\GetElement\GetElementRequest;
|
|
use App\Element\UseCases\UpdateElement\UpdateElement;
|
|
use App\Element\UseCases\UpdateElement\UpdateElementRequest;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\NotFoundException;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ElementController
|
|
{
|
|
public function __construct(
|
|
private GetElement $getElement,
|
|
private UpdateElement $updateElement,
|
|
) {
|
|
}
|
|
|
|
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' => $this->buildElementPayload($element),
|
|
], 200);
|
|
}
|
|
|
|
public function update(?int $id, Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$element = $this->updateElement->execute(
|
|
new UpdateElementRequest(
|
|
id: $id,
|
|
title: $this->stringInput($request, 'title'),
|
|
description: $this->stringInput($request, 'description'),
|
|
iconImageUrl: $this->stringInput(
|
|
$request,
|
|
'iconImageUrl',
|
|
),
|
|
richText: $this->stringInput($request, 'richText'),
|
|
pdfPath: $this->stringInput($request, 'pdfPath'),
|
|
youtubeUrl: $this->stringInput($request, 'youtubeUrl'),
|
|
)
|
|
);
|
|
} catch (BadRequestException $exception) {
|
|
return new JsonResponse([
|
|
'error' => $exception->getMessage(),
|
|
], 400);
|
|
} catch (NotFoundException $exception) {
|
|
return new JsonResponse([
|
|
'error' => $exception->getMessage(),
|
|
], 404);
|
|
}
|
|
|
|
return new JsonResponse([
|
|
'element' => $this->buildElementPayload($element),
|
|
], 200);
|
|
}
|
|
|
|
private function stringInput(Request $request, string $key): ?string
|
|
{
|
|
$value = $request->input($key);
|
|
if (! is_string($value)) {
|
|
return null;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* @return array{
|
|
* id: int,
|
|
* title: string,
|
|
* description: string,
|
|
* iconImageUrl: string|null,
|
|
* richText: string,
|
|
* pdfPath: string|null,
|
|
* youtubeUrl: string|null,
|
|
* }
|
|
*/
|
|
private function buildElementPayload(Element $element): array
|
|
{
|
|
return [
|
|
'id' => $element->getId(),
|
|
'title' => $element->getTitle(),
|
|
'description' => $element->getDescription(),
|
|
'iconImageUrl' => $element->getIconImageUrl(),
|
|
'richText' => $element->getRichText(),
|
|
'pdfPath' => $element->getPdfPath(),
|
|
'youtubeUrl' => $element->getYoutubeUrl(),
|
|
];
|
|
}
|
|
}
|