2023-07-06 10:48:32 +06:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2023-08-01 22:04:35 +06:00
|
|
|
use App\Contracts\Search;
|
2023-07-06 10:48:32 +06:00
|
|
|
use App\Models\User;
|
2023-08-01 22:04:35 +06:00
|
|
|
use App\Dto\Builder\User as UserBuilderDto;
|
|
|
|
use App\Services\User\BuilderCommand;
|
|
|
|
use App\Services\Search\CreateSearchInstanceCommand;
|
2023-07-06 10:48:32 +06:00
|
|
|
use Illuminate\Support\Str;
|
2023-08-01 22:04:35 +06:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-07-06 10:48:32 +06:00
|
|
|
|
|
|
|
final readonly class UserRepository
|
|
|
|
{
|
2023-08-01 22:04:35 +06:00
|
|
|
public function __construct(
|
|
|
|
private CreateSearchInstanceCommand $createSearchInstanceCommand,
|
|
|
|
private BuilderCommand $builderCommand
|
|
|
|
) { }
|
|
|
|
|
|
|
|
public function getUserById(int $id): ?User
|
|
|
|
{
|
|
|
|
return User::query()->where('id', $id)->first();
|
|
|
|
}
|
|
|
|
|
2023-07-06 10:48:32 +06:00
|
|
|
public function getUserByEmail(string $email): ?User
|
|
|
|
{
|
|
|
|
return User::query()->where('email', Str::lower($email))->first();
|
|
|
|
}
|
2023-08-01 22:04:35 +06:00
|
|
|
|
|
|
|
public function getUsers(UserBuilderDto $userBuilderDto, array $with = []): Search
|
|
|
|
{
|
|
|
|
$query = $this->builderCommand->execute(
|
|
|
|
query: User::query()->with($with),
|
|
|
|
userBuilderDto: $userBuilderDto
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->createSearchInstanceCommand->execute($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isExistsEmail(string $email, ?int $exceptId = null): bool
|
|
|
|
{
|
|
|
|
return User::query()
|
|
|
|
->where('email', Str::lower($email))
|
|
|
|
->when($exceptId, function (Builder $query, int $exceptId) {
|
|
|
|
$query->where('id', '!=', $exceptId);
|
|
|
|
})
|
|
|
|
->withTrashed()
|
|
|
|
->exists();
|
|
|
|
}
|
2023-07-06 10:48:32 +06:00
|
|
|
}
|