2023-07-02 16:17:18 +06:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2023-07-16 19:21:09 +06:00
|
|
|
use App\Contracts\Search;
|
2023-07-02 16:17:18 +06:00
|
|
|
use App\Models\Role;
|
2023-07-16 19:21:09 +06:00
|
|
|
use App\Dto\Builder\Role as RoleBuilderDto;
|
|
|
|
use App\Services\Role\BuilderCommand;
|
|
|
|
use App\Services\Search\CreateSearchInstanceCommand;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-07-02 16:17:18 +06:00
|
|
|
|
|
|
|
final readonly class RoleRepository
|
|
|
|
{
|
2023-08-01 22:09:06 +06:00
|
|
|
public function __construct(
|
2023-07-16 19:21:09 +06:00
|
|
|
private CreateSearchInstanceCommand $createSearchInstanceCommand,
|
|
|
|
private BuilderCommand $builderCommand
|
|
|
|
) { }
|
|
|
|
|
|
|
|
public function getRoleById(int $id): ?Role
|
|
|
|
{
|
|
|
|
return Role::query()->where('id', $id)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoleByCode(string $code): ?Role
|
|
|
|
{
|
|
|
|
return Role::query()->where('code', $code)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoles(RoleBuilderDto $roleBuilderDto, array $with = []): Search
|
|
|
|
{
|
|
|
|
$query = $this->builderCommand->execute(
|
|
|
|
query: Role::query()->with($with),
|
|
|
|
roleBuilderDto: $roleBuilderDto
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->createSearchInstanceCommand->execute($query);
|
|
|
|
}
|
|
|
|
|
2023-08-01 22:09:06 +06:00
|
|
|
public function getRolesForSelect(array $withExcepts = []): array
|
|
|
|
{
|
|
|
|
return Role::query()
|
|
|
|
->when($withExcepts, function (Builder $query, array $withExcepts) {
|
|
|
|
$query->withTrashed()->whereNull('deleted_at')->orWhereIn('id', $withExcepts);
|
|
|
|
})
|
|
|
|
->pluck('name', 'id')
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2023-07-16 19:21:09 +06:00
|
|
|
public function isExistsCode(string $code, ?int $exceptId = null): bool
|
2023-07-02 16:17:18 +06:00
|
|
|
{
|
2023-07-16 19:21:09 +06:00
|
|
|
return Role::query()
|
|
|
|
->where('code', $code)
|
|
|
|
->when($exceptId, function (Builder $query, int $exceptId) {
|
|
|
|
$query->where('id', '!=', $exceptId);
|
|
|
|
})
|
|
|
|
->withTrashed()
|
|
|
|
->exists();
|
2023-07-02 16:17:18 +06:00
|
|
|
}
|
|
|
|
}
|