Added the ability to upload pictures in the editor.

This commit is contained in:
2024-07-26 22:04:39 +05:00
parent 4d36821ecc
commit b33362a235
34 changed files with 2007 additions and 40 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Services\Admin\Project;
use App\Contracts\ServiceResultError;
use App\Dto\Service\Admin\Project\About\StoreUpdate;
use App\Enums\Morph;
use App\Models\ProjectContent;
use App\Models\User;
use App\Repositories\ProjectContentRepository;
@@ -13,6 +14,7 @@ use App\ServiceResults\ServiceResultArray;
use App\ServiceResults\StoreUpdateResult;
use App\Services\ProjectContent\ProjectContentCommandHandler;
use App\Services\Service;
use App\Services\Storage\StorageService;
use Illuminate\Support\Facades\DB;
final class AboutService extends Service
@@ -22,6 +24,7 @@ final class AboutService extends Service
private readonly ProjectLanguageRepository $projectLanguageRepository,
private readonly ProjectContentRepository $projectContentRepository,
private readonly ProjectContentCommandHandler $projectContentCommandHandler,
private readonly StorageService $storageService,
) { }
public function languages(int $projectId, User $user): ServiceResultError | ServiceResultArray
@@ -88,11 +91,19 @@ final class AboutService extends Service
if ($user->cannot('create', ProjectContent::class)) {
return $this->errFobidden(__('Access is denied'));
}
$storages = $this->storageService->getStoragesAndValidate($data->getStorages(), Morph::ProjectContent);
if (!$storages->isSuccess()) {
return $storages;
}
try {
$aboutProject = DB::transaction(function () use ($data, $projectId, $languageId) {
$aboutProject = DB::transaction(function () use ($data, $projectId, $languageId, $storages) {
$dataAboutProject = $this->getDataAboutProject($data);
return $this->projectContentCommandHandler->handleStore($projectId, $languageId, $dataAboutProject);
$aboutProject = $this->projectContentCommandHandler->handleStore($projectId, $languageId, $dataAboutProject);
$this->storageService->saveAndDelete($aboutProject, $storages);
return $aboutProject;
});
} catch (\Throwable $e) {
report($e);
@@ -108,11 +119,19 @@ final class AboutService extends Service
return $this->errFobidden(__('Access is denied'));
}
$storages = $this->storageService->getStoragesAndValidate($data->getStorages(), Morph::ProjectContent);
if (!$storages->isSuccess()) {
return $storages;
}
try {
$aboutProject = DB::transaction(function () use ($data, $content) {
$aboutProject = DB::transaction(function () use ($data, $content, $storages) {
$dataAboutProject = $this->getDataAboutProject($data);
return $this->projectContentCommandHandler->handleUpdate($content, $dataAboutProject);
$aboutProject = $this->projectContentCommandHandler->handleUpdate($content, $dataAboutProject);
$this->storageService->saveAndDelete($aboutProject, $storages);
return $aboutProject;
});
} catch (\Throwable $e) {
report($e);

View File

@@ -4,18 +4,33 @@ namespace App\Services\DocumentationContent;
use App\Dto\Service\Admin\Project\DocumentationContent\Content;
use App\Dto\Service\Admin\Project\DocumentationContent\Contents;
use App\Dto\Service\DocumentationContent\StorageDto;
use App\Exceptions\Services\DocumentationContent\ContentSaveException;
use App\Exceptions\Services\DocumentationContent\StorageCommandException;
use App\Models\Documentation;
use App\Models\DocumentationContent;
use App\Models\Project;
final readonly class ModelSyncCommand
{
public function __construct(
private StorageCommand $storageCommand,
) { }
/**
* @throws StorageCommandException
* @throws ContentSaveException
*/
public function execute(Project $project, Documentation $documentation, Contents $contents): void
{
$storageDto = new StorageDto();
$languages = $project->languages;
$documentationContents = $documentation->contents;
$newContents = [];
$contentsStorageCreated = [];
$contentLanguages = [];
foreach ($contents->getContents() as $content) {
/** @var Content $content */
$language = $languages->firstWhere('id', $content->getLanguageId());
@@ -26,15 +41,29 @@ final readonly class ModelSyncCommand
$model = $documentationContents->firstWhere('language_id', $language->id);
$data = $this->getData($content);
if (\is_null($model)) {
$contentsStorageCreated[$content->getLanguageId()] = $content->getStorages();
$newContents[] = array_merge(['language_id' => $content->getLanguageId()], $data);
$contentLanguages[] = $content->getLanguageId();
continue;
}
$storageDto->add($model, $content->getStorages());
$model->update($data);
}
if (!empty($newContents)) {
$documentation->contents()->createMany($newContents);
$contents = $documentation->contents()->whereIn('language_id', $contentLanguages)->get();
foreach ($contents as $content) {
/** @var DocumentationContent $content */
if (!isset($contentsStorageCreated[$content->language_id])) {
continue;
}
$storageDto->add($content, $contentsStorageCreated[$content->language_id]);
}
}
$this->storageCommand->execute($storageDto);
}
private function getData(Content $content): array

View File

@@ -0,0 +1,31 @@
<?php declare(strict_types=1);
namespace App\Services\DocumentationContent;
use App\Dto\Service\DocumentationContent\StorageDto;
use App\Enums\Morph;
use App\Exceptions\Services\DocumentationContent\StorageCommandException;
use App\Services\Storage\StorageService;
final readonly class StorageCommand
{
public function __construct(
private StorageService $storageService,
) { }
/**
* @throws StorageCommandException
*/
public function execute(StorageDto $storageDto): void
{
foreach ($storageDto->getStorages() as $storage) {
$storages = $this->storageService->getStoragesAndValidate($storage['storages'], Morph::DocumentationContent, $storage['documentationContent']->id);
if (!$storages->isSuccess()) {
throw new StorageCommandException($storages, 'Error when adding a file to storage: ' . $storages->getMessage());
}
$this->storageService->saveAndDelete($storage['documentationContent'], $storages);
}
}
}