62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Contracts\Search;
|
|
use App\Models\Repository;
|
|
use App\Models\User;
|
|
use App\Dto\Builder\Repository as RepositoryBuilderDto;
|
|
use App\Services\Repository\BuilderCommand;
|
|
use App\Services\Search\CreateSearchInstanceCommand;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Str;
|
|
|
|
final readonly class RepositoryRepository
|
|
{
|
|
public function __construct(
|
|
private CreateSearchInstanceCommand $createSearchInstanceCommand,
|
|
private BuilderCommand $builderCommand,
|
|
) { }
|
|
|
|
public function getRepositoryByName(User $user, string $name, bool $withTrashed = false): ?Repository
|
|
{
|
|
$query = $user->repositories();
|
|
if ($withTrashed) {
|
|
$query->withTrashed();
|
|
}
|
|
|
|
return $query->where('name', $name)->first();
|
|
}
|
|
|
|
public function getRepositories(RepositoryBuilderDto $builderDto, array $with = []): Search
|
|
{
|
|
$query = $this->builderCommand->execute(
|
|
query: Repository::query()->with($with)->orderByDesc('updated_at'),
|
|
builderDto: $builderDto
|
|
);
|
|
|
|
return $this->createSearchInstanceCommand->execute($query);
|
|
}
|
|
|
|
public function getUserRepositories(User $user, RepositoryBuilderDto $builderDto, array $with = []): Search
|
|
{
|
|
$query = $this->builderCommand->execute(
|
|
query: $user->repositories()->with($with)->orderByDesc('updated_at'),
|
|
builderDto: $builderDto
|
|
);
|
|
|
|
return $this->createSearchInstanceCommand->execute($query);
|
|
}
|
|
|
|
public function isExistsName(User $user, string $name, ?int $exceptId = null): bool
|
|
{
|
|
return $user->repositories()
|
|
->where('name', Str::lower($name))
|
|
->when($exceptId, function (Builder $query, int $exceptId) {
|
|
$query->where('id', '!=', $exceptId);
|
|
})
|
|
->withTrashed()
|
|
->exists();
|
|
}
|
|
}
|