Пример #1
0
        public async Task <IActionResult> AddFolder([FromBody] AddFolderRequest request)
        {
            var userIdString = JWTUtility.GetUserId(User);

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

            var userId = int.Parse(userIdString);

            if (request.ParentFolder != null)
            {
                // Check if the parent folder does belong to the user
                if (!await _registerService.DoesFolderBelongToUser(
                        userId, folderId: request.ParentFolder.Value))
                {
                    return(BadRequest());
                }
            }

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

            // Check if the folder does not exist
            if (await _registerService.DoesFileOrFolderAlreadyExist(userId, name: request.Name, parentFolder: request.ParentFolder))
            {
                return(BadRequest(Texts.FILE_FOLDER_ALREADY_EXISTS));
            }

            var folderAdded = await _registerService.AddFolder(userId, parentFolder : request.ParentFolder, request);

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

            return(Ok(folderAdded));
        }