From e314450b6f7359d707735578e5a0b109f279923e Mon Sep 17 00:00:00 2001 From: zak39 Date: Thu, 31 Oct 2024 11:55:43 +0100 Subject: [PATCH] refactor: Move groupfolder renaming to back-end --- appinfo/routes.php | 3 +-- lib/Controller/WorkspaceController.php | 17 +++++-------- lib/Helper/GroupfolderHelper.php | 8 ++++++ lib/Space/SpaceManager.php | 11 ++++++++ src/SpaceDetails.vue | 7 +++--- src/services/groupfoldersService.js | 1 + src/services/spaceService.js | 35 ++++++++++++++++++++++++++ 7 files changed, 65 insertions(+), 17 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index c2f6b5ea..14faae3e 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -89,8 +89,7 @@ ], [ 'name' => 'workspace#renameSpace', - // TODO move this route to /api/spaces - 'url' => '/api/space/rename', + 'url' => '/spaces/{spaceId}', 'verb' => 'PATCH' ], [ diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index dc49da9c..c0fb0b96 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -67,7 +67,7 @@ public function __construct( private UserGroup $userGroup, private WorkspaceManagerGroup $workspaceManagerGroup, private SpaceManager $spaceManager, - public $AppName, + public $AppName ) { parent::__construct($AppName, $request); } @@ -275,16 +275,12 @@ public function changeUserRole(array|string $space, * * @NoAdminRequired * @SpaceAdminRequired - * @param array|string $workspace + * @param int $spaceId * @param string $newSpaceName * - * @todo Manage errors */ - public function renameSpace(array|string $workspace, + public function renameSpace(int $spaceId, string $newSpaceName): JSONResponse { - if (gettype($workspace) === 'string') { - $workspace = json_decode($workspace, true); - } if ($this->workspaceCheck->containSpecialChar($newSpaceName)) { throw new BadRequestException('Your Workspace name must not contain the following characters: ' . implode(' ', str_split(WorkspaceCheckService::CHARACTERS_SPECIAL))); @@ -299,12 +295,11 @@ public function renameSpace(array|string $workspace, $spaceName = $this->deleteBlankSpaceName($newSpaceName); - $spaceRenamed = $this->spaceService->updateSpaceName($newSpaceName, (int)$workspace['id']); - - // TODO Handle API call failure (revert space rename and inform user) + $this->spaceManager->rename($spaceId, $spaceName); + return new JSONResponse([ 'statuscode' => Http::STATUS_NO_CONTENT, - 'space' => $spaceRenamed, + 'space' => $spaceName, ]); } } diff --git a/lib/Helper/GroupfolderHelper.php b/lib/Helper/GroupfolderHelper.php index d71af1f1..6056cd59 100644 --- a/lib/Helper/GroupfolderHelper.php +++ b/lib/Helper/GroupfolderHelper.php @@ -102,6 +102,14 @@ public function removeFolder(int $folderId): void { $this->folderManager->removeFolder($folderId); } catch (\Exception $e) { throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the removeFolder from FolderManager.'); + } + } + + public function renameFolder(int $folderId, string $newMountPoint):void { + try { + $this->folderManager->renameFolder($folderId, $newMountPoint); + } catch (\Exception $e) { + throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the renameFolder from FolderManager.'); } } } diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index 37a54c20..94cf822c 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -155,5 +155,16 @@ public function remove(string $spaceId): void { $space = $this->get($spaceId); $folderId = $space['groupfolder_id']; $this->folderHelper->removeFolder($folderId); + } + + /** + * @param int $spaceId related to the id of a space. + * @param string $newSpaceName related to the new space name. + */ + public function rename(int $spaceId, string $newSpaceName): void { + $space = $this->get($spaceId); + + $this->folderHelper->renameFolder($space['groupfolder_id'], $newSpaceName); + $this->spaceMapper->updateSpaceName($newSpaceName, $spaceId); } } diff --git a/src/SpaceDetails.vue b/src/SpaceDetails.vue index 1ae5b947..0459041d 100644 --- a/src/SpaceDetails.vue +++ b/src/SpaceDetails.vue @@ -108,8 +108,7 @@ import NcModal from '@nextcloud/vue/dist/Components/NcModal.js' import SelectUsers from './SelectUsers.vue' import RemoveSpace from './RemoveSpace.vue' import UserTable from './UserTable.vue' -import { rename, checkGroupfolderNameExist } from './services/groupfoldersService.js' -import { removeWorkspace } from './services/spaceService.js' +import { renameSpace, removeWorkspace } from './services/spaceService.js' import showNotificationError from './services/Notifications/NotificationError.js' export default { @@ -192,13 +191,13 @@ export default { // TODO: Change : the key from $root.spaces, groupnames, change the route into new spacename because // the path is `https://instance-nc/apps/workspace/workspace/Aang` const oldSpaceName = this.$route.params.space - let responseRename = await rename(this.$store.state.spaces[oldSpaceName], newSpaceName) + let responseRename = await renameSpace(this.$store.state.spaces[oldSpaceName].id, newSpaceName) responseRename = responseRename.data if (responseRename.statuscode === 204) { const space = { ...this.$store.state.spaces[oldSpaceName] } space.name = responseRename.space - space.groups = responseRename.groups + this.$store.dispatch('updateSpace', { space, }) diff --git a/src/services/groupfoldersService.js b/src/services/groupfoldersService.js index ae093b5d..a9813b0d 100644 --- a/src/services/groupfoldersService.js +++ b/src/services/groupfoldersService.js @@ -279,6 +279,7 @@ export function destroy(workspace) { * @param {object} workspace it's the object relative to workspace * @param {string} newSpaceName it's the new name for the workspace * @return {Promise} + * @deprecated */ export function rename(workspace, newSpaceName) { // Response format to return diff --git a/src/services/spaceService.js b/src/services/spaceService.js index 61dd2402..4e9092a9 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -143,3 +143,38 @@ export function removeWorkspace(spaceId) { }) return result } + +export function renameSpace(spaceId, newSpaceName) { + const respFormat = { + data: {}, + } + respFormat.data.statuscode = 500 + respFormat.data.message = 'Rename the space is impossible.' + + newSpaceName = deleteBlankSpacename(newSpaceName) + + const respFormatFinal = axios.patch(generateUrl(`/apps/workspace/spaces/${spaceId}`), + { + newSpaceName, + }) + .then(resp => { + if (resp.data.statuscode === 400) { + respFormat.data.statuscode = 400 + respFormat.data.space = null + return respFormat + } + + if (resp.data.statuscode === 204) { + respFormat.data.statuscode = 204 + respFormat.data.space = newSpaceName + return respFormat + } + + return respFormat + }) + .catch(error => { + console.error('Problem to rename the space', error) + }) + + return respFormatFinal +}