add set root ids

This commit is contained in:
Yisroel Baum 2026-05-25 21:42:38 +03:00
parent a87c98a729
commit 7736b88802
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 36 additions and 3 deletions

View file

@ -2,14 +2,17 @@
namespace App\Controllers; namespace App\Controllers;
use App\Element\ElementRepository;
use App\Set\Set as DomainSet; use App\Set\Set as DomainSet;
use App\Set\SetRepository; use App\Set\SetRepository;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
class SetController class SetController
{ {
public function __construct(private SetRepository $setRepository) public function __construct(
{ private SetRepository $setRepository,
private ElementRepository $elementRepository,
) {
} }
public function index(): JsonResponse public function index(): JsonResponse
@ -29,16 +32,20 @@ class SetController
* id: int, * id: int,
* name: string, * name: string,
* description: string, * description: string,
* iconImageUrl: string * iconImageUrl: string,
* rootElementId: int|null
* } * }
*/ */
private function buildSetPayload(DomainSet $set): array private function buildSetPayload(DomainSet $set): array
{ {
$rootElement = $this->elementRepository->findRootBySet($set);
return [ return [
'id' => $set->getId(), 'id' => $set->getId(),
'name' => $set->getName(), 'name' => $set->getName(),
'description' => $set->getDescription(), 'description' => $set->getDescription(),
'iconImageUrl' => $set->getIconImageUrl(), 'iconImageUrl' => $set->getIconImageUrl(),
'rootElementId' => $rootElement?->getId(),
]; ];
} }
} }

View file

@ -10,6 +10,8 @@ interface ElementRepository
public function find(int $id): ?Element; public function find(int $id): ?Element;
public function findRootBySet(DomainSet $set): ?Element;
/** /**
* @return Element[] * @return Element[]
*/ */

View file

@ -35,6 +35,16 @@ class EloquentElementRepository implements ElementRepository
return $model === null ? null : $this->toDomain($model); 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[] * @return Element[]
*/ */

View file

@ -37,6 +37,20 @@ class FakeElementRepository implements ElementRepository
return $this->cloneElement($this->elementsById[$id]); 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[] * @return Element[]
*/ */