From 2b65ac5771a257894d74d716dc7797ee2c3af19b 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 | 5 ++-- src/services/groupfoldersService.js | 1 + src/services/spaceService.js | 35 ++++++++++++++++++++++++++ 7 files changed, 66 insertions(+), 14 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 755947410..6ca4a58fb 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -90,8 +90,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 f92d2946c..32ed32c4a 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -41,6 +41,7 @@ use OCA\Workspace\Service\UserService; use OCA\Workspace\Service\Workspace\WorkspaceCheckService; use OCA\Workspace\Service\WorkspaceService; +use OCA\Workspace\Space\SpaceManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; @@ -64,6 +65,7 @@ public function __construct( private WorkspaceService $workspaceService, private UserGroup $userGroup, private WorkspaceManagerGroup $workspaceManagerGroup, + private SpaceManager $spaceManager, public $AppName ) { parent::__construct($AppName, $request); @@ -308,16 +310,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))); @@ -332,12 +330,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 d27b625e2..a1cd5a314 100644 --- a/lib/Helper/GroupfolderHelper.php +++ b/lib/Helper/GroupfolderHelper.php @@ -96,4 +96,12 @@ public function setFolderQuota(int $folderId, int $quota): void { throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the setFolderQuota 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 fda7d4473..d9e98890e 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -145,4 +145,15 @@ public function attachGroup(int $folderId, string $gid): void { private function deleteBlankSpaceName(string $spaceName): string { return trim($spaceName); } + + /** + * @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 3e2cc5072..bdf72d6ea 100644 --- a/src/SpaceDetails.vue +++ b/src/SpaceDetails.vue @@ -109,6 +109,7 @@ import SelectUsers from './SelectUsers.vue' import RemoveSpace from './RemoveSpace.vue' import UserTable from './UserTable.vue' import { destroy, rename, checkGroupfolderNameExist } from './services/groupfoldersService.js' +import { renameSpace } from './services/spaceService.js' import showNotificationError from './services/Notifications/NotificationError.js' export default { @@ -191,13 +192,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 8745be773..ca1ffa69b 100644 --- a/src/services/groupfoldersService.js +++ b/src/services/groupfoldersService.js @@ -291,6 +291,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 f7495137c..f53bd7761 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -125,3 +125,38 @@ export function addGroupToWorkspace(spaceId, gid) { throw new AddGroupToGroupfolderError('Error to add Space Manager group in the groupfolder') }) } + +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 +}