cannot('viewAny', Project::class)) { return $this->errFobidden(__('Access is denied')); } $projects = $this->projectRepository->getProjects( $projectBuilderDto, $querySettingsDto->getQueryWith() )->pagination( $querySettingsDto->getLimit(), $querySettingsDto->getPage() ); return $this->result([ 'projects' => $projects, 'serviceTranslationEnable' => config('translation_service.enable', false), ]); } public function show(int $id, User $user): ServiceResultError | ServiceResultArray { $project = $this->projectRepository->getProjectById($id); if (is_null($project)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('view', $project)) { return $this->errFobidden(__('Access is denied')); } return $this->result([ 'project' => $project, ]); } public function create(User $user): ServiceResultError | ServiceResultArray { if ($user->cannot('create', Project::class)) { return $this->errFobidden(__('Access is denied')); } $language = new ProjectLanguage(); $language->is_default = true; $language->title = $user->lang?->getTitle(); $language->code = $user->lang?->getLocale(); return $this->result([ 'project' => new Project(), 'languages' => collect([$language])->toArray(), ]); } public function edit(int $id, User $user): ServiceResultError | ServiceResultArray { $project = $this->projectRepository->getProjectById($id); if (is_null($project)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('view', $project)) { return $this->errFobidden(__('Access is denied')); } return $this->result([ 'project' => $project, 'languages' => $project->languages->toArray(), ]); } public function store(StoreUpdate $data, User $user): ServiceResultError | StoreUpdateResult { if ($user->cannot('create', Project::class)) { return $this->errFobidden(__('Access is denied')); } if ($this->projectRepository->isExistsCode($data->getCode())) { return $this->errValidate( __('validation.unique', ['attribute' => __('validation.attributes.code')]), ['code' => __('validation.unique', ['attribute' => __('validation.attributes.code')])] ); } $storages = $this->storageService->getStoragesAndValidate($data->getStorages(), Morph::Project); if (!$storages->isSuccess()) { return $storages; } try { $project = DB::transaction(function () use ($data, $user, $storages) { $dataProject = $this->getDataProject($data); $project = $this->projectCommandHandler->handleStore($dataProject); $this->createAdminRoleForProjectCommand->execute($project, $user); $this->languageModelSyncCommand->execute($project, $data->getLanguages()); $this->storageService->saveAndDelete($project, $storages); return $project; }); $this->clearCacheCommandHandler->all(); } catch (\Throwable $e) { report($e); return $this->errService(__('Server Error')); } return $this->resultStoreUpdateModel($project, __('The project was successfully created')); } public function update(int $id, StoreUpdate $data, User $user): ServiceResultError | StoreUpdateResult { $project = $this->projectRepository->getProjectById($id); if (is_null($project)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('update', $project)) { return $this->errFobidden(__('Access is denied')); } if ($this->projectRepository->isExistsCode($data->getCode(), $project->id)) { return $this->errValidate( __('validation.unique', ['attribute' => __('validation.attributes.code')]), ['code' => __('validation.unique', ['attribute' => __('validation.attributes.code')])] ); } $storages = $this->storageService->getStoragesAndValidate($data->getStorages(), Morph::Project, $project->id); if (!$storages->isSuccess()) { return $storages; } try { $project = DB::transaction(function () use ($data, $project, $storages) { $dataProject = $this->getDataProject($data); $project = $this->projectCommandHandler->handleUpdate($project, $dataProject); $this->languageModelSyncCommand->execute($project, $data->getLanguages()); $this->storageService->saveAndDelete($project, $storages); return $project; }); $this->clearCacheCommandHandler->all(); } catch (\Throwable $e) { report($e); return $this->errService(__('Server Error')); } return $this->resultStoreUpdateModel($project, __('The project was successfully updated')); } public function destroy(int $id, User $user): ServiceResultError|ServiceResultSuccess { $project = $this->projectRepository->getProjectById($id); if (is_null($project)) { return $this->errNotFound(__('Not Found')); } if ($user->cannot('delete', $project)) { return $this->errFobidden(__('Access is denied')); } try { DB::transaction(function () use ($project) { $this->projectCommandHandler->handleDestroy($project); }); $this->clearCacheCommandHandler->all(); } catch (\Throwable $e) { report($e); return $this->errService(__('Server Error')); } return $this->ok(__('The project has been deleted')); } private function getDataProject(StoreUpdate $data): array { return [ 'name' => $data->getName(), 'code' => $data->getCode(), 'http_host' => $data->getHttpHost(), 'is_public' => $data->isPublic(), ]; } }