Exemplo n.º 1
0
        public async Task <IActionResult> ChangeRegisterName([FromBody] ChangeRegisterNameRequest request)
        {
            var userIdString = JWTUtility.GetUserId(User);

            if (userIdString == null)
            {
                return(BadRequest());
            }

            var userId = int.Parse(userIdString);

            // Check if the register does belong to the user.
            if (!await _registerService.DoesRegisterBelongToUser(userId, registerId: request.RegisterId))
            {
                return(BadRequest());
            }

            // Check if the new name is valid.
            if (!FileNameUtility.FileFolderNameIsValid(request.NewName))
            {
                return(BadRequest(Texts.INVALID_FILE_NAME));
            }

            // Check if the new name already exists in the parent folder.
            var parentFolderId = await _registerService.GetParentFolderForRegister(request.RegisterId);

            if (await _registerService.DoesFileOrFolderAlreadyExist(userId, name: request.NewName, parentFolder: parentFolderId))
            {
                return(BadRequest(Texts.FILE_FOLDER_ALREADY_EXISTS));
            }

            var registerChanged = await _registerService.ChangeRegisterName(userId, request);

            if (registerChanged == null)
            {
                return(StatusCode(statusCode: 500, value: Texts.ERROR_MODIFYING_REGISTER));
            }

            return(Ok(registerChanged));
        }
Exemplo n.º 2
0
        public async Task <Register> ChangeRegisterName(int userId, ChangeRegisterNameRequest request)
        {
            var registerToChange = await _context.Register.Where(
                register => register.Id == request.RegisterId && register.Author == userId
                ).FirstOrDefaultAsync();

            if (registerToChange == null)
            {
                return(null);
            }

            var newRelativePath = GenerateNewRelativePathForChangeName(registerToChange, request.NewName);

            if (registerToChange.IsFolder)
            {
                //Rename the folder with the service.
                var folderChanged = await _userFilesService.MoveOrRenameFolder(registerToChange.PathOrUrl, newRelativePath);

                if (!folderChanged)
                {
                    return(null);
                }

                // Change the name of the register
                registerToChange.Name = request.NewName;
                //Replace the relative path of the register and its subregisters (recursively)
                await ChangeRelativePathOfSubRegisters(
                    register : registerToChange,
                    oldRelativePath : registerToChange.PathOrUrl,
                    newRelativePath : newRelativePath
                    );
            }
            else
            {
                // Rename the file with the service.
                var fileChanged = await _userFilesService.MoveOrRenameFile(registerToChange.PathOrUrl, newRelativePath);

                if (!fileChanged)
                {
                    return(null);
                }

                // Change the name and the path of the register.
                registerToChange.Name      = request.NewName;
                registerToChange.PathOrUrl = registerToChange.PathOrUrl.Replace(
                    registerToChange.PathOrUrl, newRelativePath
                    );

                _context.Register.Update(registerToChange);
            }

            var entriesWritten = await _context.SaveChangesAsync();

            // To prevent a possible object cycle when serialize this object in the controller's response
            registerToChange.InverseParentFolderNavigation = null;

            if (entriesWritten > 0)
            {
                return(registerToChange);
            }

            return(null);
        }