add set persistence
This commit is contained in:
parent
4ac01460fd
commit
5b9df7d02a
7 changed files with 167 additions and 0 deletions
|
|
@ -16,6 +16,8 @@ use App\Email\Emailer;
|
|||
use App\Email\EmailFactory;
|
||||
use App\Email\LaravelEmailer;
|
||||
use App\Email\LaravelEmailFactory;
|
||||
use App\Set\EloquentSetRepository;
|
||||
use App\Set\SetRepository;
|
||||
use App\User\EloquentUserRepository;
|
||||
use App\User\UserRepository;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
|
@ -45,6 +47,10 @@ class AppServiceProvider extends ServiceProvider
|
|||
);
|
||||
$this->app->bind(Emailer::class, LaravelEmailer::class);
|
||||
$this->app->bind(EmailFactory::class, LaravelEmailFactory::class);
|
||||
$this->app->bind(
|
||||
SetRepository::class,
|
||||
EloquentSetRepository::class,
|
||||
);
|
||||
$this->app->bind(PasswordHasher::class, BcryptPasswordHasher::class);
|
||||
$this->app->bind(TokenGenerator::class, RandomTokenGenerator::class);
|
||||
$this->app->bind(Clock::class, SystemClock::class);
|
||||
|
|
|
|||
13
backend/app/Set/CreateSetDto.php
Normal file
13
backend/app/Set/CreateSetDto.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace App\Set;
|
||||
|
||||
use App\User\User;
|
||||
|
||||
final readonly class CreateSetDto
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public User $creator,
|
||||
) {}
|
||||
}
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
29
backend/app/Set/Set.php
Normal file
29
backend/app/Set/Set.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Set;
|
||||
|
||||
use App\User\User;
|
||||
|
||||
final readonly class Set
|
||||
{
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private string $name,
|
||||
private User $creator,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getCreator(): User
|
||||
{
|
||||
return $this->creator;
|
||||
}
|
||||
}
|
||||
26
backend/app/Set/SetModel.php
Normal file
26
backend/app/Set/SetModel.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Set;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property int $creator_id
|
||||
*
|
||||
* @method static Builder<static>|SetModel newModelQuery()
|
||||
* @method static Builder<static>|SetModel newQuery()
|
||||
* @method static Builder<static>|SetModel query()
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
#[Fillable(['name', 'creator_id'])]
|
||||
class SetModel extends Model
|
||||
{
|
||||
protected $table = 'sets';
|
||||
|
||||
public $timestamps = false;
|
||||
}
|
||||
13
backend/app/Set/SetRepository.php
Normal file
13
backend/app/Set/SetRepository.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace App\Set;
|
||||
|
||||
interface SetRepository
|
||||
{
|
||||
public function create(CreateSetDto $dto): Set;
|
||||
|
||||
/**
|
||||
* @return list<Set>
|
||||
*/
|
||||
public function all(): array;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
<?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): void {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->foreignId('creator_id')
|
||||
->constrained('users')
|
||||
->restrictOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('sets');
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue