diff --git a/backend/app/Element/ElementRepository.php b/backend/app/Element/ElementRepository.php index 86631bd..fc46687 100644 --- a/backend/app/Element/ElementRepository.php +++ b/backend/app/Element/ElementRepository.php @@ -2,6 +2,7 @@ namespace App\Element; +use App\Set\Set; use DomainException; interface ElementRepository @@ -12,4 +13,14 @@ interface ElementRepository public function create(CreateElementDto $dto): Element; public function find(int $id): ?Element; + + /** + * @return list + */ + public function findTopLevelBySet(Set $set): array; + + /** + * @return list + */ + public function findByParentElement(Element $parentElement): array; } diff --git a/backend/app/Element/EloquentElementRepository.php b/backend/app/Element/EloquentElementRepository.php index 3b8bc7b..cfd93f7 100644 --- a/backend/app/Element/EloquentElementRepository.php +++ b/backend/app/Element/EloquentElementRepository.php @@ -45,6 +45,48 @@ class EloquentElementRepository implements ElementRepository return $model === null ? null : $this->toDomain($model); } + public function findTopLevelBySet(Set $set): array + { + $models = ElementModel::query() + ->where('set_id', $set->getId()) + ->whereNull('parent_element_id') + ->orderBy('position') + ->orderBy('id') + ->get(); + $elements = []; + + foreach ($models as $model) { + $elements[] = $this->toDomainWithRelations( + model: $model, + set: $set, + parentElement: null, + ); + } + + return $elements; + } + + public function findByParentElement(Element $parentElement): array + { + $models = ElementModel::query() + ->where('set_id', $parentElement->getSet()->getId()) + ->where('parent_element_id', $parentElement->getId()) + ->orderBy('position') + ->orderBy('id') + ->get(); + $elements = []; + + foreach ($models as $model) { + $elements[] = $this->toDomainWithRelations( + model: $model, + set: $parentElement->getSet(), + parentElement: $parentElement, + ); + } + + return $elements; + } + /** * @throws DomainException */ @@ -94,6 +136,18 @@ class EloquentElementRepository implements ElementRepository } } + return $this->toDomainWithRelations( + model: $model, + set: $set, + parentElement: $parentElement, + ); + } + + private function toDomainWithRelations( + ElementModel $model, + Set $set, + ?Element $parentElement, + ): Element { return new Element( id: $model->id, name: $model->name, diff --git a/backend/tests/Fakes/FakeElementRepository.php b/backend/tests/Fakes/FakeElementRepository.php index ebe197d..e44cb53 100644 --- a/backend/tests/Fakes/FakeElementRepository.php +++ b/backend/tests/Fakes/FakeElementRepository.php @@ -5,6 +5,7 @@ namespace Tests\Fakes; use App\Element\CreateElementDto; use App\Element\Element; use App\Element\ElementRepository; +use App\Set\Set; use DomainException; class FakeElementRepository implements ElementRepository @@ -38,6 +39,34 @@ class FakeElementRepository implements ElementRepository return $element === null ? null : $this->copy($element); } + public function findTopLevelBySet(Set $set): array + { + $elements = array_filter( + $this->elements, + function (Element $element) use ($set): bool { + return $element->getSet()->getId() === $set->getId() + && $element->getParentElement() === null; + }, + ); + + return $this->orderedCopies($elements); + } + + public function findByParentElement(Element $parentElement): array + { + $elements = array_filter( + $this->elements, + function (Element $element) use ($parentElement): bool { + return $element->getSet()->getId() + === $parentElement->getSet()->getId() + && $element->getParentElement()?->getId() + === $parentElement->getId(); + }, + ); + + return $this->orderedCopies($elements); + } + /** * @throws DomainException */ @@ -79,6 +108,27 @@ class FakeElementRepository implements ElementRepository return $maximumPosition + 1; } + /** + * @param array $elements + * @return list + */ + private function orderedCopies(array $elements): array + { + usort($elements, function (Element $first, Element $second): int { + $positionComparison = $first->getPosition() + <=> $second->getPosition(); + if ($positionComparison !== 0) { + return $positionComparison; + } + + return $first->getId() <=> $second->getId(); + }); + + return array_map(function (Element $element): Element { + return $this->copy($element); + }, $elements); + } + private function copy(Element $element): Element { $parentElement = $element->getParentElement();