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,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,10 @@ namespace App\Providers;
|
||||||
|
|
||||||
use App\Auth\EloquentSessionRepository;
|
use App\Auth\EloquentSessionRepository;
|
||||||
use App\Auth\SessionRepository;
|
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\EloquentUserRepository;
|
||||||
use App\User\UserRepository;
|
use App\User\UserRepository;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
@ -20,5 +24,13 @@ class RepositoryServiceProvider extends ServiceProvider
|
||||||
SessionRepository::class,
|
SessionRepository::class,
|
||||||
EloquentSessionRepository::class
|
EloquentSessionRepository::class
|
||||||
);
|
);
|
||||||
|
$this->app->bind(
|
||||||
|
SetRepository::class,
|
||||||
|
EloquentSetRepository::class
|
||||||
|
);
|
||||||
|
$this->app->bind(
|
||||||
|
ElementRepository::class,
|
||||||
|
EloquentElementRepository::class
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
backend/app/Set/CreateSetDto.php
Normal file
10
backend/app/Set/CreateSetDto.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set;
|
||||||
|
|
||||||
|
class CreateSetDto
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $name,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
41
backend/app/Set/EloquentSetRepository.php
Normal file
41
backend/app/Set/EloquentSetRepository.php
Normal 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
21
backend/app/Set/Set.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
backend/app/Set/SetModel.php
Normal file
27
backend/app/Set/SetModel.php
Normal 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'];
|
||||||
|
}
|
||||||
15
backend/app/Set/SetRepository.php
Normal file
15
backend/app/Set/SetRepository.php
Normal 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;
|
||||||
|
}
|
||||||
40
backend/app/Set/UseCases/CreateSet/CreateSet.php
Normal file
40
backend/app/Set/UseCases/CreateSet/CreateSet.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
backend/app/Set/UseCases/CreateSet/CreateSetRequest.php
Normal file
10
backend/app/Set/UseCases/CreateSet/CreateSetRequest.php
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Set\UseCases\CreateSet;
|
||||||
|
|
||||||
|
class CreateSetRequest
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public ?string $name,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
21
backend/database/migrations/2026_05_24_000000_sets_table.php
Normal file
21
backend/database/migrations/2026_05_24_000000_sets_table.php
Normal 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');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -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');
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue