コード例 #1
0
        private static async Task <ConfigEntityResult> TryExtractConfigEntityAsync(string initiatingMethod, CommandContext context, CommandTypes storeType, Func <ConfigEntity, ConfigEntity> adjustEntity)
        {
            // get config entity if exists
            var config = await context.StorageEngine.GetEntityByNameAsync(context.ConfigurationEntryKey);

            if (config == null)
            {
                return(new ConfigEntityResult
                {
                    Success = false,
                    ThrowError = () => SanityChecks.NotFound(context.ConfigurationEntryKey, initiatingMethod)
                });
            }
            // for diff checking
            var before = new ConfigEntity
            {
                Id    = config.Id,
                Name  = new string(config.Name.ToCharArray()),
                Value = new string(config.Value.ToCharArray())
            };

            // callback for entity
            config = adjustEntity(config);

            config.CrudOperationName = storeType;
            bool isSuccess = await context.StorageEngine.StoreEntityAsync(config);

            if (!isSuccess)
            {
                return(new ConfigEntityResult
                {
                    ThrowError = () => SanityChecks.StorageFailed <ConfigEntity>
                                 (
                        context.CommandType.ToString(),
                        initiatingMethod
                                 ),
                    Success = false
                });
            }

            return(new ConfigEntityResult
            {
                Before = before,
                Entity = config,
                Success = true
            });
        }
コード例 #2
0
        ///<summary>
        /// Actual Crud.Create Command using the CommandContext and the StorageEngine
        ///</summary>
        internal static async Task <OperationResult> CreateAsync(CommandContext context)
        {
            string methName = $"{nameof(AllCommands)}.{nameof(CreateAsync)}";

            SanityChecks.CheckNull(context, methName);
            SanityChecks.IsSameOperationType(CommandTypes.Create.ToString(), context.CommandType.ToString());

            // create a new entity from key and value
            var entity = GenerateConfigEntityFromChangeRequest(context.ChangeRequest);

            bool success = false;

            entity.CrudOperationName = CommandTypes.Create;

            // try store
            success = await context.StorageEngine.StoreEntityAsync(entity);

            if (!success)
            {
                return(SanityChecks.StorageFailed <ConfigEntity>(context.CommandType.ToString(), methName));
            }

            return(OperationResult.Success(entity));
        }
コード例 #3
0
        public void StorageFailedThrowsOperationFailure()
        {
            var operationResult = SanityChecks.StorageFailed <string>("fullname", "methodname");

            Assert.Equal(ResultType.Forbidden, operationResult.ResultType);
        }