public async Task <IEnumerable <object> > Handle(CreateEntitiesCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                var entityType = _commandsHelper.GetEntityType(request.Entities);

                _transactionService.StartTransaction();

                var entityCollection = _transactionService.GetCollection(entityType);
                var edgesCollection  = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                var parent = await GetParentNode(edgesCollection, request.ParentId);

                var treeEdges = request.Entities
                                .Select(x => CreateTreeEdge(x, parent, entityCollection))
                                .ToList();

                ValidateTreeEdges(edgesCollection, treeEdges);

                await edgesCollection.Add(treeEdges);

                await entityCollection.Add(request.Entities);

                _transactionService.CommitTransaction();

                await NotifyEntitiesCreation(request.Entities, parent);

                return(request.Entities);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntitiesCreationException("Entities creation failed", e);
            }
        }
示例#2
0
        public async Task <Unit> Handle(RenameEntityCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                _transactionService.StartTransaction();

                var edgesCollection = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                var renameRootNode = await _commandsHelper.VerifyEntity(request.EntityId, edgesCollection, SecurityRight.Rename);

                var renameDescendants = edgesCollection.GetDescendants(renameRootNode)
                                        .ToList();

                var oldRootPath = renameRootNode.Path;
                var newRootPath = _pathBuilder.RenameLeaf(oldRootPath, request.NewName);

                // renameRootNode is set with the instance taken from renameDescendants
                renameRootNode          = renameDescendants.First(x => x.Id == renameRootNode.Id);
                renameRootNode.NodeName = request.NewName;

                renameDescendants
                .ForEach(x => UpdateNodePath(x, oldRootPath, newRootPath));

                await edgesCollection.Update(renameDescendants);

                _transactionService.CommitTransaction();
                return(Unit.Value);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntitiesRenameException("Entity rename failed", e);
            }
        }
示例#3
0
        public async Task <IEnumerable <TEntity> > Handle(PartialUpdateEntitiesCommand <TContext, TEntity, TField> request, CancellationToken cancellationToken)
        {
            try
            {
                _transactionService.StartTransaction();

                var entityCollection = _transactionService.GetCollection <TEntity>();
                var edgesCollection  = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                await _commandsHelper.VerifyEntities(request.Entities, edgesCollection, SecurityRight.Update);

                await entityCollection.Update <TField>(request.Entities, request.Field, request.Value);

                _transactionService.CommitTransaction();

                var entities = await entityCollection.GetItems(request.Entities);

                await NotifyEntitiesUpdate(entities);

                return(entities);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntitiesUpdateException("Entities update failed", e);
            }
        }
        public async Task <IEnumerable <object> > Handle(UpdateEntitiesCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                var entityType = _commandsHelper.GetEntityType(request.Entities);

                _transactionService.StartTransaction();

                var entityCollection = _transactionService.GetCollection(entityType);
                var edgesCollection  = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                await _commandsHelper.VerifyEntities(request.Entities, edgesCollection, SecurityRight.Update);

                await entityCollection.Update(request.Entities);

                _transactionService.CommitTransaction();

                await NotifyEntitiesUpdate(request.Entities);

                return(request.Entities);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntitiesUpdateException("Entities update failed", e);
            }
        }
        public async Task <Unit> Handle(SetPermissionsCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                _transactionService.StartTransaction();

                var edgesCollection = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                await _commandsHelper.VerifyEntity(request.EntityId, edgesCollection, SecurityRight.Administer);

                var securityRulesCollection = _transactionService.GetCollection <EntitySecurityRule, SecurityRulesCollection>();

                if (request.Rules != null)
                {
                    await securityRulesCollection.SetRules(request.EntityId, request.Subject, request.Rules);
                }
                else
                {
                    await securityRulesCollection.ClearRules(request.EntityId, request.Subject);
                }


                _transactionService.CommitTransaction();
                return(Unit.Value);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new SetPermissionsException("Error setting permissions", e);
            }
        }
示例#6
0
        public async Task <Unit> Handle(DeleteEntitiesCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                var entityType = _commandsHelper.GetEntityType(request.Entities);
                var nodesId    = _commandsHelper.GetEntitiesId(request.Entities);

                _transactionService.StartTransaction();

                var edgesCollection = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                var treeNodes = await _commandsHelper.VerifyEntities(nodesId, edgesCollection, SecurityRight.Delete);

                var deletedNodes = new List <EntityNode>();

                foreach (var treeNode in treeNodes)
                {
                    deletedNodes.AddRange(await DeleteEntityAndDescendants(treeNode, edgesCollection));
                }

                _transactionService.CommitTransaction();

                await NotifyEntitiesDeletion(deletedNodes);

                return(Unit.Value);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntitiesDeleteException("Entities delete failed", e);
            }
        }
        public async Task <Unit> Handle(MoveEntityCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                _transactionService.StartTransaction();

                var edgesCollection = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                var moveTargetNode = await _commandsHelper.VerifyEntity(request.NewParentId, edgesCollection, SecurityRight.Create);

                if (moveTargetNode == null)
                {
                    throw new NodeNotFoundException($"Target node {request.NewParentId} not found");
                }

                var moveSourceNode = await _commandsHelper.VerifyEntity(request.EntityId, edgesCollection, SecurityRight.Move);

                if (moveSourceNode == null)
                {
                    throw new NodeNotFoundException($"Source node {request.EntityId} not found");
                }

                var nodesToMove = edgesCollection.GetDescendants(moveSourceNode)
                                  .ToList();

                MoveNodes(nodesToMove, moveSourceNode, moveTargetNode);

                await edgesCollection.Update(nodesToMove);

                _transactionService.CommitTransaction();
                return(Unit.Value);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntityMoveException("Entity move failed", e);
            }
        }