copy user entity and auth from ysv
This commit is contained in:
parent
9d5bfc33a6
commit
613180d459
24 changed files with 612 additions and 0 deletions
13
backend/app/User/CreateUserDto.php
Normal file
13
backend/app/User/CreateUserDto.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
|
||||
class CreateUserDto
|
||||
{
|
||||
public function __construct(
|
||||
public EmailAddress $email,
|
||||
public string $passwordHash,
|
||||
) {}
|
||||
}
|
||||
52
backend/app/User/EloquentUserRepository.php
Normal file
52
backend/app/User/EloquentUserRepository.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
|
||||
class EloquentUserRepository implements UserRepository
|
||||
{
|
||||
public function create(CreateUserDto $dto): User
|
||||
{
|
||||
$model = UserModel::create([
|
||||
'email' => $dto->email->value(),
|
||||
'password_hash' => $dto->passwordHash,
|
||||
]);
|
||||
|
||||
return $this->toDomain($model);
|
||||
}
|
||||
|
||||
public function findByEmail(EmailAddress $email): ?User
|
||||
{
|
||||
$model = UserModel::where('email', $email->value())->first();
|
||||
|
||||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
public function findByEmailDomain(string $domain): array
|
||||
{
|
||||
$models = UserModel::where('email', 'like', '%@'.$domain)->get();
|
||||
$users = [];
|
||||
foreach ($models as $model) {
|
||||
$users[] = $this->toDomain($model);
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
public function find(int $id): ?User
|
||||
{
|
||||
$model = UserModel::find($id);
|
||||
|
||||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
private function toDomain(UserModel $model): User
|
||||
{
|
||||
return new User(
|
||||
id: $model->id,
|
||||
email: new EmailAddress($model->email),
|
||||
passwordHash: $model->password_hash,
|
||||
);
|
||||
}
|
||||
}
|
||||
29
backend/app/User/User.php
Normal file
29
backend/app/User/User.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private EmailAddress $email,
|
||||
private string $passwordHash,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getEmail(): EmailAddress
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function getPasswordHash(): string
|
||||
{
|
||||
return $this->passwordHash;
|
||||
}
|
||||
}
|
||||
27
backend/app/User/UserModel.php
Normal file
27
backend/app/User/UserModel.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $email
|
||||
* @property string $password_hash
|
||||
*
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel query()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel whereEmail($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|UserModel whereId($value)
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class UserModel extends Model
|
||||
{
|
||||
protected $table = 'users';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = ['email', 'password_hash'];
|
||||
}
|
||||
19
backend/app/User/UserRepository.php
Normal file
19
backend/app/User/UserRepository.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
|
||||
interface UserRepository
|
||||
{
|
||||
public function create(CreateUserDto $dto): User;
|
||||
|
||||
public function findByEmail(EmailAddress $email): ?User;
|
||||
|
||||
/**
|
||||
* @return User[]
|
||||
*/
|
||||
public function findByEmailDomain(string $domain): array;
|
||||
|
||||
public function find(int $id): ?User;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue