From 7736b8880273cda7581d0def3f32524a49086944 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Mon, 25 May 2026 21:42:38 +0300 Subject: [PATCH] add set root ids --- backend/app/Controllers/SetController.php | 13 ++++++++++--- backend/app/Element/ElementRepository.php | 2 ++ backend/app/Element/EloquentElementRepository.php | 10 ++++++++++ backend/tests/Fakes/FakeElementRepository.php | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/backend/app/Controllers/SetController.php b/backend/app/Controllers/SetController.php index 1a035c9..6fcbb8c 100644 --- a/backend/app/Controllers/SetController.php +++ b/backend/app/Controllers/SetController.php @@ -2,14 +2,17 @@ namespace App\Controllers; +use App\Element\ElementRepository; use App\Set\Set as DomainSet; use App\Set\SetRepository; use Illuminate\Http\JsonResponse; class SetController { - public function __construct(private SetRepository $setRepository) - { + public function __construct( + private SetRepository $setRepository, + private ElementRepository $elementRepository, + ) { } public function index(): JsonResponse @@ -29,16 +32,20 @@ class SetController * id: int, * name: string, * description: string, - * iconImageUrl: string + * iconImageUrl: string, + * rootElementId: int|null * } */ private function buildSetPayload(DomainSet $set): array { + $rootElement = $this->elementRepository->findRootBySet($set); + return [ 'id' => $set->getId(), 'name' => $set->getName(), 'description' => $set->getDescription(), 'iconImageUrl' => $set->getIconImageUrl(), + 'rootElementId' => $rootElement?->getId(), ]; } } diff --git a/backend/app/Element/ElementRepository.php b/backend/app/Element/ElementRepository.php index 4f39fb6..8ed13bd 100644 --- a/backend/app/Element/ElementRepository.php +++ b/backend/app/Element/ElementRepository.php @@ -10,6 +10,8 @@ interface ElementRepository public function find(int $id): ?Element; + public function findRootBySet(DomainSet $set): ?Element; + /** * @return Element[] */ diff --git a/backend/app/Element/EloquentElementRepository.php b/backend/app/Element/EloquentElementRepository.php index 61e1433..68452de 100644 --- a/backend/app/Element/EloquentElementRepository.php +++ b/backend/app/Element/EloquentElementRepository.php @@ -35,6 +35,16 @@ class EloquentElementRepository implements ElementRepository return $model === null ? null : $this->toDomain($model); } + public function findRootBySet(DomainSet $set): ?Element + { + $model = ElementModel::where('set_id', $set->getId()) + ->whereNull('parent_element_id') + ->orderBy('id') + ->first(); + + return $model === null ? null : $this->toDomain($model); + } + /** * @return Element[] */ diff --git a/backend/tests/Fakes/FakeElementRepository.php b/backend/tests/Fakes/FakeElementRepository.php index 65dbcad..cbb4e13 100644 --- a/backend/tests/Fakes/FakeElementRepository.php +++ b/backend/tests/Fakes/FakeElementRepository.php @@ -37,6 +37,20 @@ class FakeElementRepository implements ElementRepository return $this->cloneElement($this->elementsById[$id]); } + public function findRootBySet(DomainSet $set): ?Element + { + foreach ($this->elementsById as $element) { + if ( + $element->getSet()->getId() === $set->getId() + && $element->getParentElement() === null + ) { + return $this->cloneElement($element); + } + } + + return null; + } + /** * @return Element[] */