add set elements
This commit is contained in:
parent
db35a97910
commit
2c4cdabc15
17 changed files with 497 additions and 0 deletions
14
backend/app/Element/CreateElementDto.php
Normal file
14
backend/app/Element/CreateElementDto.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
use App\Set\Set;
|
||||
|
||||
class CreateElementDto
|
||||
{
|
||||
public function __construct(
|
||||
public Set $set,
|
||||
public string $title,
|
||||
public ?Element $parentElement,
|
||||
) {}
|
||||
}
|
||||
35
backend/app/Element/Element.php
Normal file
35
backend/app/Element/Element.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
use App\Set\Set;
|
||||
|
||||
class Element
|
||||
{
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private string $title,
|
||||
private Set $set,
|
||||
private ?Element $parentElement,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getSet(): Set
|
||||
{
|
||||
return $this->set;
|
||||
}
|
||||
|
||||
public function getParentElement(): ?Element
|
||||
{
|
||||
return $this->parentElement;
|
||||
}
|
||||
}
|
||||
40
backend/app/Element/ElementModel.php
Normal file
40
backend/app/Element/ElementModel.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $set_id
|
||||
* @property string $title
|
||||
* @property int|null $parent_element_id
|
||||
*
|
||||
* @method static Builder<static>|ElementModel newModelQuery()
|
||||
* @method static Builder<static>|ElementModel newQuery()
|
||||
* @method static Builder<static>|ElementModel query()
|
||||
* @method static Builder<static>|ElementModel whereId($value)
|
||||
* @method static Builder<static>|ElementModel whereParentElementId($value)
|
||||
* @method static Builder<static>|ElementModel whereSetId($value)
|
||||
* @method static Builder<static>|ElementModel whereTitle($value)
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class ElementModel extends Model
|
||||
{
|
||||
protected $table = 'elements';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'set_id',
|
||||
'title',
|
||||
'parent_element_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'set_id' => 'integer',
|
||||
'parent_element_id' => 'integer',
|
||||
];
|
||||
}
|
||||
15
backend/app/Element/ElementRepository.php
Normal file
15
backend/app/Element/ElementRepository.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
interface ElementRepository
|
||||
{
|
||||
public function create(CreateElementDto $dto): Element;
|
||||
|
||||
public function find(int $id): ?Element;
|
||||
|
||||
/**
|
||||
* @return Element[]
|
||||
*/
|
||||
public function findBySetId(int $id): array;
|
||||
}
|
||||
75
backend/app/Element/EloquentElementRepository.php
Normal file
75
backend/app/Element/EloquentElementRepository.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element;
|
||||
|
||||
use App\Set\SetRepository;
|
||||
use DomainException;
|
||||
|
||||
class EloquentElementRepository implements ElementRepository
|
||||
{
|
||||
public function __construct(private SetRepository $setRepo) {}
|
||||
|
||||
public function create(CreateElementDto $dto): Element
|
||||
{
|
||||
$model = ElementModel::create([
|
||||
'set_id' => $dto->set->getId(),
|
||||
'title' => $dto->title,
|
||||
'parent_element_id' => $dto->parentElement?->getId(),
|
||||
]);
|
||||
|
||||
return new Element(
|
||||
id: $model->id,
|
||||
title: $dto->title,
|
||||
set: $dto->set,
|
||||
parentElement: $dto->parentElement,
|
||||
);
|
||||
}
|
||||
|
||||
public function find(int $id): ?Element
|
||||
{
|
||||
$model = ElementModel::find($id);
|
||||
|
||||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element[]
|
||||
*/
|
||||
public function findBySetId(int $id): array
|
||||
{
|
||||
$models = ElementModel::where('set_id', $id)->orderBy('id')->get();
|
||||
$elements = [];
|
||||
foreach ($models as $model) {
|
||||
$elements[] = $this->toDomain($model);
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
private function toDomain(ElementModel $model): Element
|
||||
{
|
||||
$set = $this->setRepo->find($model->set_id);
|
||||
if ($set === null) {
|
||||
throw new DomainException(
|
||||
"Set with id: {$model->set_id} doesnt exist"
|
||||
);
|
||||
}
|
||||
|
||||
$parentElement = null;
|
||||
if ($model->parent_element_id !== null) {
|
||||
$parentElement = $this->find($model->parent_element_id);
|
||||
if ($parentElement === null) {
|
||||
throw new DomainException(
|
||||
"Element with id: {$model->parent_element_id} doesnt exist"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new Element(
|
||||
id: $model->id,
|
||||
title: $model->title,
|
||||
set: $set,
|
||||
parentElement: $parentElement,
|
||||
);
|
||||
}
|
||||
}
|
||||
84
backend/app/Element/UseCases/CreateElement/CreateElement.php
Normal file
84
backend/app/Element/UseCases/CreateElement/CreateElement.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\CreateElement;
|
||||
|
||||
use App\Element\CreateElementDto;
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Set\SetRepository;
|
||||
use DomainException;
|
||||
|
||||
class CreateElement
|
||||
{
|
||||
public function __construct(
|
||||
private ElementRepository $elementRepo,
|
||||
private SetRepository $setRepo,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function execute(CreateElementRequest $request): Element
|
||||
{
|
||||
if ($request->setId === null) {
|
||||
throw new BadRequestException('setId is required');
|
||||
}
|
||||
if ($request->title === null || $request->title === '') {
|
||||
throw new BadRequestException('title is required');
|
||||
}
|
||||
|
||||
$set = $this->setRepo->find($request->setId);
|
||||
if ($set === null) {
|
||||
throw new DomainException(
|
||||
"Set with id: {$request->setId} doesnt exist"
|
||||
);
|
||||
}
|
||||
|
||||
if ($request->parentElementId === null) {
|
||||
$this->validateNoRootElementExists($request->setId);
|
||||
|
||||
return $this->elementRepo->create(new CreateElementDto(
|
||||
set: $set,
|
||||
title: $request->title,
|
||||
parentElement: null,
|
||||
));
|
||||
}
|
||||
|
||||
$parentElement = $this->elementRepo->find(
|
||||
$request->parentElementId
|
||||
);
|
||||
if ($parentElement === null) {
|
||||
throw new DomainException(
|
||||
"Element with id: {$request->parentElementId} doesnt exist"
|
||||
);
|
||||
}
|
||||
if ($parentElement->getSet()->getId() !== $set->getId()) {
|
||||
throw new DomainException(
|
||||
'Parent element must belong to the same set'
|
||||
);
|
||||
}
|
||||
|
||||
return $this->elementRepo->create(new CreateElementDto(
|
||||
set: $set,
|
||||
title: $request->title,
|
||||
parentElement: $parentElement,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DomainException
|
||||
*/
|
||||
private function validateNoRootElementExists(int $setId): void
|
||||
{
|
||||
$elements = $this->elementRepo->findBySetId($setId);
|
||||
foreach ($elements as $element) {
|
||||
if ($element->getParentElement() === null) {
|
||||
throw new DomainException(
|
||||
'A root element already exists for this set'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\CreateElement;
|
||||
|
||||
class CreateElementRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $setId,
|
||||
public ?string $title,
|
||||
public ?int $parentElementId,
|
||||
) {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue