Пример #1
0
        public object MoveForm(MoveFormRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();
            var parentId = GuidHelper.GetGuid(request.NewParentId);

            // Catch all errors.
            try
            {

                // Declare list of anonymous type.
                var savedDescendants = new[]
                {
                    new
                    {
                        Id = string.Empty,
                        Path = new string[] { }
                    }
                }.Take(0).ToList();

                // Variables.
                var formId = GuidHelper.GetGuid(request.FormId);
                var form = Persistence.Retrieve(formId);
                var parentPath = Entities.Retrieve(parentId).Path;
                var oldFormPath = form.Path;
                var oldParentPath = oldFormPath.Take(oldFormPath.Length - 1).ToArray();
                var configs = ConFormPersistence.RetrieveChildren(formId);

                // Move form and configurations.
                var path = EntityHelper.GetClientPath(Entities.MoveEntity(form, parentPath));
                foreach (var config in configs)
                {
                    var descendantParentPath = config.Path.Take(config.Path.Length - 1);
                    var descendantPathEnd = descendantParentPath.Skip(oldParentPath.Length);
                    var newParentPath = parentPath.Concat(descendantPathEnd).ToArray();
                    var clientPath = EntityHelper.GetClientPath(
                        Entities.MoveEntity(config, newParentPath));
                    savedDescendants.Add(new
                    {
                        Id = GuidHelper.GetString(config.Id),
                        Path = clientPath
                    });
                }

                // Success.
                result = new
                {
                    Success = true,
                    Id = GuidHelper.GetString(formId),
                    Path = path,
                    Descendants = savedDescendants.ToArray()
                };

            }
            catch (Exception ex)
            {

                // Error.
                LogHelper.Error<FormsController>(MoveFormError, ex);
                result = new
                {
                    Success = false,
                    Reason = UnhandledError
                };

            }

            // Return result.
            return result;
        }
Пример #2
0
        public object MoveFolder(MoveFolderRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();
            var parentId = GuidHelper.GetGuid(request.NewParentId);

            // Catch all errors.
            try
            {

                // Declare list of anonymous type.
                var savedDescendants = new[]
                {
                    new
                    {
                        Id = string.Empty,
                        Path = new string[] { }
                    }
                }.Take(0).ToList();

                // Variables.
                var folderId = GuidHelper.GetGuid(request.FolderId);
                var parentPath = Entities.Retrieve(parentId).Path;

                // Get folder and descendants.
                var folder = Persistence.Retrieve(folderId);
                var descendants = Entities.RetrieveDescendants(folderId);

                // Check if destination folder is under current folder.
                var oldFolderPath = folder.Path;
                if (parentPath.Any(x => x == folderId))
                {
                    result = new
                    {
                        Success = false,
                        Reason = FolderUnderItself
                    };
                    return result;
                }

                // Move folder and descendants.
                var oldParentPath = oldFolderPath.Take(oldFolderPath.Length - 1).ToArray();
                var path = EntityHelper.GetClientPath(Entities.MoveEntity(folder, parentPath));
                foreach (var descendant in descendants)
                {
                    var descendantParentPath = descendant.Path.Take(descendant.Path.Length - 1);
                    var descendantPathEnd = descendantParentPath.Skip(oldParentPath.Length);
                    var newParentPath = parentPath.Concat(descendantPathEnd).ToArray();
                    var clientPath = EntityHelper.GetClientPath(
                        Entities.MoveEntity(descendant, newParentPath));
                    savedDescendants.Add(new
                    {
                        Id = GuidHelper.GetString(descendant.Id),
                        Path = clientPath
                    });
                }

                // Success.
                result = new
                {
                    Success = true,
                    Id = GuidHelper.GetString(folderId),
                    Path = path,
                    Descendants = savedDescendants.ToArray()
                };

            }
            catch (Exception ex)
            {

                // Error.
                LogHelper.Error<FoldersController>(MoveFolderError, ex);
                result = new
                {
                    Success = false,
                    Reason = UnhandledError
                };

            }

            // Return result.
            return result;
        }