my-projects-website/app/application/app/Repositories/DocumentationVersionRepository.php

52 lines
1.8 KiB
PHP

<?php declare(strict_types=1);
namespace App\Repositories;
use App\Contracts\Search;
use App\Models\DocumentationVersion;
use App\Services\DocumentationVersion\BuilderCommand;
use App\Services\Search\CreateSearchInstanceCommand;
use App\Dto\Builder\DocumentationVersion as DocumentationVersionBuilderDto;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
final readonly class DocumentationVersionRepository
{
public function __construct(
private CreateSearchInstanceCommand $createSearchInstanceCommand,
private BuilderCommand $builderCommand
) { }
public function getVersions(int $projectId, DocumentationVersionBuilderDto $documentationVersionBuilderDto, array $with = []): Search
{
$query = $this->builderCommand->execute(
query: DocumentationVersion::query()->where('project_id', $projectId)->with($with),
documentationVersionBuilderDto: $documentationVersionBuilderDto
);
return $this->createSearchInstanceCommand->execute($query);
}
public function getVersionById(int $id): ?DocumentationVersion
{
return DocumentationVersion::query()->where('id', $id)->first();
}
public function getVersionByCode(string $code): ?DocumentationVersion
{
return DocumentationVersion::query()->where('code', $code)->first();
}
public function isExistsSlug(int $projectId, string $slug, ?int $exceptId = null): bool
{
return DocumentationVersion::query()
->where('project_id', $projectId)
->where('slug', Str::lower($slug))
->when($exceptId, function (Builder $query, int $exceptId) {
$query->where('id', '!=', $exceptId);
})
->withTrashed()
->exists();
}
}