Ejemplo n.º 1
0
        public Attempt<OperationResult<OperationResultType, EntityContainer>?> RenameContainer(int id, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
        {
            var evtMsgs = EventMessagesFactory.Get();
            using (var scope = ScopeProvider.CreateCoreScope())
            {
                try
                {
                    var container = _dataTypeContainerRepository.Get(id);

                    //throw if null, this will be caught by the catch and a failed returned
                    if (container == null)
                        throw new InvalidOperationException("No container found with id " + id);

                    container.Name = name;

                    var renamingEntityContainerNotification = new EntityContainerRenamingNotification(container, evtMsgs);
                    if (scope.Notifications.PublishCancelable(renamingEntityContainerNotification))
                    {
                        scope.Complete();
                        return OperationResult.Attempt.Cancel(evtMsgs, container);
                    }

                    _dataTypeContainerRepository.Save(container);
                    scope.Complete();

                    scope.Notifications.Publish(new EntityContainerRenamedNotification(container, evtMsgs).WithStateFrom(renamingEntityContainerNotification));

                    return OperationResult.Attempt.Succeed(OperationResultType.Success, evtMsgs, container);
                }
                catch (Exception ex)
                {
                    return OperationResult.Attempt.Fail<EntityContainer>(evtMsgs, ex);
                }
            }
        }
    public Attempt <OperationResult <OperationResultType, EntityContainer>?> RenameContainer(int id, string name, int userId = Constants.Security.SuperUserId)
    {
        EventMessages eventMessages = EventMessagesFactory.Get();

        using (ICoreScope scope = ScopeProvider.CreateCoreScope())
        {
            scope.WriteLock(WriteLockIds); // also for containers

            try
            {
                EntityContainer?container = _containerRepository?.Get(id);

                //throw if null, this will be caught by the catch and a failed returned
                if (container == null)
                {
                    throw new InvalidOperationException("No container found with id " + id);
                }

                container.Name = name;

                var renamingNotification = new EntityContainerRenamingNotification(container, eventMessages);
                if (scope.Notifications.PublishCancelable(renamingNotification))
                {
                    scope.Complete();
                    return(OperationResult.Attempt.Cancel <EntityContainer>(eventMessages));
                }

                _containerRepository?.Save(container);
                scope.Complete();

                var renamedNotification = new EntityContainerRenamedNotification(container, eventMessages);
                renamedNotification.WithStateFrom(renamingNotification);
                scope.Notifications.Publish(renamedNotification);

                return(OperationResult.Attempt.Succeed(OperationResultType.Success, eventMessages, container));
            }
            catch (Exception ex)
            {
                return(OperationResult.Attempt.Fail <EntityContainer>(eventMessages, ex));
            }
        }
    }