find elements by set
This commit is contained in:
parent
aaa494afe4
commit
b44830fa53
3 changed files with 12 additions and 6 deletions
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Element;
|
||||
|
||||
use App\Set\Set as DomainSet;
|
||||
|
||||
interface ElementRepository
|
||||
{
|
||||
public function create(CreateElementDto $dto): Element;
|
||||
|
|
@ -11,5 +13,5 @@ interface ElementRepository
|
|||
/**
|
||||
* @return Element[]
|
||||
*/
|
||||
public function findBySetId(int $id): array;
|
||||
public function findBySet(DomainSet $set): array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Element;
|
||||
|
||||
use App\Set\Set as DomainSet;
|
||||
use App\Set\SetRepository;
|
||||
use DomainException;
|
||||
|
||||
|
|
@ -35,9 +36,11 @@ class EloquentElementRepository implements ElementRepository
|
|||
/**
|
||||
* @return Element[]
|
||||
*/
|
||||
public function findBySetId(int $id): array
|
||||
public function findBySet(DomainSet $set): array
|
||||
{
|
||||
$models = ElementModel::where('set_id', $id)->orderBy('id')->get();
|
||||
$models = ElementModel::where('set_id', $set->getId())
|
||||
->orderBy('id')
|
||||
->get();
|
||||
$elements = [];
|
||||
foreach ($models as $model) {
|
||||
$elements[] = $this->toDomain($model);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use App\Element\CreateElementDto;
|
|||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Set\Set as DomainSet;
|
||||
use App\Set\SetRepository;
|
||||
use DomainException;
|
||||
|
||||
|
|
@ -37,7 +38,7 @@ class CreateElement
|
|||
}
|
||||
|
||||
if ($request->parentElementId === null) {
|
||||
$this->validateNoRootElementExists($request->setId);
|
||||
$this->validateNoRootElementExists($set);
|
||||
|
||||
return $this->elementRepo->create(new CreateElementDto(
|
||||
set: $set,
|
||||
|
|
@ -70,9 +71,9 @@ class CreateElement
|
|||
/**
|
||||
* @throws DomainException
|
||||
*/
|
||||
private function validateNoRootElementExists(int $setId): void
|
||||
private function validateNoRootElementExists(DomainSet $set): void
|
||||
{
|
||||
$elements = $this->elementRepo->findBySetId($setId);
|
||||
$elements = $this->elementRepo->findBySet($set);
|
||||
foreach ($elements as $element) {
|
||||
if ($element->getParentElement() === null) {
|
||||
throw new DomainException(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue