A documentation section has been added to the site.
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Services\Admin\Project;
|
||||
use App\Dto\QuerySettingsDto;
|
||||
use App\Dto\Builder\DocumentationVersion as DocumentationVersionBuilderDto;
|
||||
use App\Dto\Service\Admin\Project\DocumentationVersion\StoreUpdate;
|
||||
use App\Enums\CacheTag;
|
||||
use App\Enums\DocumentationVersionStatus;
|
||||
use App\Models\DocumentationVersion;
|
||||
use App\Models\User;
|
||||
@@ -14,6 +15,7 @@ use App\ServiceResults\ServiceResultArray;
|
||||
use App\ServiceResults\ServiceResultError;
|
||||
use App\ServiceResults\ServiceResultSuccess;
|
||||
use App\ServiceResults\StoreUpdateResult;
|
||||
use App\Services\ClearCacheCommandHandler;
|
||||
use App\Services\DocumentationVersion\DocumentationVersionCommandHandler;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -24,6 +26,7 @@ final class DocumentationVersionService extends Service
|
||||
private readonly ProjectRepository $projectRepository,
|
||||
private readonly DocumentationVersionRepository $documentationVersionRepository,
|
||||
private readonly DocumentationVersionCommandHandler $documentationVersionCommandHandler,
|
||||
private readonly ClearCacheCommandHandler $clearCacheCommandHandler,
|
||||
) { }
|
||||
|
||||
public function index(int $projectId, DocumentationVersionBuilderDto $documentationVersionBuilderDto, QuerySettingsDto $querySettingsDto, User $user): ServiceResultError | ServiceResultArray
|
||||
@@ -138,6 +141,7 @@ final class DocumentationVersionService extends Service
|
||||
$dataVersion = $this->getDataVersion($data);
|
||||
return $this->documentationVersionCommandHandler->handleStore($project, $dataVersion);
|
||||
});
|
||||
$this->clearCacheCommandHandler->byTag(CacheTag::DocumantationVersion);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
return $this->errService(__('Server Error'));
|
||||
@@ -174,6 +178,7 @@ final class DocumentationVersionService extends Service
|
||||
$dataVersion = $this->getDataVersion($data);
|
||||
return $this->documentationVersionCommandHandler->handleUpdate($version, $dataVersion);
|
||||
});
|
||||
$this->clearCacheCommandHandler->byTag(CacheTag::DocumantationVersion);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
return $this->errService(__('Server Error'));
|
||||
@@ -202,6 +207,7 @@ final class DocumentationVersionService extends Service
|
||||
DB::transaction(function () use ($version) {
|
||||
$this->documentationVersionCommandHandler->handleDestroy($version);
|
||||
});
|
||||
$this->clearCacheCommandHandler->byTag(CacheTag::DocumantationVersion);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
return $this->errService(__('Server Error'));
|
||||
|
@@ -10,10 +10,12 @@ final readonly class BuilderCommand
|
||||
{
|
||||
public function execute(Relation | Builder $query, DocumentationBuilderDto $documentationBuilderDto): Relation | Builder
|
||||
{
|
||||
if ($documentationBuilderDto->isPublic() !== null) {
|
||||
$query->where('is_public', $documentationBuilderDto->isPublic());
|
||||
}
|
||||
|
||||
return $query;
|
||||
return $query
|
||||
->when($documentationBuilderDto->isPublic(), function (Builder $query) use ($documentationBuilderDto) {
|
||||
$query->where('is_public', $documentationBuilderDto->isPublic());
|
||||
})
|
||||
->when($documentationBuilderDto->getCategoryId(), function (Builder $query) use ($documentationBuilderDto) {
|
||||
$query->where('category_id', $documentationBuilderDto->getCategoryId()->getCategoryId());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -10,10 +10,12 @@ final readonly class BuilderCommand
|
||||
{
|
||||
public function execute(Relation | Builder $query, DocumentationCategoryBuilderDto $documentationCategoryBuilderDto): Relation | Builder
|
||||
{
|
||||
if ($documentationCategoryBuilderDto->isPublic() !== null) {
|
||||
$query->where('is_public', $documentationCategoryBuilderDto->isPublic());
|
||||
}
|
||||
|
||||
return $query;
|
||||
return $query
|
||||
->when($documentationCategoryBuilderDto->isPublic(), function (Builder $query) use ($documentationCategoryBuilderDto) {
|
||||
$query->where('is_public', $documentationCategoryBuilderDto->isPublic());
|
||||
})
|
||||
->when($documentationCategoryBuilderDto->getParentId(), function (Builder $query) use ($documentationCategoryBuilderDto) {
|
||||
$query->where('parent_id', $documentationCategoryBuilderDto->getParentId()->getCategoryId());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
151
app/application/app/Services/Site/DocumentationService.php
Normal file
151
app/application/app/Services/Site/DocumentationService.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Site;
|
||||
|
||||
use App\Dto\Service\Site\Documentation;
|
||||
use App\Enums\CacheTag;
|
||||
use App\Enums\DocumentationVersionStatus;
|
||||
use App\Models\DocumentationCategory;
|
||||
use App\Models\Documentation as ModelDocumentation;
|
||||
use App\Models\DocumentationVersion;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use App\Repositories\DocumentationCategoryRepository;
|
||||
use App\Repositories\DocumentationRepository;
|
||||
use App\ServiceResults\ServiceResultArray;
|
||||
use App\ServiceResults\ServiceResultError;
|
||||
use App\ServiceResults\Site\DocumentationService\DefaultVersion;
|
||||
use app\ServiceResults\Site\PagePossibleWithoutTranslation;
|
||||
use App\Services\Service;
|
||||
use App\Dto\Builder\DocumentationCategory as DocumentationCategoryBuilderDto;
|
||||
use App\Dto\Builder\Documentation as DocumentationBuilderDto;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
final class DocumentationService extends Service
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DocumentationCategoryRepository $documentationCategoryRepository,
|
||||
private readonly DocumentationRepository $documentationRepository
|
||||
) { }
|
||||
|
||||
public function defaultVersion(Project $project, ?User $user): ServiceResultError | DefaultVersion
|
||||
{
|
||||
$seconds = 3600;
|
||||
$isPublic = null;
|
||||
if ($user?->cannot('viewAny', DocumentationVersion::class)) {
|
||||
$isPublic = 1;
|
||||
}
|
||||
$version = CacheTag::DocumantationVersion->getCache()
|
||||
->remember(self::class . $project->id . '_' . $isPublic ?? 0, $seconds, function () use ($project, $isPublic) {
|
||||
$versions = $project->documentationVersions()
|
||||
->when($isPublic, function (Builder $query) {
|
||||
$query->where('is_public', 1);
|
||||
})
|
||||
->limit(10)
|
||||
->get();
|
||||
return $versions->firstWhere('status', DocumentationVersionStatus::CurrentVersion)
|
||||
??
|
||||
$versions->first() ?? false;
|
||||
});
|
||||
|
||||
if ($version === false) {
|
||||
return $this->errNotFound(__('Not Found'));
|
||||
}
|
||||
|
||||
return new DefaultVersion($version);
|
||||
}
|
||||
|
||||
public function index(Documentation $documentation): ServiceResultError | ServiceResultArray
|
||||
{
|
||||
return $this->result(array_merge($documentation->toArray(), [
|
||||
'categories' => $this->getCategories($documentation, null),
|
||||
'documentations' => $this->getDocumentations($documentation, null),
|
||||
]));
|
||||
}
|
||||
|
||||
public function category(string $slug, Documentation $documentation): ServiceResultError | PagePossibleWithoutTranslation
|
||||
{
|
||||
$category = $this->documentationCategoryRepository->getCategoryBySlugWithContent($slug, $documentation->getVersion()->id, $documentation->getWebsiteTranslations()->getLanguage());
|
||||
if (!$category) {
|
||||
return $this->errNotFound(__('Not Found'));
|
||||
}
|
||||
if (
|
||||
$category->is_public === false &&
|
||||
($documentation->getUser() === null || $documentation->getUser()->cannot('view', $category))
|
||||
) {
|
||||
return $this->errFobidden(__('Access is denied'));
|
||||
}
|
||||
|
||||
$data = array_merge($documentation->toArray(), [
|
||||
'category' => $category,
|
||||
'categories' => $this->getCategories($documentation, $category->id),
|
||||
'documentations' => $this->getDocumentations($documentation, $category->id),
|
||||
]);
|
||||
return $this->resultSitePage($documentation->getProject(), $documentation->getWebsiteTranslations(), $data, \is_null($category->content?->title));
|
||||
}
|
||||
|
||||
public function view(string $slug, Documentation $documentation): ServiceResultError | PagePossibleWithoutTranslation
|
||||
{
|
||||
$document = $this->documentationRepository->getDocumentationBySlugWithContent($slug, $documentation->getVersion()->id, $documentation->getWebsiteTranslations()->getLanguage());
|
||||
if (!$document) {
|
||||
return $this->errNotFound(__('Not Found'));
|
||||
}
|
||||
if (
|
||||
$document->is_public === false &&
|
||||
($documentation->getUser() === null || $documentation->getUser()->cannot('view', $document))
|
||||
) {
|
||||
return $this->errFobidden(__('Access is denied'));
|
||||
}
|
||||
|
||||
$data = array_merge($documentation->toArray(), [
|
||||
'documentation' => $document,
|
||||
]);
|
||||
return $this->resultSitePage($documentation->getProject(), $documentation->getWebsiteTranslations(), $data, \is_null($document->content?->title));
|
||||
}
|
||||
|
||||
private function getCategories(Documentation $documentation, ?int $parentId): Collection
|
||||
{
|
||||
$isPublic = null;
|
||||
if ($documentation->getUser() === null || $documentation->getUser()->cannot('viewAny', DocumentationCategory::class)) {
|
||||
$isPublic = true;
|
||||
}
|
||||
$builderDto = new DocumentationCategoryBuilderDto(
|
||||
isPublic: $isPublic,
|
||||
parentId: new DocumentationCategoryBuilderDto\Category($parentId),
|
||||
);
|
||||
$with = [
|
||||
'content' => function (HasOne $hasOne) use ($documentation) {
|
||||
$hasOne->where('language_id', $documentation->getWebsiteTranslations()->getLanguage()->id);
|
||||
}
|
||||
];
|
||||
return $this->documentationCategoryRepository->getCategories(
|
||||
$documentation->getVersion()->id,
|
||||
$builderDto,
|
||||
$with
|
||||
)->all();
|
||||
}
|
||||
|
||||
private function getDocumentations(Documentation $documentation, ?int $categoryId): Collection
|
||||
{
|
||||
$isPublic = null;
|
||||
if ($documentation->getUser() === null || $documentation->getUser()->cannot('viewAny', ModelDocumentation::class)) {
|
||||
$isPublic = true;
|
||||
}
|
||||
$builderDto = new DocumentationBuilderDto(
|
||||
isPublic: $isPublic,
|
||||
categoryId: new DocumentationCategoryBuilderDto\Category($categoryId),
|
||||
);
|
||||
$with = [
|
||||
'content' => function (HasOne $hasOne) use ($documentation) {
|
||||
$hasOne->where('language_id', $documentation->getWebsiteTranslations()->getLanguage()->id);
|
||||
}
|
||||
];
|
||||
return $this->documentationRepository->getDocumentations(
|
||||
$documentation->getVersion()->id,
|
||||
$builderDto,
|
||||
$with
|
||||
)->all();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user