Example #1
0
 public void CheckNullThrowsArgumentException(string value, bool shouldThrow)
 {
     if (shouldThrow)
     {
         Assert.Throws <ArgumentException>(() => SanityChecks.CheckNull(value, "test-method"));
     }
     else
     {
         SanityChecks.CheckNull(value, "test-method");
     }
 }
        ///<summary>
        /// Actual Crud.Update method using CommandContext and StorageEngine
        ///</summary>
        internal static async Task <OperationResult> UpdateAsync(CommandContext context)
        {
            string methName = $"{nameof(AllCommands)}.{nameof(UpdateAsync)}";

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

            var configEntity = await TryExtractConfigEntityAsync(methName, context, CommandTypes.UpdateValue, c => { c.Value = context.ChangeRequest.Value; return(c); });

            if (configEntity.Success == false)
            {
                return(configEntity.ThrowError());
            }
            // return result
            return(OperationResult.Success(configEntity.Entity));
        }
        ///<summary>
        /// Actual Crud.Delete method using CommandContext and StorageEngine
        ///</summary>
        internal static async Task <OperationResult> DeleteAsync(CommandContext context)
        {
            string methName = $"{nameof(AllCommands)}.{nameof(DeleteAsync)}";

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

            var configEntityResult = await TryExtractConfigEntityAsync(methName, context, CommandTypes.Delete, c => c);

            if (configEntityResult.Success == false)
            {
                return(configEntityResult.ThrowError());
            }

            // return result
            return(OperationResult.Success(configEntityResult.Entity));
        }
        ///<summary>
        /// Actual Query to get Data from the StorageModel
        ///</summary>
        internal static async Task <OperationResult> QueryConfigEntityAsync(QueryContext context)
        {
            string methName = $"{nameof(AllQueries)}.{nameof(QueryConfigEntityAsync)}";

            SanityChecks.CheckNull(context, methName, "QueryContext");
            SanityChecks.IsSameOperationType(QueryTypes.ValueRequest.ToString(), context.QueryType.ToString());

            // check if exists
            var config = await context.StorageEngine.GetEntityByNameAsync(context.ConfigurationEntryKey);

            if (config == null)
            {
                return(SanityChecks.NotFound(context.ConfigurationEntryKey, methName));
            }

            // return entity
            return(OperationResult.Success(config));
        }
        ///<summary>
        /// Actual Query for all Config Entities using the StorageModel.
        ///</summary>
        internal static async Task <OperationResult> QueryAllConfigEntityValuesAsync(QueryContext context)
        {
            string methName = $"{nameof(AllQueries)}.{nameof(QueryConfigEntityAsync)}";

            SanityChecks.CheckNull(context, methName, "QueryContext");
            SanityChecks.IsSameOperationType(QueryTypes.AllValues.ToString(), context.QueryType.ToString());

            // check if exists
            var configs = await context.StorageEngine.AllEntitesAsync();

            if (configs == null || configs.Any() == false)
            {
                return(SanityChecks.NotFound(context.ConfigurationEntryKey, methName));
            }

            // return entity
            var collection = new ConfigCollection {
                 WrappedConfigEntities = configs
            };

            return(OperationResult.Success(collection));
        }
        ///<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));
        }