add element persistence
This commit is contained in:
parent
aaab0e5379
commit
fcc6ade8f3
7 changed files with 289 additions and 0 deletions
15
backend/app/Element/CreateElementDto.php
Normal file
15
backend/app/Element/CreateElementDto.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
use App\Set\Set;
|
||||
|
||||
final readonly class CreateElementDto
|
||||
{
|
||||
public function __construct(
|
||||
public Set $set,
|
||||
public string $name,
|
||||
public string $kind,
|
||||
public ?Element $parentElement,
|
||||
) {}
|
||||
}
|
||||
47
backend/app/Element/ElementModel.php
Normal file
47
backend/app/Element/ElementModel.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $set_id
|
||||
* @property string $name
|
||||
* @property string $kind
|
||||
* @property int|null $parent_element_id
|
||||
* @property int $position
|
||||
*
|
||||
* @method static Builder<static>|ElementModel newModelQuery()
|
||||
* @method static Builder<static>|ElementModel newQuery()
|
||||
* @method static Builder<static>|ElementModel query()
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
#[Fillable([
|
||||
'set_id',
|
||||
'name',
|
||||
'kind',
|
||||
'parent_element_id',
|
||||
'position',
|
||||
])]
|
||||
class ElementModel extends Model
|
||||
{
|
||||
protected $table = 'elements';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'set_id' => 'integer',
|
||||
'parent_element_id' => 'integer',
|
||||
'position' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
10
backend/app/Element/ElementRepository.php
Normal file
10
backend/app/Element/ElementRepository.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
interface ElementRepository
|
||||
{
|
||||
public function create(CreateElementDto $dto): Element;
|
||||
|
||||
public function find(int $id): ?Element;
|
||||
}
|
||||
98
backend/app/Element/EloquentElementRepository.php
Normal file
98
backend/app/Element/EloquentElementRepository.php
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
use App\Set\Set;
|
||||
use App\Set\SetRepository;
|
||||
use RuntimeException;
|
||||
|
||||
class EloquentElementRepository implements ElementRepository
|
||||
{
|
||||
public function __construct(
|
||||
private SetRepository $setRepository,
|
||||
) {}
|
||||
|
||||
public function create(CreateElementDto $dto): Element
|
||||
{
|
||||
$position = $this->nextPosition(
|
||||
$dto->set,
|
||||
$dto->parentElement,
|
||||
);
|
||||
$model = ElementModel::create([
|
||||
'set_id' => $dto->set->getId(),
|
||||
'name' => $dto->name,
|
||||
'kind' => $dto->kind,
|
||||
'parent_element_id' => $dto->parentElement?->getId(),
|
||||
'position' => $position,
|
||||
]);
|
||||
|
||||
return new Element(
|
||||
id: $model->id,
|
||||
name: $model->name,
|
||||
kind: $model->kind,
|
||||
set: $dto->set,
|
||||
parentElement: $dto->parentElement,
|
||||
position: $model->position,
|
||||
);
|
||||
}
|
||||
|
||||
public function find(int $id): ?Element
|
||||
{
|
||||
$model = ElementModel::find($id);
|
||||
|
||||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
private function nextPosition(
|
||||
Set $set,
|
||||
?Element $parentElement,
|
||||
): int {
|
||||
$query = ElementModel::query()
|
||||
->where('set_id', $set->getId());
|
||||
if ($parentElement === null) {
|
||||
$query->whereNull('parent_element_id');
|
||||
} else {
|
||||
$query->where('parent_element_id', $parentElement->getId());
|
||||
}
|
||||
|
||||
$currentMaximum = $query->max('position');
|
||||
if ($currentMaximum === null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return (int) $currentMaximum + 1;
|
||||
}
|
||||
|
||||
private function toDomain(ElementModel $model): Element
|
||||
{
|
||||
$set = $this->findSet($model->set_id);
|
||||
|
||||
$parentElement = null;
|
||||
if ($model->parent_element_id !== null) {
|
||||
$parentElement = $this->find($model->parent_element_id);
|
||||
if ($parentElement === null) {
|
||||
throw new RuntimeException('element parent not found');
|
||||
}
|
||||
}
|
||||
|
||||
return new Element(
|
||||
id: $model->id,
|
||||
name: $model->name,
|
||||
kind: $model->kind,
|
||||
set: $set,
|
||||
parentElement: $parentElement,
|
||||
position: $model->position,
|
||||
);
|
||||
}
|
||||
|
||||
private function findSet(int $id): Set
|
||||
{
|
||||
foreach ($this->setRepository->all() as $set) {
|
||||
if ($set->getId() === $id) {
|
||||
return $set;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException('element set not found');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue