Skip to content

Commit

Permalink
Merge pull request #1082 from arawa/refactor/rename-group-folders-bac…
Browse files Browse the repository at this point in the history
…kend-side

Move groupfolder renaming to back-end
  • Loading branch information
zak39 authored Nov 7, 2024
2 parents bbb0fb0 + a09b101 commit 822ff4e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 19 deletions.
3 changes: 1 addition & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@
],
[
'name' => 'workspace#renameSpace',
// TODO move this route to /api/spaces
'url' => '/api/space/rename',
'url' => '/spaces/{spaceId}',
'verb' => 'PATCH'
],
[
Expand Down
17 changes: 6 additions & 11 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct(
private UserGroup $userGroup,
private WorkspaceManagerGroup $workspaceManagerGroup,
private SpaceManager $spaceManager,
public $AppName,
public $AppName
) {
parent::__construct($AppName, $request);
}
Expand Down Expand Up @@ -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)));
Expand All @@ -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,
]);
}
}
8 changes: 8 additions & 0 deletions lib/Helper/GroupfolderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,12 @@ public function removeFolder(int $folderId): void {
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.');
}
}
}
18 changes: 18 additions & 0 deletions lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,22 @@ public function remove(string $spaceId): void {
$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);

if ($this->workspaceCheck->isExist($newSpaceName)) {
throw new WorkspaceNameExistException(
title: 'Error - Duplicate space name',
message: "This space or groupfolder already exist. Please, input another space.\nIf \"toto\" space exist, you cannot create the \"tOTo\" space.\nMake sure you the groupfolder doesn't exist."
);
}

$this->folderHelper->renameFolder($space['groupfolder_id'], $newSpaceName);
$this->spaceMapper->updateSpaceName($newSpaceName, $spaceId);
}
}
9 changes: 3 additions & 6 deletions src/SpaceDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -187,18 +186,16 @@ export default {

const newSpaceName = e.target[0].value

await checkGroupfolderNameExist(newSpaceName)

// 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,
})
Expand Down
1 change: 1 addition & 0 deletions src/services/groupfoldersService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/services/spaceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,45 @@ 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 => {
if ('response' in error && 'data' in error.response) {
showNotificationError(error.response.data.title, error.response.data.message, 5000)
throw new Error(error.response.data.message)
} else {
showNotificationError('Error to rename a workspace', error.message, 5000)
console.error('Problem to rename the space', error)
throw new Error(error.message)
}
})

return respFormatFinal
}

0 comments on commit 822ff4e

Please sign in to comment.