add set persistence
This commit is contained in:
parent
4ac01460fd
commit
5b9df7d02a
7 changed files with 167 additions and 0 deletions
56
backend/app/Set/EloquentSetRepository.php
Normal file
56
backend/app/Set/EloquentSetRepository.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace App\Set;
|
||||
|
||||
use App\User\UserRepository;
|
||||
use RuntimeException;
|
||||
|
||||
class EloquentSetRepository implements SetRepository
|
||||
{
|
||||
public function __construct(
|
||||
private UserRepository $userRepository,
|
||||
) {}
|
||||
|
||||
public function create(CreateSetDto $dto): Set
|
||||
{
|
||||
$model = SetModel::create([
|
||||
'name' => $dto->name,
|
||||
'creator_id' => $dto->creator->getId(),
|
||||
]);
|
||||
|
||||
return new Set(
|
||||
id: $model->id,
|
||||
name: $model->name,
|
||||
creator: $dto->creator,
|
||||
);
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
$models = SetModel::query()
|
||||
->orderBy('name')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
$sets = [];
|
||||
|
||||
foreach ($models as $model) {
|
||||
$sets[] = $this->toDomain($model);
|
||||
}
|
||||
|
||||
return $sets;
|
||||
}
|
||||
|
||||
private function toDomain(SetModel $model): Set
|
||||
{
|
||||
$creator = $this->userRepository->find($model->creator_id);
|
||||
if ($creator === null) {
|
||||
throw new RuntimeException('set creator not found');
|
||||
}
|
||||
|
||||
return new Set(
|
||||
id: $model->id,
|
||||
name: $model->name,
|
||||
creator: $creator,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue