add element child list

This commit is contained in:
Yisroel Baum 2026-05-26 20:16:22 +03:00
parent aa746fe3f0
commit 7350d747f3
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
9 changed files with 186 additions and 10 deletions

View file

@ -17,7 +17,7 @@ class ElementController
public function show(?int $id): JsonResponse
{
try {
$element = $this->getElement->execute(
$result = $this->getElement->execute(
new GetElementRequest(id: $id)
);
} catch (BadRequestException $exception) {
@ -30,7 +30,17 @@ class ElementController
], 404);
}
$element = $result->getElement();
$childElements = [];
foreach ($result->getChildElements() as $childElement) {
$childElements[] = [
'id' => $childElement->getId(),
'title' => $childElement->getTitle(),
];
}
return new JsonResponse([
'childElements' => $childElements,
'element' => [
'id' => $element->getId(),
'title' => $element->getTitle(),

View file

@ -16,4 +16,9 @@ interface ElementRepository
* @return Element[]
*/
public function findBySet(DomainSet $set): array;
/**
* @return Element[]
*/
public function findByParentElement(Element $parentElement): array;
}

View file

@ -61,6 +61,25 @@ class EloquentElementRepository implements ElementRepository
return $elements;
}
/**
* @return Element[]
*/
public function findByParentElement(Element $parentElement): array
{
$models = ElementModel::where(
'parent_element_id',
$parentElement->getId(),
)
->orderBy('id')
->get();
$elements = [];
foreach ($models as $model) {
$elements[] = $this->toDomain($model);
}
return $elements;
}
private function toDomain(ElementModel $model): Element
{
$set = $this->setRepo->find($model->set_id);

View file

@ -2,7 +2,6 @@
namespace App\Element\UseCases\GetElement;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
@ -17,7 +16,7 @@ class GetElement
* @throws BadRequestException
* @throws NotFoundException
*/
public function execute(GetElementRequest $request): Element
public function execute(GetElementRequest $request): GetElementResult
{
if ($request->id === null) {
throw new BadRequestException('id is required');
@ -28,6 +27,10 @@ class GetElement
throw new NotFoundException('Element not found');
}
return $element;
return new GetElementResult(
element: $element,
childElements: $this->elementRepository
->findByParentElement($element),
);
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace App\Element\UseCases\GetElement;
use App\Element\Element;
class GetElementResult
{
/**
* @param Element[] $childElements
*/
public function __construct(
private Element $element,
private array $childElements,
) {
}
public function getElement(): Element
{
return $this->element;
}
/**
* @return Element[]
*/
public function getChildElements(): array
{
return $this->childElements;
}
}