diff --git a/src/app/Domain/Shared/Casts/UuidCast.php b/src/app/Domain/Shared/Casts/UuidCast.php new file mode 100644 index 0000000..355248b --- /dev/null +++ b/src/app/Domain/Shared/Casts/UuidCast.php @@ -0,0 +1,42 @@ + null]; + } + + if ($value instanceof UuidInterface) { + return [$key => $value->toString()]; + } + + return [$key => Uuid::fromString((string) $value)->toString()]; + } + + public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed + { + return Uuid::fromString((string) $value); + } +} \ No newline at end of file diff --git a/src/app/Domain/Shared/Repositories/BaseRepository.php b/src/app/Domain/Shared/Repositories/BaseRepository.php index 4b1bba3..e9f5f5f 100644 --- a/src/app/Domain/Shared/Repositories/BaseRepository.php +++ b/src/app/Domain/Shared/Repositories/BaseRepository.php @@ -43,4 +43,3 @@ abstract class BaseRepository extends Model return $model->newQuery(); } } - diff --git a/src/app/Domain/Shared/ValueObjects/Uuid.php b/src/app/Domain/Shared/ValueObjects/Uuid.php new file mode 100644 index 0000000..9ac50a4 --- /dev/null +++ b/src/app/Domain/Shared/ValueObjects/Uuid.php @@ -0,0 +1,59 @@ +value->toString(); + } + + public function equals(self $other): bool + { + return $this->value->equals($other); + } + + public function __toString(): string + { + return $this->toString(); + } + + public static function validate(string|array $uuid): bool + { + $uuids = is_array($uuid) ? $uuid : [$uuid]; + + foreach ($uuids as $u) { + if (!RamseyUuid::isValid($u)) { + return false; + } + } + return true; + } + + +} diff --git a/src/app/Domain/User/Actions/ShowAction.php b/src/app/Domain/User/Actions/ShowAction.php index 49e5ac6..6725c25 100644 --- a/src/app/Domain/User/Actions/ShowAction.php +++ b/src/app/Domain/User/Actions/ShowAction.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Domain\User\Actions; use App\Domain\User\Data\ShowRequest; +use App\Domain\User\Data\ShowResponseData; use App\Domain\User\Repositories\UserRepository; use App\Models\User; @@ -16,8 +17,9 @@ class ShowAction } - public function execute(ShowRequest $request): User + public function execute(ShowRequest $request) { - return $this->userRepository->whereUuid($request->user_uuid)->firstOrFail(); + $result = $this->userRepository->whereUuid($request->user_uuid)->firstOrFail(); + return ShowResponseData::fromModel($result); } } diff --git a/src/app/Domain/User/Data/ShowResponseData.php b/src/app/Domain/User/Data/ShowResponseData.php new file mode 100644 index 0000000..db15ba4 --- /dev/null +++ b/src/app/Domain/User/Data/ShowResponseData.php @@ -0,0 +1,31 @@ +uuid->toString(), + name: $model->name, + role: new RoleData( + uuid: $model->role->uuid->toString(), + name: $model->role->name, + ) + ); + } +} diff --git a/src/app/Domain/User/Data/ValueObjects/RoleData.php b/src/app/Domain/User/Data/ValueObjects/RoleData.php new file mode 100644 index 0000000..8196598 --- /dev/null +++ b/src/app/Domain/User/Data/ValueObjects/RoleData.php @@ -0,0 +1,16 @@ + UuidCast::class, + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ]; + public function users() { return $this->hasMany(User::class); diff --git a/src/app/Models/User.php b/src/app/Models/User.php index 12aecd9..27471dd 100644 --- a/src/app/Models/User.php +++ b/src/app/Models/User.php @@ -6,6 +6,9 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; + +use App\Domain\Shared\Casts\UuidCast; +use App\Domain\Shared\ValueObjects\Uuid; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -13,7 +16,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; /** - * @property string $uuid + * @property Uuid $uuid * @property string $name * @property string $email * @property string $password @@ -59,6 +62,7 @@ class User extends Authenticatable protected function casts(): array { return [ + 'uuid' => UuidCast::class, 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; diff --git a/src/resources/views/pages/user/show.blade.php b/src/resources/views/pages/user/show.blade.php index e5a29c5..481a456 100644 --- a/src/resources/views/pages/user/show.blade.php +++ b/src/resources/views/pages/user/show.blade.php @@ -1,7 +1,7 @@ @extends ('layouts.app') @section ('content') -

{{ $data }}

+

{{ dd($data) }}