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