add set elements

This commit is contained in:
Yisroel Baum 2026-05-24 22:32:46 +03:00
parent db35a97910
commit 2c4cdabc15
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
17 changed files with 497 additions and 0 deletions

View 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,
) {}
}

View 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;
}
}

View 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',
];
}

View 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;
}

View 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,
);
}
}

View 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'
);
}
}
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace App\Element\UseCases\CreateElement;
class CreateElementRequest
{
public function __construct(
public ?int $setId,
public ?string $title,
public ?int $parentElementId,
) {}
}

View file

@ -4,6 +4,10 @@ namespace App\Providers;
use App\Auth\EloquentSessionRepository;
use App\Auth\SessionRepository;
use App\Element\ElementRepository;
use App\Element\EloquentElementRepository;
use App\Set\EloquentSetRepository;
use App\Set\SetRepository;
use App\User\EloquentUserRepository;
use App\User\UserRepository;
use Illuminate\Support\ServiceProvider;
@ -20,5 +24,13 @@ class RepositoryServiceProvider extends ServiceProvider
SessionRepository::class,
EloquentSessionRepository::class
);
$this->app->bind(
SetRepository::class,
EloquentSetRepository::class
);
$this->app->bind(
ElementRepository::class,
EloquentElementRepository::class
);
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace App\Set;
class CreateSetDto
{
public function __construct(
public string $name,
) {}
}

View file

@ -0,0 +1,41 @@
<?php
namespace App\Set;
class EloquentSetRepository implements SetRepository
{
public function create(CreateSetDto $dto): Set
{
$model = SetModel::create([
'name' => $dto->name,
]);
return $this->toDomain($model);
}
public function find(int $id): ?Set
{
$model = SetModel::find($id);
return $model === null ? null : $this->toDomain($model);
}
public function getAll(): array
{
$models = SetModel::orderBy('id')->get();
$sets = [];
foreach ($models as $model) {
$sets[] = $this->toDomain($model);
}
return $sets;
}
private function toDomain(SetModel $model): Set
{
return new Set(
id: $model->id,
name: $model->name,
);
}
}

21
backend/app/Set/Set.php Normal file
View file

@ -0,0 +1,21 @@
<?php
namespace App\Set;
class Set
{
public function __construct(
private int $id,
private string $name,
) {}
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace App\Set;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $name
*
* @method static Builder<static>|SetModel newModelQuery()
* @method static Builder<static>|SetModel newQuery()
* @method static Builder<static>|SetModel query()
* @method static Builder<static>|SetModel whereId($value)
* @method static Builder<static>|SetModel whereName($value)
*
* @mixin \Eloquent
*/
class SetModel extends Model
{
protected $table = 'sets';
public $timestamps = false;
protected $fillable = ['name'];
}

View file

@ -0,0 +1,15 @@
<?php
namespace App\Set;
interface SetRepository
{
public function create(CreateSetDto $dto): Set;
public function find(int $id): ?Set;
/**
* @return Set[]
*/
public function getAll(): array;
}

View file

@ -0,0 +1,40 @@
<?php
namespace App\Set\UseCases\CreateSet;
use App\Element\CreateElementDto;
use App\Element\ElementRepository;
use App\Exceptions\BadRequestException;
use App\Set\CreateSetDto;
use App\Set\Set;
use App\Set\SetRepository;
class CreateSet
{
public function __construct(
private SetRepository $setRepo,
private ElementRepository $elementRepo,
) {}
/**
* @throws BadRequestException
*/
public function execute(CreateSetRequest $request): Set
{
if ($request->name === null || $request->name === '') {
throw new BadRequestException('name is required');
}
$set = $this->setRepo->create(new CreateSetDto(
name: $request->name,
));
$this->elementRepo->create(new CreateElementDto(
set: $set,
title: $set->getName(),
parentElement: null,
));
return $set;
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace App\Set\UseCases\CreateSet;
class CreateSetRequest
{
public function __construct(
public ?string $name,
) {}
}

View file

@ -0,0 +1,21 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('sets', function (Blueprint $table) {
$table->id();
$table->string('name');
});
}
public function down(): void
{
Schema::dropIfExists('sets');
}
};

View file

@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('elements', function (Blueprint $table) {
$table->id();
$table->foreignId('set_id')->constrained('sets');
$table->string('title');
$table->foreignId('parent_element_id')
->nullable()
->constrained('elements');
});
}
public function down(): void
{
Schema::dropIfExists('elements');
}
};