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

64 lines
2.1 KiB
PHP

<?php declare(strict_types=1);
namespace App\Repositories;
use App\Dto\Builder\Documentation as DocumentationBuilderDto;
use App\Contracts\Search;
use App\Models\Documentation;
use App\Models\ProjectLanguage;
use App\Services\Documentation\BuilderCommand;
use App\Services\Search\CreateSearchInstanceCommand;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;
final readonly class DocumentationRepository
{
public function __construct(
private CreateSearchInstanceCommand $createSearchInstanceCommand,
private BuilderCommand $builderCommand
) { }
public function getDocumentations(int $versionId, DocumentationBuilderDto $documentationBuilderDto, array $with = []): Search
{
$query = $this->builderCommand->execute(
query: Documentation::query()->where('version_id', $versionId)->with($with),
documentationBuilderDto: $documentationBuilderDto
);
return $this->createSearchInstanceCommand->execute($query);
}
public function getDocumentationById(int $id): ?Documentation
{
return Documentation::query()->where('id', $id)->first();
}
public function getDocumentationBySlugWithContent(string $slug, int $versionId, ProjectLanguage $language): ?Documentation
{
$with = [
'content' => function (HasOne $hasOne) use ($language) {
$hasOne->where('language_id', $language->id);
}
];
return Documentation::query()
->where('version_id', $versionId)
->where('slug', $slug)
->with($with)
->first();
}
public function isExistsSlug(int $versionId, string $slug, ?int $exceptId = null): bool
{
return Documentation::query()
->where('version_id', $versionId)
->where('slug', Str::lower($slug))
->when($exceptId, function (Builder $query, int $exceptId) {
$query->where('id', '!=', $exceptId);
})
->withTrashed()
->exists();
}
}