add set elements
This commit is contained in:
parent
db35a97910
commit
2c4cdabc15
17 changed files with 497 additions and 0 deletions
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,
|
||||
) {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue