add ordered element queries
This commit is contained in:
parent
88c8a018ad
commit
503d868e29
3 changed files with 115 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Element;
|
namespace App\Element;
|
||||||
|
|
||||||
|
use App\Set\Set;
|
||||||
use DomainException;
|
use DomainException;
|
||||||
|
|
||||||
interface ElementRepository
|
interface ElementRepository
|
||||||
|
|
@ -12,4 +13,14 @@ interface ElementRepository
|
||||||
public function create(CreateElementDto $dto): Element;
|
public function create(CreateElementDto $dto): Element;
|
||||||
|
|
||||||
public function find(int $id): ?Element;
|
public function find(int $id): ?Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<Element>
|
||||||
|
*/
|
||||||
|
public function findTopLevelBySet(Set $set): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<Element>
|
||||||
|
*/
|
||||||
|
public function findByParentElement(Element $parentElement): array;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,48 @@ class EloquentElementRepository implements ElementRepository
|
||||||
return $model === null ? null : $this->toDomain($model);
|
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
|
* @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(
|
return new Element(
|
||||||
id: $model->id,
|
id: $model->id,
|
||||||
name: $model->name,
|
name: $model->name,
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace Tests\Fakes;
|
||||||
use App\Element\CreateElementDto;
|
use App\Element\CreateElementDto;
|
||||||
use App\Element\Element;
|
use App\Element\Element;
|
||||||
use App\Element\ElementRepository;
|
use App\Element\ElementRepository;
|
||||||
|
use App\Set\Set;
|
||||||
use DomainException;
|
use DomainException;
|
||||||
|
|
||||||
class FakeElementRepository implements ElementRepository
|
class FakeElementRepository implements ElementRepository
|
||||||
|
|
@ -38,6 +39,34 @@ class FakeElementRepository implements ElementRepository
|
||||||
return $element === null ? null : $this->copy($element);
|
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
|
* @throws DomainException
|
||||||
*/
|
*/
|
||||||
|
|
@ -79,6 +108,27 @@ class FakeElementRepository implements ElementRepository
|
||||||
return $maximumPosition + 1;
|
return $maximumPosition + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, Element> $elements
|
||||||
|
* @return list<Element>
|
||||||
|
*/
|
||||||
|
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
|
private function copy(Element $element): Element
|
||||||
{
|
{
|
||||||
$parentElement = $element->getParentElement();
|
$parentElement = $element->getParentElement();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue